-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathRemoteServerSessionLoop2.ps1
More file actions
40 lines (33 loc) · 1.46 KB
/
Copy pathRemoteServerSessionLoop2.ps1
File metadata and controls
40 lines (33 loc) · 1.46 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
<#
.SYNOPSIS
.DESCRIPTION
.NOTES
#>
#Update this list of servers or replace the statically generated array with a Get-ExchangeServer or Get-ADDomainController type of cmdlet.
$servers = @('', '', '')
$Creds = Get-Credential
if ($session) { Remove-PSSession $session }
#Loop through each server in the list, open a PowerShell remoting session, then show the name and status of the session. Skip (continue) to the next server if a connection fails.
foreach ($server in $servers) {
$session = $null
Try {
Write-Information "Connecting to $server... " -InformationAction Continue
$session = New-PSSession -ComputerName $server -Name $server -Credential $Creds -ErrorAction Stop
# Enter-PSSession is interactive-only; use Invoke-Command to run code in the remote session.
Invoke-Command -Session $session -ErrorAction Stop -ScriptBlock {
<#
Code to be run on each remote server goes here.
#>
Write-Information 'Inner code.' -InformationAction Continue
}
} Catch {
Write-Warning "Failed to create or use the PSSession for $server. Skipping." -WarningAction Continue
Continue
} Finally {
if ($session) {
# Cleanup and then show the current PSSession state.
Remove-PSSession $session -ErrorAction SilentlyContinue
Write-Information "$($session.ComputerName) $($session.State) `n`n" -InformationAction Continue
}
}
}