Enter PSSession - then Errors

Ask your 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.
Posts: 24
Joined: Tue Apr 16, 2019 8:16 pm

Enter PSSession - then Errors

Post by plautodfl »

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: 7398
Joined: Tue May 29, 2007 4:43 pm

Re: Enter PSSession - then Errors

Post by Alexander Riedel »

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

Posts: 14475
Joined: Tue May 22, 2007 9:57 am

Re: Enter PSSession - then Errors

Post by jvierra »

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*