I want to test whether or not winrm is running on a list of servers.
winrm id -r:servername works for individual systems but I want to test recursively for a list from a csv or text file.
With output to a file saying "working" or "not working" for each.
How do I do this?
Thanks all.
Edit:
Gotten to a point where I am passing a list of vm's and piping along until I get successful winrm connections output to a file and failures shown in the console.
get-vm |where {$.powerstate -like "PoweredOn"}|get-vmguest |where {$.guestfamily -like "windowsGuest"}|foreach {winrm id -r:$_.hostname} |Out-File c:\scripts\winrmtest.txt
In my out-file I am getting output like IdentifyResponse ProtocolVersion = http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd ProductVendor = Microsoft Corporation ProductVersion = OS: 6.1.7601 SP: 1.0 Stack: 2.0
for successful connections and on the console I get the following for failures:
Error number: -2144108526 0x80338012 The client cannot connect to the destination specified in the request. Verify that the service on the destination is run ning and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destinat ion, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination t o analyze and configure the WinRM service: "winrm quickconfig". WSManFault Message = The client cannot connect to the destination specified in the request. Verify that the service on the dest ination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running o n the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig".
Need to get all the output into the file, along with the name of the guest vm the response is for.
Please keep in mind I have not used the VM cmdlets, however below is some code I think should help you. I added a wmi to check the winrm service on each machine, if the service isn't running, look at the startservice()
method for the win32_service
. if this is unfamiliar to you, pipe | gm
to see available properties and methods.
However here are a few recommendations:
Exporting and managing data is easy and clean.
"-Filter" vs "Where{}"
Look into the VM cmdlets and see if they support -filter {property -operator "*filterby*"}
your code will run much faster.
$
All_VMS_Status = @()
get-vm | where {$.powerstate -like "PoweredOn"} | get-vmguest | where {$.guestfamily -like "windowsGuest"} | foreach {
<# Create PowerShell Object with Hostname #>
$psobject = New-Object -TypeName psobject
$psobject | Add-Member -MemberType NoteProperty -Name "VM-HostName" -Value $($_.HostName)
<# Determin if WINRM is working #>
if(winrm id -r:$_.hostname) {
$Connection_Status = "Success"
} Else {
$Connection_Status = "Failed"
}
<# Check winrm service on remorte PC #>
$remote_winrm_Service = Get-WmiObject win32_service -ComputerName $($_.hostname) | Where{ $_.Name -eq "winrm"}
<# Add all information to PS object for exporting #>
$psobject | Add-Member -MemberType NoteProperty -Name "Winrm-Connection" -Value $Connection_Status
$psobject | Add-Member -MemberType NoteProperty -Name "winrm-state" -Value $($remote_winrm_Service.State)
$psobject | Add-Member -MemberType NoteProperty -Name "winrm-startmode" -Value $($remote_winrm_Service.StartMode)
$psobject | Add-Member -MemberType NoteProperty -Name "winrm-ExitCode" -Value $($remote_winrm_Service.ExitCode)
$psobject | Add-Member -MemberType NoteProperty -Name "winrm-Status" -Value $($remote_winrm_Service.Status)
$All_VMS_Status += $psobject
}
<# Export to csv #>
$All_VMS_Status | Export-Csv -Path "c:\scripts\winrmtest.csv" -NoTypeInformation`