Today, I kind of stumbled across a way of connecting to a remote host quite a bit more sophisticated. Basically, the difference is that it will check to see if the connection was successful or not.
This still works off the basic premise of opening a new PowerShell process and using the -ArgumentList parameter to instantly connect to a remote system. This is the command I use (THIS IS JUST FOR CLEARER EXPLANATION, USE THE ONE-LINER BELOW):
Start-Process PowerShell -ArgumentList "-noexit -command Clear-Host; try{ Enter-PSSession -ComputerName $COMPUTERNAMEHERE -ErrorAction Stop; Write-Host 'You should now be connected to the remote host, check below...' -ForeGroundColor Green }catch{ Write-Host 'Could not perform action - most likely that access is denied for the invoke' -ForeGroundColor Red } "
Or as a one-liner:
Start-Process PowerShell -ArgumentList "-noexit -command Clear-Host; try{ Enter-PSSession -ComputerName $COMPUTERNAMEHERE -ErrorAction Stop; Write-Host 'You should now be connected to the remote host, check below...' -ForeGroundColor Green }catch{Write-Host 'Could not perform action - most likely that access is denied for the invoke' -ForeGroundColor Red } "
This is what the command does
- Starts a new powershell console
- clears the host
- tries to the connection
- writes output to console
Enjoy!