-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathRemoteServerSessionLoop2.ps1
More file actions
38 lines (30 loc) · 1.31 KB
/
Copy pathRemoteServerSessionLoop2.ps1
File metadata and controls
38 lines (30 loc) · 1.31 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
<#
.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 = New-PSSession -ComputerName $server -Name $server -Credential $Creds
Try {
Write-Information "Connecting to $server... " -InformationAction Continue
# Enter-PSSession is interactive-only; use Invoke-Command to run code in the remote session.
Invoke-Command -Session $session -ScriptBlock {
<#
Code to be run on each remote server goes here.
#>
Write-Information 'Inner code.' -InformationAction Continue
}
} Catch {
Write-Warning "Failed to connect to $server. Skipping." -WarningAction Continue
Continue
}
Write-Output $session.State
#Cleanup and then show the current PSSession state.
Remove-PSSession $session
Write-Information "$($session.ComputerName) $($session.State) `n`n" -InformationAction Continue
}