# PSSession in script

**URL:** <https://forums.powershell.org/t/pssession-in-script/6055>\
**Category:** PowerShell Help\
**Created:** [March 22, 2016, 10:52pm UTC](https://forums.powershell.org/t/pssession-in-script/6055 "2016-03-22T22:52:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![beranmuden](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/beranmuden/32/4019_2.png) [@beranmuden](https://forums.powershell.org/u/beranmuden)\
**Post date:** [March 22, 2016, 10:52pm UTC](https://forums.powershell.org/t/pssession-in-script/6055/1 "2016-03-22T22:52:16Z")

</div>

Hi all,

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!

Thanks and regards,

Troy

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [March 23, 2016, 12:06am UTC](https://forums.powershell.org/t/pssession-in-script/6055/2 "2016-03-23T00:06:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![beranmuden](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/beranmuden/32/4019_2.png) [@beranmuden](https://forums.powershell.org/u/beranmuden)\
**Post date:** [March 23, 2016, 12:25am UTC](https://forums.powershell.org/t/pssession-in-script/6055/3 "2016-03-23T00:25:49Z")

</div>

“doing things interactively in the remote session, then exit when they’re done and have the script continue”

Exactly that.

I use Invoke for other stuff, which works perfectly. But I also want to be able to start an interactive session which interrupts the main script.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [March 23, 2016, 12:52am UTC](https://forums.powershell.org/t/pssession-in-script/6055/4 "2016-03-23T00:52:55Z")

</div>

I’ve never tried that before. Will experiment a bit and see what I can find.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [March 23, 2016, 1:07am UTC](https://forums.powershell.org/t/pssession-in-script/6055/5 "2016-03-23T01:07:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![beranmuden](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/beranmuden/32/4019_2.png) [@beranmuden](https://forums.powershell.org/u/beranmuden)\
**Post date:** [March 23, 2016, 1:26am UTC](https://forums.powershell.org/t/pssession-in-script/6055/6 "2016-03-23T01:26:34Z")

</div>

Exactly my problem 🙂

Thanks for thinking along Dave!

---

<div class="post-metadata">

**Author:** ![beranmuden](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/beranmuden/32/4019_2.png) [@beranmuden](https://forums.powershell.org/u/beranmuden)\
**Post date:** [March 23, 2016, 8:49pm UTC](https://forums.powershell.org/t/pssession-in-script/6055/7 "2016-03-23T20:49:33Z")

</div>

Think I might have found a way.  
Have a look at my solution:

```
## some scripting

## ask for remote session part

    if ($DoSession -eq "Y")
        {
        cls
        $DoSessionPS1 = $env:SystemRoot+"\temp\doremote.ps1"
        if (Test-Path $DoSessionPS1) {del $DoSessionPS1}
        "`$Credential = (Get-Credential $env:USERNAME)" >$DoSessionPS1
        "`$PSSession = New-PSSession $Server -Authentication Credssp -Credential `$Credential" >>$DoSessionPS1
        "Enter-PSSession -Session `$PSSession" >>$DoSessionPS1
        cmd.exe /c start Powershell.exe -NoExit -File $DoSessionPS1
        }
    else {Write-Host "Operation Aborted" -BackgroundColor Red -ForegroundColor Yellow}
    }
Read-Host "Press enter to continue..."
Get-PSSession |Remove-PSSession
```

Perhaps it’s not very pretty, but it does the trick for now.  
Again, thanks for thinking along!

Regards,

T.O.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:41pm UTC](https://forums.powershell.org/t/pssession-in-script/6055/8 "2024-05-16T20:41:44Z")

</div>

Closed on May 16, 2024, 8:41pm UTC


