Page 1 of 1

powershell script for copy script to remote servers and then execute the script

Posted: Tue Jun 26, 2018 3:07 am
by Rajeshgvd
Hi ,

I need some help on copying the script to remote servers and then execute the same copying script.
I know how to copy files from source to destination, but not able to execute the file script . please help me.
Below is the script.

$computers = get-content "C:\psscripts\servers.txt"

$source = "C:\test\script.ps1"

$destination = "C$\temp\"

foreach ($computer in $computers) {
if ((Test-Path -Path \\$computer\$destination))
{Copy-item $source -Destination "\\$computer\$destination" -Recurse}
else
{
"\\$computer\$destination is not reachable or does not exist"
}
}

Re: powershell script for copy script to remote servers and then execute the script

Posted: Tue Jun 26, 2018 3:12 am
by jvierra
You don't have to copy the script. Just execute it remotely.

Invoke-Command -File C:\test\script.ps1 -Computer $computer