46 lines
1.4 KiB
PowerShell
46 lines
1.4 KiB
PowerShell
|
function Select-AvailableWinRmHosts {
|
|||
|
<#
|
|||
|
.SYNOPSIS
|
|||
|
Returns a filtered list of servers that are responding to WinRM requests.
|
|||
|
|
|||
|
.PARAMETER ComputerName
|
|||
|
List of servers to test WinRM connections against.
|
|||
|
|
|||
|
.PARAMETER ReturnBadServers
|
|||
|
Returns the list of servers that could not be connected to.
|
|||
|
#>
|
|||
|
[CmdletBinding()]
|
|||
|
param(
|
|||
|
[string[]]$ComputerName,
|
|||
|
[switch]$ReturnBadServers
|
|||
|
)
|
|||
|
|
|||
|
if(Test-IsCollectionNullOrEmpty $ComputerName) {
|
|||
|
return $null
|
|||
|
}
|
|||
|
|
|||
|
$results = Invoke-Parallel -objects $ComputerName -returnObjects -numThreads 32 -script {
|
|||
|
param($server)
|
|||
|
|
|||
|
# Invoke a simple script block to verify that WinRM is listening.
|
|||
|
try {
|
|||
|
$success = (1 -eq (Invoke-Command -ComputerName $server -ErrorAction Stop -ScriptBlock { return 1 }))
|
|||
|
if($success) {
|
|||
|
return $server
|
|||
|
} else {
|
|||
|
return $null
|
|||
|
}
|
|||
|
} catch {
|
|||
|
Write-Warning "Select-AvailableWinRMHosts: Could not connect to [$server]`n$_"
|
|||
|
return $null
|
|||
|
}
|
|||
|
}
|
|||
|
$results = ($results | Where-Object { $null -ne $_ })
|
|||
|
|
|||
|
# Produce a list of the unavailable servers from the available servers.
|
|||
|
if($ReturnBadServers.IsPresent) {
|
|||
|
$results = $ComputerName | Where-Object { $results -notcontains $_ }
|
|||
|
}
|
|||
|
|
|||
|
return $results
|
|||
|
}
|