如果未启用服务器远程处理,则退出 PS1 脚本(Test-WsMan [计算机 IP])

Exit PS1 script if server remoting not-enabled (Test-WsMan [Computer IP])

客户端的这个 cmdlet 允许我检查服务器是否启用了 Powershell 远程处理。在下面的示例中,结果显示为:

PS C:\WINDOWS\system32> Test-WsMan xxx.104.50.xxx  

wsmid           : http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.xsd
ProtocolVersion : http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd
ProductVendor   : Microsoft Corporation
ProductVersion  : OS: 0.0.0 SP: 0.0 Stack: 3.0

但是,我不知道如何从 运行 Test-WsMan 中获取 true/false。 在我的自动化脚本中,我想检查是否启用了远程处理,如果没有,则停止脚本并退出。

我希望我的问题有道理,我希望有人比我聪明得多。

来自MSDN

If the tested computer is running the service, the cmdlet displays the WS-Management identity schema, the protocol version, the product vendor, and the product version of the tested service.

MSDN 没有明确说明它不是 运行 服务时的作用,但我相信您可以将其视为真实的虚假信息。如果没有返回任何内容,则说明它不工作。 PowerShell 非常擅长将表达式作为布尔值求值。

if(test-wsman $computer -ErrorAction SilentlyContinue){
    # Remoting is enabled
} else {
    # Something is wrong. 
}

-ErrorAction SilentlyContinue 将涵盖任何失败原因,例如名称错误或无法联系。如果远程处理 "not working".

,则由您决定要做什么