我希望我的PowerShell脚本在我运行的任何命令失败时停止(例如set -e在bash中).我正在使用Powershell命令(New-Object System.Net.WebClient)和程序(.\setup.exe).
Out-File 似乎在使用UTF-8时强制BOM:
$MyFile = Get-Content $MyPath
$MyFile | Out-File -Encoding "UTF8" $MyPath
Run Code Online (Sandbox Code Playgroud)
如何使用PowerShell以UTF-8编写没有BOM的文件?
我正在尝试执行此powershell命令
Invoke-WebRequest -Uri https://apod.nasa.gov/apod/
我收到这个错误."Invoke-WebRequest:请求已中止:无法创建SSL/TLS安全通道." https请求似乎有效(" https://google.com ")但不是这个问题.如何让它工作或使用其他powershell命令来读取页面内容?
如何更改以下代码以查看目录中的所有.log文件而不仅仅是一个文件?
我需要遍历所有文件并删除所有不包含"step4"或"step9"的行.目前这将创建一个新文件,但我不知道如何在for each这里使用循环(新手).
实际文件的名称如下:2013 09 03 00_01_29.log.我希望输出文件覆盖它们,或者具有SAME名称,附加"out".
$In = "C:\Users\gerhardl\Documents\My Received Files\Test_In.log"
$Out = "C:\Users\gerhardl\Documents\My Received Files\Test_Out.log"
$Files = "C:\Users\gerhardl\Documents\My Received Files\"
Get-Content $In | Where-Object {$_ -match 'step4' -or $_ -match 'step9'} | `
Set-Content $Out
Run Code Online (Sandbox Code Playgroud) PowerShell 1.0可以创建类似于Unix的硬链接和软链接吗?
如果这不是内置的,有人可以指向我有一个模仿这个的ps1脚本的网站吗?
这是任何好壳,恕我直言的必要功能.:)
我想通过设置标志并在整个脚本中查看数据来输出PowerShell脚本中的变量和值.
我该怎么做?
例如,PowerShell与以下PHP代码等效的是什么?
echo "filesizecounter: " . $filesizecounter
Run Code Online (Sandbox Code Playgroud) 我有一个.ps1文件,我想在其中定义自定义函数.
想象一下,该文件名为MyFunctions.ps1,内容如下:
Write-Host "Installing functions"
function A1
{
Write-Host "A1 is running!"
}
Write-Host "Done"
Run Code Online (Sandbox Code Playgroud)
要运行此脚本并理论上注册A1函数,我导航到.ps1文件所在的文件夹并运行该文件:
.\MyFunctions.ps1
Run Code Online (Sandbox Code Playgroud)
这输出:
Installing functions
Done
Run Code Online (Sandbox Code Playgroud)
然而,当我尝试调用A1时,我只是得到错误,指出该名称没有命令/功能:
The term 'A1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling
of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:3
+ A1 <<<<
+ CategoryInfo : ObjectNotFound: (A1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)
我必须误解一些PowerShell概念.我可以不在脚本文件中定义函数吗?
请注意 …
使用PowerShell,是否可以删除包含文件的某个目录而不提示确认操作?
我有一个.zip文件,需要使用Powershell解压缩整个内容.我这样做但它似乎不起作用:
$shell = New-Object -ComObject shell.application
$zip = $shell.NameSpace("C:\a.zip")
MkDir("C:\a")
foreach ($item in $zip.items()) {
$shell.Namespace("C:\a").CopyHere($item)
}
Run Code Online (Sandbox Code Playgroud)
怎么了?该目录C:\a仍为空.