1

PowerShell Version 5 build 10586

I am using the following code to remote connect to a server from my local PC

$cimSession = New-CimSession -ComputerName "SERVERNAME.DOMAIN.COM"


     Invoke-Command -ComputerName SERVERNAME -ScriptBlock {Get-ChildItem “C:\Temp\ps”}

     Invoke-Command -ComputerName SERVERNAME -FilePath "\\SERVERNAME\c$\Temp\ps\PS_SCRIPT_FILE.ps1"


Remove-CimSession -CimSession $cimSession

The first command is able to run successfully and sees the PowerShell file on the remote server.

The second command fails with an error:

Invoke-Command : Cannot find path '\\SERVERNAME\c$\Temp\ps\PS_SCRIPT_FILE.ps1' because it does not exist.

Is there another way to call/run a PowerShell script on the C Drive of a remote server?

  • I have granted my account full access to the specified file.
  • I have also tried to share the specified folder and given myself Read/Write access to the folder.
  • I have changed the file path to the share path and get the same result.
  • Invoke-Command -ComputerName SERVERNAME -FilePath "\\SERVERNAME\ps\PS_SCRIPT_FILE.ps1"
1
  • Note that your Invoke-Command commands are not using the $cimSession they are using WinRM protocol to invoke the commands, not WSMAN. This is why the first command worked. In your example above you do not even need to establish a CimSession. Either already be running under credentials that can use WinRM; or use the -Credentials option to pass credentials if you are not authenticated to the remote server. technet.microsoft.com/en-au/library/hh849719.aspx Commented Aug 25, 2016 at 3:39

1 Answer 1

2

Try this:

Invoke-Command -ComputerName SERVERNAME -ScriptBlock { Invoke-Command -FilePath "C:\Temp\ps\PS_SCRIPT_FILE.ps1" }

In your existing code the -FilePath parameter is processed on the calling machine. By including that as a parameter within the ScriptBlock it should be processed on the target machine.

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.