testingEnter-PSSession是否成功

有没有方法来testingEnter-PSSession是否成功,或者如果Enable-Remoting已经是真的? 我不需要能够进入机器本身,只是一个方法来找出返回代码。 基本上,只是检查是否可以完成远程login到机器,或者如果机器仍然需要远程启用。

Enter-PSSession专用于交互式使用。

要testing是否启用远程处理,请使用New-PSSession

 $testSession = New-PSSession -Computer $targetComputer if(-not($testSession)) { Write-Warning "$targetComputer inaccessible!" } else { Write-Host "Great! $targetComputer is accessible!" Remove-PSSession $testSession } 

如果成功, New-PSSession将返回新的PSSession对象 – 如果失败,它将不返回任何东西,而$testSession$null (从而使-not($testSession) -eq $true

你也可以使用

  Test-WSMan computername 

或者与validation:

  Test-WSMan myserver -Credential peter -Authentication Negotiate 

然后检查返回对象。

如果这个工作PSSession也应该工作。

 function CanRemote { $session = New-PSSession $ComputerName -ErrorAction SilentlyContinue if ($session -is [System.Management.Automation.Runspaces.PSSession]) {Write-Host "Remote test succeeded: $ComputerName."} else {Write-Host "Remote test failed: $ComputerName."} }