处理远程连接异常
Handling a remote connection exception
此脚本的预期结果是找出主机列表中的 PowerShell 版本。我想在远程机器关闭或没有启用远程处理时处理异常。但是,catch
似乎没有输入。只会出现来自 PowerShell 的标准红色火焰状错误消息。
我需要做什么来捕获异常?
X:\Scripts\PSAutomation> Get-Content .\get-versions.ps1
server_list = @(
'CAPPY'
)
$server_list |
ForEach-Object {
Try {
Invoke-Command -ComputerName $_ {$PSVersionTable.PSVersion}
}
Catch
{
Write-Host "Failed to connect to $_"
}
}
X:\Scripts\PSAutomation> .\get-versions.ps1
[CAPPY] Connecting to remote server CAPPY failed with the following error message : WinRM cannot process the
request. The following error occurred while using Kerberos authentication: Cannot find the computer CAPPY. Verify
that the computer exists on the network and that the name provided is spelled correctly. For more information, see
the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OpenError: (CAPPY:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : NetworkPathNotFound,PSSessionStateBroken
只需将该调用的 ErrorAction
设置为 stop
:
Invoke-Command -ComputerName "sdf" {$PSVersionTable.PSVersion} -ErrorAction Stop
此脚本的预期结果是找出主机列表中的 PowerShell 版本。我想在远程机器关闭或没有启用远程处理时处理异常。但是,catch
似乎没有输入。只会出现来自 PowerShell 的标准红色火焰状错误消息。
我需要做什么来捕获异常?
X:\Scripts\PSAutomation> Get-Content .\get-versions.ps1
server_list = @(
'CAPPY'
)
$server_list |
ForEach-Object {
Try {
Invoke-Command -ComputerName $_ {$PSVersionTable.PSVersion}
}
Catch
{
Write-Host "Failed to connect to $_"
}
}
X:\Scripts\PSAutomation> .\get-versions.ps1
[CAPPY] Connecting to remote server CAPPY failed with the following error message : WinRM cannot process the
request. The following error occurred while using Kerberos authentication: Cannot find the computer CAPPY. Verify
that the computer exists on the network and that the name provided is spelled correctly. For more information, see
the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OpenError: (CAPPY:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : NetworkPathNotFound,PSSessionStateBroken
只需将该调用的 ErrorAction
设置为 stop
:
Invoke-Command -ComputerName "sdf" {$PSVersionTable.PSVersion} -ErrorAction Stop