Тестировать соединение WinRM/WSMan?
Я пытаюсь проверить, работает ли winrm в списке систем; однако я не могу уловить / заставить замолчать ошибку, которая появляется, когда я пытаюсь подключиться к системе. Похоже, работает на одной системе:
PS C:\Users\Egr> winrm id -r:system1
IdentifyResponse
ProtocolVersion = http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd
ProductVendor = Microsoft Corporation
ProductVersion = OS: x.x.xxxx SP: x.x Stack: x.x
Но не работает на другом:
PS C:\Users\Egr> winrm id -r:system2
WSManFault
Message = WinRM cannot process the request. The following error occured while using Kerberos authentication: The net
work path was not found.
Possible causes are:
-The user name or password specified are invalid.
-Kerberos is used when no authentication method and no user name are specified.
-Kerberos accepts domain user names, but not local user names.
-The Service Principal Name (SPN) for the remote computer name and port does not exist.
-The client and remote computers are in different domains and there is no trust between the two domains.
After checking for the above issues, try the following:
-Check the Event Viewer for events related to authentication.
-Change the authentication method; add the destination computer to the WinRM TrustedHosts configuration setting or use
HTTPS transport.
Note that computers in the TrustedHosts list might not be authenticated.
-For more information about WinRM configuration, run the following command: winrm help config.
Error number: -2147024843 0x80070035
The network path was not found.
Я пытался окружить его блоком try / catch, но, похоже, это не заставляет его замолчать. Я пытаюсь выполнить проверку этих систем, чтобы определить, на каких из них WinRM настроен правильно и работает; но если скрипт продолжит выводить этот текст, он не будет работать очень аккуратно. Есть ли способ подавить этот текст, или есть лучший способ проверить подключение WinRM?
1 ответ
Решение
Вы можете перенаправить поток ошибок на $null
и оценить $LastExitCode
обнаружить ошибку:
$rhost = 'system2'
winrm id -r:$rhost 2>$null
if ($LastExitCode -eq 0) {
Write-Host "$rhost OK" -ForegroundColor green
} else {
Write-Host "$rhost unavailable" -ForegroundColor red
}