0

I have this script in the client computer that try to run a script from the server computer:

try {
      $s = New-PSSession -ComputerName "name" -Authentication CredSSP -Credential $credential
      Enter-PSSession -Id $s.Id
      Set-ExecutionPolicy ByPass -Scope CurrentUser
      Invoke-Command -Session $s -FilePath c:\release1.ps1
} catch {
      Write-Error "Error occurred: " $_.Exception.ToString()
} finally {
      Exit-PSSession
      Remove-PSSession $s }

the script in the server is something like this

Set-Alias vmrun "C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe" Start-Process -NoNewWindow -FilePath vmrun -ArgumentList " -T ws revertToSnapshot M:\myvm.vmx Release1"

but I got the error

Write-Error : A positional parameter cannot be found that accepts argument 'System.Management.Automation.ItemNotFoundException: Cannot find path 'C:\release1.ps1' because it does not exist.

3
  • 1
    -FilePath must specify a path to a file on the local computer. Is C:\release1.ps1 on the local computer or the remote one? Commented Apr 24, 2013 at 14:30
  • the file is in the remote computer Commented Apr 24, 2013 at 14:37
  • Note that Enter-PSSession -Id $s.Id enters an interactive session that the user must explicitly exit. Therefore, all subsequent commands (a) don't execute until that has happened and (b) then execute locally. Commented Jan 18, 2022 at 20:55

1 Answer 1

2

-FilePath must specify a path to a file on the local computer. The file is read locally, then passed to the remote computer as a block of code.

See http://technet.microsoft.com/en-us/library/hh849719.aspx:

Runs the specified local script on one or more remote computers. Enter the path and file name of the script, or pipe a script path to Invoke-Command. The script must reside on the local computer or in a directory that the local computer can access. Use the ArgumentList parameter to specify the values of parameters in the script.

The solution is to put release1.ps1 on the local computer. Or if it must be on the remote computer, then put it in a share that is accessible to the local computer and access it with a UNC path.

Invoke-Command -Session $s -FilePath \\name\share\release1.ps1
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.