mhopkins321 mhopkins321 - 3 months ago 31
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 = "10.0.0.3"
$fullPath = $fullPath + $release
$fullPath = $fullPath + ".bat"
start-process $fullPath


Currently I log in to a remote machine to run this code for this example 10.0.0.2. The code then pulls the files from another remote machine (10.0.0.3). What I would like to do is execute this script from my local machine (10.0.0.1) 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

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....

Comments