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

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
Rajeshgvd
Posts: 7
Joined: Wed Jun 20, 2018 12:19 am

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

Post by Rajeshgvd » Tue Jun 26, 2018 3:07 am

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"
}
}

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Tue Jun 26, 2018 3:12 am

You don't have to copy the script. Just execute it remotely.

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

Locked