ps/Modules/Alkami.DevOps.Common/Public/Select-AvailableWinRmHosts.ps1

46 lines
1.4 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
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
}