1

I'm getting an error while using New-PSSesion and Enter-PSSesion together, but they are working seperately. My aim is to copy a file from local server to remote server and afterwards run some commands in remote server

Below is my script:

$Session = New-PSSession -ComputerName "IP_Address" -Credential "domainname\username"
Copy-Item "C:\Users\username\test1.txt" -Destination "C:\Users\username\" -ToSession $Session
Enter-PSSession $Session
Copy-Item "C:\Users\username\" -Destination "C:\Users\username\targetdir\"
...
Exit-PSSession

When I run above script with PowerShellISE, it results in an error:

Copy-Item : Cannot find path 'C:\Users\username\test1.txt' because it does not exist.
At line:4 char:1
+ Copy-Item "C:\Users\username\test1.txt" -Dest ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\username\test1.txt:String) [Copy-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

The strange thing is, when I run these from the powershell CLI (not ISE), it works.

What is the reason behind this?

2
  • 1
    Are you running this as a script, or are you manually typing the commands into the a shell? Enter-PSSession is for interactive use only, for a script you'd use Invoke-Command instead Commented Mar 11, 2022 at 13:32
  • Note: Unlike what the error message suggests, I suspect it is the second Copy-Item command that causes the problem, which - unlike what you expect - also runs locally. Commented Mar 11, 2022 at 17:59

1 Answer 1

3

Enter-PSSession's purpose is to enter an interactive session on a remote machine that the user must exit manually, by submitting exit or its equivalent (in remote sessions only), Exit-PSSession[1]

  • The only conceivable reason to use it in a script is to pause the running script in order to enter an interactive remote session that the user can operate in. Once the user exits the interactive session, the script continues locally.

  • However, due to a bug that doesn't actually work as of PowerShell 7.2.1, and execution unexpectedly continues locally right away (which is what you're seeing) - see this answer.

For automated remote execution, use Invoke-Command instead.

  • It requires that you pass all statements to execute remotely to its -ScriptBlock parameter in the form of a script block { ... }.

  • If you need to pass multiple batches of statements to a remote computer at different times, you can create an explicit session object with New-PSSession and use it for every Invoke-Command call with the -Session parameter (instead of implicitly creating a one-off session via -ComputerName).

For more information, see the conceptual about_Remote help topic.

In your case, replace the Enter-PSSession ... Exit-PSSession block of lines with the following:

Invoke-Command -Session $Session -ScriptBlock {
  Copy-Item "C:\Users\username\" -Destination "C:\Users\username\targetdir\"
  # ...
}

[1] Exit-PSSession exists primarily for symmetry with Enter-PSSession. There is no strict reason to use it; always using exit - which works in both local and remote sessions - is fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.