1
이 스크립트의 결과는 호스트 목록에서 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'은 일반 콘솔 텍스트 색상으로 표시하는 빨간색 메시지가 나타납니다'사용. 그러나'Write-Host'에 대해서는 아무 것도 나타나지 않습니다. – lit
당신이 틀렸다고 생각합니다. 메시지는 연결 실패 ...로 시작해야합니다. 캐치 블록의 $ _은 현재 foreach-object 파이프 라인 변수 대신 오류를 나타 내기 때문입니다. '$ current = $ _'와 같이 쓰기 전에 호스트 변수에'$ _' 대신'$ current'를 사용하십시오. –
예. 당신 말이 맞아요. 나는 그것을보아야했다. – lit