Home > database >  Invoke-Command not finding script, but it's there
Invoke-Command not finding script, but it's there

Time:03-18

I'm attempting to run a very simple script on a different server and am getting an ItemNotFoundException. Here's my script:

Invoke-Command  -ComputerName myserver -FilePath C:\laurietest\testthis.ps1  
Invoke-Command -ComputerName myserver  -ScriptBlock {
    Write-Output "Directory before copying:"
    Get-ChildItem C:\laurietest\
    Copy-Item -Path C:\laurietest\output.txt -Destination C:\laurietest\output2.txt
      Write-Output "Directory after copying:"
      Get-ChildItem C:\laurietest\
}

And here's the output I get when I run the command: enter image description here It lists the contents of the directory and the file testthis.ps1 is definitely there, but the Invoke-Command can't seem to find it. Any ideas?

CodePudding user response:

The -FilePath parameter expects a local file path.

If the path refers to a file on the remote machine you'll want to use the -ScriptBlock parameter instead:

Invoke-Command -ComputerName myserver -ScriptBlock { C:\laurietest\testthis.ps1 }

Or, if you need to pass a variable path:

$remoteScriptFilePath = 'C:\laurietest\testthis.ps1'

Invoke-Command -ComputerName myserver -ScriptBlock { & $using:remoteScriptFilePath }
  • Related