Perhaps you can help me with the following challenge:
I want to run a PSSession command in a script, but I want the main script to continue ONLY when the PSSession has ended.
Write-Host "Starting remote session..."
$PSSession = New-PSSession "$Server" -Authentication Credssp -Credential $Credential
Enter-PSSession -Session $PSSession
## SHOW WHEN REMOTE SESSIE HAS ENDED
Write-Host "Remote session ended"
# Continue script
When I try it like above, the script will continue and leaving the session open in the process. If anyone has an idea how to make the script wait before continuing, please let me know!
Enter-PSSession is used for an interactive shell. In a script, you’d typically want to use Invoke-Command instead. What’s your intention with this script? Do you want to have the caller doing things interactively in the remote session, then exit when they’re done and have the script continue?
Doesn’t seem to work. What’s actually happening is the script runs to its completion, and then you get dumped out to an interactive prompt from Enter-PSSession. It doesn’t interrupt the script at all.