Here is another small script snippet I used in one of my project to quickly check status of WinRM setup on a list of servers I was managing. This was part of a bigger project work, and this data was important for my project on whether WinRM is setup as per requirements or not.
You need to provide a file path to a list of system names on which this script should be executed as parameter. Once executed, it will generate a csv file with WinRM status if the systems provided can be reachable from the system from which you run this script. Share your comments if it helps or if you face any issue.
Param(
[Parameter(Mandatory=$true)]
[String]$serverList
)
#Provide a text file path containing a list of server FQDN or IP Addresses which will be checked.
if(!(Test-Path $serverList)){
Write-Host "ERROR: Path not found.Check the path and try again!" -ForegroundColor Red
}
else{
$currentDir = $(Get-Location).Path
$oFile = "$($currentDir)\Server_WinRM_State.csv"
if(Test-Path $oFile){
Remove-Item $oFile -Force
}
if(Test-Path $oFile){Remove-Item $oFile -Force}
"ServerName,WinRM Configured?,WinRM HTTP Port,WinRM HTTPS Port" | Out-File $oFile -Append -Encoding ASCII
ForEach($srv in Get-Content $serverList){
$winrmConfig = $winrmHTTP = $winrmHTTPS = ""
# Checks if the System is responding to Ping and can be connected.
Try{
if(Test-Connection $srv -Count 2){
# To Check WINRM configured or not:
$winrmConfig = [system.convert]::ToBoolean(((winrm get winrm/config/winrs -r:$srv | Where-Object{$_ -imatch "AllowRemoteShellAccess"}).split("="))[1].trim())
# To Check the Default HTTP listener port on a remote machine:
$winrmHTTP = [System.Convert]:: ToInt32(((winrm get winrm/config/Service/DefaultPorts -r:$srv | Where-Object{$_ -imatch "HTTP = " }).split("="))[1].trim())
# To Check the Default HTTPS listener port on a remote machine:
$winrmHTTPS = [System.Convert]:: ToInt32(((winrm get winrm/config/Service/DefaultPorts -r:$srv | Where-Object{$_ -imatch "HTTPS = " }).split("="))[1].trim())
}
"$srv,$winrmConfig,$winrmHTTP,$winrmHTTPS" | Out-File $oFile -Append -Encoding ASCII
}
catch{
Write-Error -Message $_.Exception
}
}
}
Download above script and save it to your local drive and create a text file with list of server FQDN or IP address to get the status of WinRM. After that, run the script as shown below.
Verify the csv file generated as output of the execution.