Stan Di
Stan Di

Reputation: 83

How to run PowerShell script on a remote computer?

I'm aiming to run the script for Google Chrome installation on a remote computer. The script for local installation has been saved on my PC (\localhost\c$\Chrome\Chrome Installer.ps1").

I was thinking to copy the script first to the remote machine and then run the script remotely. Could you please suggest the best way to run that script remotely? Thank you in advance

Best Regards, Stan

You can find the script below:

$Installer = "$env:temp\chrome_installer.exe"
$url = 'http://dl.google.com/chrome/install/375.126/chrome_installer.exe'
Invoke-WebRequest -Uri $url -OutFile $Installer -UseBasicParsing
Start-Process -FilePath $Installer -Args '/silent /install' -Wait
Remove-Item -Path $Installer
$login = Read-Host "Please enter login id"
$comp = Read-Host "Please enter computer name or IPV4 adress"
Copy-Item -Path "\\localhost\c$\Chrome\Chrome Installer.ps1"  -Dest "\\$($comp)\c$\temp"

Upvotes: 0

Views: 2395

Answers (2)

smorris
smorris

Reputation: 16

You can do enter-pssession to get a local prompt and paste in your script using your credentials. You can also checkout invoke-commdand and specify a script file and tagret.

Run: update-help

then run:

help invoke-command -examples

You should see something that works for your situation.

Upvotes: 0

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174435

I was thinking to copy the script first to the remote machine and then run the script remotely.

You don't actually need to copy it to the remote host first - you can have Invoke-Command run a script from your local file system on a remote computer like so:

Invoke-Command -ComputerName $comp -FilePath "C:\Chrome\Chrome Installer.ps1"

To pass credentials for the remote machine, specify the account name as an argument to the -Credential parameter and PowerShell will prompt you for the password:

$login = Read-Host "Enter the user name for the remote host"
Invoke-Command -ComputerName $comp -FilePath "C:\Chrome\Chrome Installer.ps1" -Credential .\$login

Upvotes: 2

Related Questions