Testing if Enter-PSSession is successful
Enter-PSSession
is exclusively for interactive use.
To test whether remoting is enabled, use New-PSSession
:
$testSession = New-PSSession -Computer $targetComputer
if(-not($testSession))
{
Write-Warning "$targetComputer inaccessible!"
}
else
{
Write-Host "Great! $targetComputer is accessible!"
Remove-PSSession $testSession
}
If successful, New-PSSession
will return the new PSSession object - if it failed it won't return anything, and $testSession
is $null
(thus making -not($testSession) -eq $true
)
You can also use
Test-WSMan computername
or with autentication:
Test-WSMan myserver -Credential peter -Authentication Negotiate
and then check the return object.
If that works PSSession should also work.
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."}
}