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

Ask your 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.
This topic is 5 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Rajeshgvd
Posts: 7
Last visit: Fri Jun 29, 2018 1:53 am

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

Post 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"
}
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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

Invoke-Command -File C:\test\script.ps1 -Computer $computer
This topic is 5 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked