Page 1 of 1

Enter PSSession - then Errors

Posted: Wed Sep 25, 2019 12:32 pm
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

Re: Enter PSSession - then Errors

Posted: Wed Sep 25, 2019 1:18 pm
by Alexander Riedel
[Topic moved by moderator]

Re: Enter PSSession - then Errors

Posted: Wed Sep 25, 2019 1:27 pm
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*