Is there a way to run scripts that are found on other computers with powershell?

75 views Asked by At

I've just learned how to run local scripts in other computers using Invoke-Command and the -ComputerName argument . I was just wondering if there is a way to do the opposite, that is, running locally a script found on another computer.

2

There are 2 answers

0
David Trevor On

Yup, just call the script via UNC path. You must have read permissions on the remote file system of course

& "\\HOSTNAME\c$\INSTALL\remote.ps1"
0
Darin On

Using a UNC path as described in David Trevor's answer is probably the simplest answer to this question, but it relies on network shares and permissions, and gives no regard to where on the local computer you want it to run.

While unlikely, I believe there could be situations where you can invoke scripts on a remote computer, but can't gain access to files through a UNC path. Also, you may need the execute the remote script as if it was located in a certain folder on the local computer. In either situation, the following function may provide a better answer:

function Invoke-Locally {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true, Position = 0)]
        [string]$ComputerName,
        [Parameter(Mandatory = $true, Position = 1)]
        [string]$RemoteFile,
        [Parameter(Mandatory = $false, Position = 2)]
        [string]$LocalPath
    )
    if (-not $PSBoundParameters.ContainsKey('LocalPath')) { $LocalPath = $RemoteFile }
    $RemoteScript = Invoke-Command -ComputerName $ComputerName -ArgumentList $RemoteFile -ScriptBlock {
        param (
            [Parameter(Mandatory = $true, Position = 0)]
            [string]$RemoteFile
        )
        Get-Content $RemoteFile -Raw
    }
    . ([System.Management.Automation.Language.Parser]::ParseInput($RemoteScript, $LocalPath, [ref]$null, [ref]$null)).GetScriptBlock()
}

This function calls a script block on the remote computer, the script block reads and returns the remote computer's file's content, and then saves its value in $RemoteScript. Then a trick provided in Mathias R. Jessen's answer is used to execute the script stored in $RemoteScript as if it was in the file path provided by $LocalPath.

The function would be called as follows:

$CN = 'PL001005SEC'; $RF = 'C:\Users\Public\Test.ps1'; $LP = 'C:\Users\Public\'
Invoke-Locally -ComputerName $CN -RemoteFile $RF -LocalPath $LP

Where -ComputerName is the name of the remote computer.
Where -RemoteFile is the file on the remote computer.
Where -LocalPath is the optional local path of execution.
NOTE:

  1. For -LocalPath, the last \ is important!.
  2. -LocalPath is optional, and will default to the value of -RemoteFile, resulting in executing the script as if it was located on the local computer in the same path as it was found on the remote computer.

In the above example call, if Test.ps1 on the remote computer contains this code:

"[$PSScriptRoot]"
1..3 | Foreach-Object {
    "[$_]"
}

It produces this output:

[C:\Users\Public]
[1]
[2]
[3]