-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTest-PSRemoting.ps1
196 lines (188 loc) · 7.17 KB
/
Test-PSRemoting.ps1
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<#
.SYNOPSIS
Check remote target computers for PowerShell remoting capabilities.
Uses runspaces for concurrency.
Copyright (C) 2015, Svendsen Tech
All rights reserved.
Author: Joakim Borger Svendsen
.EXAMPLE
.\Test-PSRemoting.ps1 -ComputerName $Servers -Credential $mycred
.EXAMPLE
.\Test-PSRemoting.ps1 -ComputerName $Servers | Format-Table -AutoSize
.EXAMPLE
$Results = .\Test-PSRemoting.ps1 -ComputerName $Servers
.LINK
http://www.powershelladmin.com/wiki/Test_PowerShell_Remoting_Cmdlet
#>
[CmdletBinding()]
param(
# Target computer names.
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Mandatory=$true)][Alias('Cn', 'Hostname', 'PSComputerName')]
[string[]] $ComputerName,
# Prompt for credentials to be used when connecting.
[switch] $PromptForCredentials,
# Supply PSCredentials object to be used when connecting.
[System.Management.Automation.Credential()] $Credential = [System.Management.Automation.PSCredential]::Empty,
# Number of simultaneously running threads.
[int] $ThrottleLimit = 32,
# Do not display progress using Write-Progress.
[switch] $HideProgress,
# Timeout in seconds.
[int] $Timeout = 30,
# Don't display end summary showing start and end time using Write-Host.
[switch] $Quiet
)
begin {
$MyEAP = 'Stop'
$ErrorActionPreference = $MyEAP
$StartTime = Get-Date
if ($PromptForCredentials) {
$Credential = Get-Credential
}
$RunspaceTimers = [HashTable]::Synchronized(@{})
$Runspaces = New-Object -TypeName System.Collections.ArrayList
$RunspaceCounter = 0
Write-Verbose -Message 'Creating initial session state.'
$ISS = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
$ISS.Variables.Add((New-Object -TypeName System.Management.Automation.Runspaces.SessionStateVariableEntry -ArgumentList 'RunspaceTimers', (Get-Variable -Name 'RunspaceTimers' -ValueOnly), ''))
Write-Verbose -Message 'Creating runspace pool.'
$RunspacePool = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspacePool(1, $ThrottleLimit, $ISS, $Host)
$RunspacePool.ApartmentState = 'STA'
$RunspacePool.Open()
# This is the script block that is run for each computer.
$ScriptBlock = {
[CmdletBinding()]
param(
[string] $Computer,
[int] $ID,
$Credential
)
# Get the start time.
$RunspaceTimers.$ID = Get-Date
# The objects returned here are passed to the host...
if (-not (Test-Connection $Computer -Count 1 -Quiet)) {
New-Object psobject -Property @{
ComputerName = $Computer
Success = $null
Error = 'No ping reply'
}
continue
}
$IcmHash = @{
ErrorAction = 'Stop'
ComputerName = $Computer
ScriptBlock = { 'It works' }
}
if ($Credential.Username -ne $null) {
$IcmHash.Credential = $Credential
}
try {
$Result = Invoke-Command @IcmHash
}
catch {
New-Object psobject -Property @{
ComputerName = $Computer
Success = $null
Error = $_
}
continue
}
# Check if results are as expected.
if ($Result -ne 'It works') {
New-Object psobject -Property @{
ComputerName = $Computer
Success = $null
Error = 'Unknown error'
}
continue
}
# Everything went well, return success object.
New-Object psobject -Property @{
ComputerName = $Computer
Success = $true
Error = $null
}
} # end of script block
function Get-PSRemotingResult {
[CmdletBinding()]
param( [switch]$Wait )
do
{
$More = $false
foreach ($Runspace in $Runspaces) {
$StartTime = $RunspaceTimers[$Runspace.ID]
if ($Runspace.Handle.IsCompleted)
{
Write-Verbose -Message ('Thread done for {0}' -f $Runspace.IObject)
$Runspace.PowerShell.EndInvoke($Runspace.Handle)
$Runspace.PowerShell.Dispose()
$Runspace.PowerShell = $null
$Runspace.Handle = $null
}
elseif ($Runspace.Handle -ne $null) {
$More = $true
}
if ($Timeout -and $StartTime) {
if ((New-TimeSpan -Start $StartTime).TotalSeconds -ge $Timeout -and $Runspace.PowerShell) {
Write-Warning -Message ('Timeout {0}' -f $Runspace.IObject)
$Runspace.PowerShell.Dispose()
$Runspace.PowerShell = $null
$Runspace.Handle = $null
}
}
}
if ($More -and $PSBoundParameters['Wait']) {
Start-Sleep -Milliseconds 100
}
foreach ($Thread in $Runspaces.Clone()) {
if (-not $Thread.Handle) {
Write-Verbose -Message ('Removing {0} from runspaces' -f $Thread.IObject)
$Runspaces.Remove($Thread)
}
}
if (-not $HideProgress) {
$ProgressSplatting = @{
Activity = 'Testing PSRemoting capabilities'
Status = 'Processing: {0} of {1} total threads done' -f ($RunspaceCounter - $Runspaces.Count), $RunspaceCounter
PercentComplete = ($RunspaceCounter - $Runspaces.Count) / $RunspaceCounter * 100
}
Write-Progress @ProgressSplatting
}
}
while ($More -and $PSBoundParameters['Wait'])
} # end of Get-PSRemotingResult
} # end of begin block
process {
foreach ($Computer in $ComputerName) {
$RunspaceCounter++
$psCMD = [System.Management.Automation.PowerShell]::Create().AddScript($ScriptBlock)
[void] $psCMD.AddParameter('ID', $RunspaceCounter)
[void] $psCMD.AddParameter('Computer', $Computer)
[void] $psCMD.AddParameter('Credential', $Credential)
[void] $psCMD.AddParameter('Verbose', $VerbosePreference)
$psCMD.RunspacePool = $RunspacePool
Write-Verbose -Message "Testing PSRemoting capabilities: Checking $Computer"
[void]$Runspaces.Add(@{
Handle = $psCMD.BeginInvoke()
PowerShell = $psCMD
IObject = $Computer
ID = $RunspaceCounter
})
Get-PSRemotingResult
}
}
end {
Get-PSRemotingResult -Wait
if (-not $HideProgress) {
Write-Progress -Activity 'Testing PSRemoting capabilities' -Status 'Done' -Completed
}
Write-Verbose -Message 'Closing runspace pool.'
$RunspacePool.Close()
$RunspacePool.Dispose()
if (-not $Quiet) {
Write-Host -ForegroundColor Green ('Start time: ' + $StartTime)
Write-Host -ForegroundColor Green ('End time: ' + (Get-Date))
}
}