mhopkins321 mhopkins321 - 1 year ago 138
PowerShell Question

Executing a powershell script locally to run on a remote computer

I have the current powershell script

$release = read-host "enter release"
$fullPath = ""
$fullPath = $fullPath + $release
$fullPath = $fullPath + ".bat"
start-process $fullPath

Currently I log in to a remote machine to run this code for this example The code then pulls the files from another remote machine ( What I would like to do is execute this script from my local machine ( and have it run on .2. I have full access to the box so that should make it easier. How can i go about doing this?

Answer Source

I have used this once in a project. Where i needed to create website on APP server and had to run from a INT server. So I kept the site setup script on App server and ran remotely from INT server.

 $script = 
 $Filetoexe = $args[0]
 invoke-expression "$Filetoexe"
 Invoke-Command -ComputerName <Computername> -credential <credentials> -ScriptBlock $script -Args "D:\sitesetup.ps1"

save this and run this. The last line "Invoke-Command" will get executed and it will access the remote computer that you have given and will execute the script block .

The script block takes the argument passed in the last line and executes the file in the script block.

What ever function you need to run on the remote system must be done with in the script block.

Hope it is helpful....

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download