Enter PSSession - then Errors

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Post Reply
Posts: 19
Joined: Tue Apr 16, 2019 8:16 pm

Enter PSSession - then Errors

Post by plautodfl » Wed Sep 25, 2019 12:32 pm

If I run lines 1,2,3,4 the script fails on line 4 and says profile location not found.
If I run lines 1,2,3 wait until the PSSession is created then run line 4 everything works.
How do I initiate a wait until the PSSession is complete before proceeding to the next line in the script?

$ServerName = "***-*-*-*****"
Enter-PSSession $servername
$ProfileLocation = "d:\Profiles"
cd $ProfileLocation

User avatar
Alexander Riedel
Posts: 7049
Joined: Tue May 29, 2007 4:43 pm

Re: Enter PSSession - then Errors

Post by Alexander Riedel » Wed Sep 25, 2019 1:18 pm

[Topic moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
Posts: 13793
Joined: Tue May 22, 2007 9:57 am

Re: Enter PSSession - then Errors

Post by jvierra » Wed Sep 25, 2019 1:27 pm

You can't use Enter-PsSession in a script. It is only allowed in an interactive session. Use the following to remote commands.

Code: Select all

$ServerName = "***-*-*-*****"
$sb = {
 	cd d:\Profiles
	Write-Host $env:COMPUTERNAME $PWD
Invoke-Command -ScriptBlock $sb -ComputerName $servername
This will execute the commands in the scriptblocks on the remote server.

Read the documentation provided that discusses remoting and how it works.
help about_remote*

Post Reply