0
votes

We have a powershell script which is on our server. Trying to figure out how to use Invoke-Command with the -ComputerName to run it remotely, but when I run:

Invoke-Command - ComputerName XXXX -FilePath YYYY

It appears that Invoke-Command is looking for a local file. How do I specify that I want to remotely run a script which is only available on the remote machine?

1
Do you mean if it's on a network share? if so try -Authenication Credssp when you PSSession. This allows for a double hopIsaac

1 Answers

1
votes

You can use the -ScriptBlock parameter along with dot sourcing.

Invoke-Command -ComputerName machine1 -Scriptblock {
    . "C:\path\to\file.ps1"
}

Edit: adding a version that uses creds

# Will bring up a dialogue box asking for credentials
$creds = get-credential
Invoke-Command -ComputerName machine1 -Credential $creds -Scriptblock {
    . "C:\path\to\file.ps1"
}