在PoshCode上查看Get-WebFile脚本,http: //poshcode.org/3226 ,我注意到这个奇怪的对我来说:
$URL_Format_Error = [string]"..."
Write-Error $URL_Format_Error
return
Run Code Online (Sandbox Code Playgroud)
与以下相反,这是什么原因?
$URL_Format_Error = [string]"..."
Throw $URL_Format_Error
Run Code Online (Sandbox Code Playgroud)
甚至更好:
$URL_Format_Error = New-Object System.FormatException "..."
Throw $URL_Format_Error
Run Code Online (Sandbox Code Playgroud)
据我所知,你应该使用Write-Error来解决非终止错误,并使用Throw来终止错误,所以在我看来你不应该使用Write-Error然后使用Return.有区别吗?
我的情况:
$ErrorActionPreference = "Stop";
"1 - $ErrorActionPreference;"
Get-ChildItem NoSuchFile.txt -ErrorAction SilentlyContinue;
"2 - $ErrorActionPreference;"
Get-ChildItem NoSuchFile.txt -ErrorAction Stop;
"3 - $ErrorActionPreference;"
Run Code Online (Sandbox Code Playgroud)
输出:
1 - 停止;
2 - 停止;
并显示错误...
现在,
$ErrorActionPreference = "Stop";
"1 - $ErrorActionPreference;"
(Get-PSSessionConfiguration -Name "MyShellUri" -ErrorAction SilentlyContinue)
"2 - $ErrorActionPreference;"
Run Code Online (Sandbox Code Playgroud)
输出:
1 - 停止;
并显示错误...
为什么不能为Get-PSSessionConfiguration工作-ErrorAction SilentlyContinue?
更新:
现在,
$ErrorActionPreference = "Continue"
"1 - $ErrorActionPreference;"
(Get-PSSessionConfiguration -Name "MyShellUri" -ErrorAction SilentlyContinue)
"2 - $ErrorActionPreference;"
Run Code Online (Sandbox Code Playgroud)
输出:
1 - 继续;
2 - 继续;
现在,
$ErrorActionPreference = "SilentlyContinue"
"1 - …
Run Code Online (Sandbox Code Playgroud) 我正在使用Jenkins PowerShell插件来构建项目.
但是,我发现Jenkins总是认为我的构建成功,无论我在Windows PowerShell
命令内输入什么.
这是一个例子:
如您所见,asdf
这不是一个合法的命令.詹金斯应该FAILURE
在构建之后给我.
但控制台输出给了我:
Started by user admin
Building in workspace C:\Users\Administrator\.jenkins\jobs\Test\workspace
[workspace] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\Users\ADMINI~1\AppData\Local\Temp\hudson2092642221832331776.ps1'"
The term 'asdf' 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 C:\Users\ADMINI~1\AppData\Local\Temp\hudson2092642221832331776.ps1:1 char:5
+ asdf <<<<
+ CategoryInfo : ObjectNotFound: (asdf:String) [], …
Run Code Online (Sandbox Code Playgroud) 我今天第一次给PowerShell(v3.0)拍摄了一个镜头,并且对它的一些错误处理概念实现的奇怪方式感到非常沮丧.
我编写了以下代码(使用远程注册表PowerShell模块)
try
{
New-RegKey -ComputerName $PCName -Key $Key -Name $Value
Write-Host -fore Green ($Key + ": created")
}
catch
{
Write-Host -fore Red "Unable to create RegKey: " $Key
Write-Host -fore Red $_
}
Run Code Online (Sandbox Code Playgroud)
(这只是一个片段)
显然,PowerShell的默认行为是不捕获非终止的错误.所以我按照不同的人的建议在我的脚本顶部添加了以下行:
$ErrorActionPreference = "Stop"
Run Code Online (Sandbox Code Playgroud)
在PowerShell ISE中执行此操作确实捕获了所有错误.但是,从终端执行以下命令仍然无法捕获我的错误.
来自ISE:
PS C:\windows\system32> C:\Data\Scripts\PowerShell\Error.ps1
Errorhandling: Stop
SOFTWARE\MySoftware does not exist. Attempting to create
Unable to create RegKey: SOFTWARE\MySoftware
Key 'SOFTWARE\MySoftware' doesn't exist.
Run Code Online (Sandbox Code Playgroud)
从命令行:
PS C:\Data\Scripts\PowerShell> .\Error.ps1
Errorhandling: Stop
SOFTWARE\MySoftware does not exist. Attempting to create
New-RegKey : Key …
Run Code Online (Sandbox Code Playgroud) 为什么在使用调用运算符时使用非零退出代码时,PowerShell脚本不会结束$ErrorActionPerference = "Stop"
?
使用以下示例,我得到结果managed to get here with exit code 1
:
$ErrorActionPreference = "Stop"
& cmd.exe /c "exit 1"
Write-Host "managed to get here with exit code $LASTEXITCODE"
Run Code Online (Sandbox Code Playgroud)
调用运算符的Microsoft文档没有讨论使用call运算符时应该发生什么,它只说明以下内容:
运行命令,脚本或脚本块.调用运算符(也称为"调用运算符")允许您运行存储在变量中并由字符串表示的命令.由于调用操作符不解析命令,因此无法解释命令参数.
此外,如果这是预期的行为,是否有任何其他方法让调用操作符导致错误而不是让它继续?