-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExecuteRemoteScript.ps1
More file actions
68 lines (51 loc) · 2.13 KB
/
Copy pathExecuteRemoteScript.ps1
File metadata and controls
68 lines (51 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<#
.SYNOPSIS
Executes a specified PowerShell script on a list of remote servers.
GitHub Repository: https://github.com/roalhelm/
.DESCRIPTION
This script reads a list of server names from a file and executes a specified
PowerShell script on each server. The script uses the provided credentials to
establish a remote connection to each server in the list and runs the script
in the specified path on each remote server.
For each server, it outputs whether the execution was successful or if there
was an error. This is useful for performing batch operations across multiple
servers from a central location.
.PARAMETER serverListPath
Path to the file containing the list of server names.
.PARAMETER remoteScriptPath
Path to the PowerShell script to be executed remotely on each server.
.NOTES
Script requires the necessary permissions to connect to the remote servers.
Each server in the server list must allow PowerShell Remoting.
.AUTHOR
Script created by Ronny Alhelm
Date: 05.11.2024
.EXAMPLE
.\ExecuteRemoteScript.ps1
This command will execute the script specified in $remoteScriptPath on all
servers listed in $serverListPath.
#>
# Main Script: ExecuteRemoteScript.ps1
# Path to the server list
$serverListPath = ".\clientList.txt"
# Path to the script that should be executed on the remote servers
$remoteScriptPath = ".\Script.ps1"
# Load script content
$scriptContent = Get-Content -Path $remoteScriptPath -Raw
# Load server list
$servers = Get-Content -Path $serverListPath
# Prompt for credentials
$credential = Get-Credential
foreach ($server in $servers) {
try {
Write-Host "Connecting to $server..."
# Execute remote script
Invoke-Command -ComputerName $server -ScriptBlock {
param ($script)
Invoke-Expression $script
} -ArgumentList $scriptContent -Credential $credential
Write-Host "Script executed successfully on $server.`n"
} catch {
Write-Host "Error executing script on $server $_`n" -ForegroundColor Red
}
}