Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell:使用Try-Catch调用命令_Powershell_Try Catch_Invoke Command - Fatal编程技术网

PowerShell:使用Try-Catch调用命令

PowerShell:使用Try-Catch调用命令,powershell,try-catch,invoke-command,Powershell,Try Catch,Invoke Command,我使用以下代码为我提供一批计算机状态的输出: $Win2k8r2Computers = "Server1", "Server2", "Server3", "Server4" $results = Invoke-Command -ComputerName $Win2k8r2Computers { #} $props = @{} Try { <#If ($PSVersionTabl

我使用以下代码为我提供一批计算机状态的输出:

$Win2k8r2Computers = "Server1", "Server2", "Server3", "Server4"

$results = Invoke-Command -ComputerName $Win2k8r2Computers { #}
    $props = @{}
    Try {
        <#If ($PSVersionTable.PSVersion.Major -eq "2") {
            $props.Add('Message',"Server (Win2008r2) is currently running an incompatible version of PowerShell (v2.1)")
            }#>
        If (Get-Service | Where-Object { $_.Name -eq "W3SVC" } -ErrorAction Stop) {
            $props.Add('Message', "IIS is installed (Win2008r2)")
        }
        Else {
            $props.Add('Message', "IIS is NOT installed (Win2008r2)")
        }
    }
    catch {
        $props.Add('Message', 'Error: {0}' -f $_)
    }
    New-Object -Type PSObject -Prop $Props
}
$Win2k8r2Computers=“Server1”、“Server2”、“Server3”、“Server4”
$results=Invoke命令-ComputerName$Win2k8r2Computers{#}
$props=@{}
试一试{
If(获取服务| Where对象{$\.Name-eq“W3SVC”}-ErrorAction停止){
$props.Add('消息',“IIS已安装(Win2008r2)”)
}
否则{
$props.Add('消息',“未安装IIS(Win2008r2)”)
}
}
抓住{
$props.Add('Message','Error:{0}'-f$\
}
新对象类型PSObject-Prop$Props
}

它按预期工作,而不是catch看起来没有实际捕获并将错误返回到$results变量。我缺少什么?

在当前代码中,您只将参数
-ErrorAction
传递给
Where Object
。因此,您只能捕获
Where-Object
cmdlet的错误<代码>获取服务仍将使用默认的
错误操作
继续
运行

要将
Get Service
Where Object
的错误转化为可捕获的终止错误,请将
-ErrorAction“Stop”
传递给这两个错误

If (Get-Service -ErrorAction Stop | Where-Object { $_.Name -eq "W3SVC" } -ErrorAction Stop)
。。。或者(更有效地)在脚本开头设置
$ErrorActionPreference
变量,并删除
-ErrorAction
参数:

$Win2k8r2Computers = "Server1", "Server2", "Server3", "Server4"

$results = Invoke-Command -ComputerName $Win2k8r2Computers { #}
    $props = @{}
    Try {
        $ErrorActionPreference = 'Stop'

        <#If ($PSVersionTable.PSVersion.Major -eq "2") {
            $props.Add('Message',"Server (Win2008r2) is currently running an incompatible version of PowerShell (v2.1)")
            }#>
        If (Get-Service | Where-Object { $_.Name -eq "W3SVC" }) {
            $props.Add('Message', "IIS is installed (Win2008r2)")
        }
        Else {
            $props.Add('Message', "IIS is NOT installed (Win2008r2)")
        }
    }
    catch {
        $props.Add('Message', 'Error: {0}' -f $_)
    }
    New-Object -Type PSObject -Prop $Props
}
$Win2k8r2Computers=“Server1”、“Server2”、“Server3”、“Server4”
$results=Invoke命令-ComputerName$Win2k8r2Computers{#}
$props=@{}
试一试{
$ErrorActionPreference='Stop'
If(获取服务| Where对象{$\.Name-eq“W3SVC”}){
$props.Add('消息',“IIS已安装(Win2008r2)”)
}
否则{
$props.Add('消息',“未安装IIS(Win2008r2)”)
}
}
抓住{
$props.Add('Message','Error:{0}'-f$\
}
新对象类型PSObject-Prop$Props
}
注意事项:

PowerShell脚本模块中的cmdlet将忽略
$ErrorActionPreference
,除非它们特别注意处理它。 请参阅以获得一些见解


但在这种情况下,它可以工作,因为这两个cmdlet都是在C#模块中实现的。

如果未找到
W3SVC
服务,是否要抛出错误?因此,您希望看到一条消息
IIS未安装(Win2008r2)
,以及搜索该服务时出现的错误消息?不,如果无法连接到服务器,则会引发错误。我现在意识到,-ErrorAction现在完全在错误的地方。但是,感谢zett42,我不知道我怎么会错过,-ErrorAction不在正确的地方,并且已经按照您的建议纠正了这一点,但是,不管怎样,$result输出中似乎仍然没有捕捉到错误。@jshizzle在本地机器上为我工作。尝试在
$ErrorActionPreference
行之后插入
写入错误'myerror'
。这是否会添加到
$result
?同样,当我运行它时(使用产生连接错误的计算机),输出会显示在控制台中,但在调用$results时不会显示。从那以后,我使用了另一种方法来获取信息并在调用命令块之外对其进行操作,但我仍然对如何在某一点上使上述方法起作用感兴趣,因为我认为它可能是有用的替代方法,并且有它的位置。@jshizzle对于产生连接错误的计算机来说,这到底是什么意思?您的意思是“<代码>调用命令< /代码>被中断,因为到远程机器的连接在执行的中间丢失了吗?我的意思是当最初的远程管理连接尝试和失败时。我正在筛选计算机以检查是否安装了IIS,但希望捕获由于连接错误而无法访问的计算机。