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.
This topic is 4 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
plautodfl
Posts: 30
Last visit: Thu Jun 09, 2022 6:09 am

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: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Enter PSSession - then Errors

Post by Alexander Riedel »

[Topic moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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*
This topic is 4 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked