Hello!
I've a script that reads service state using WMI, before that, the script tests if the destination workstation is alive with Test-Connection. When it fails it return it's offline in a proper way to export to csv later.
#Get credential
$cre1 = Get-Credential "Enter Credential"
$hostname = Read-Host "Enter Machine name"
#ICMP test 4 packets
If(Test-Connection -ComputerName $hostname -count 4 -Quiet ){
#Read service state
Get-WmiObject -Class Win32_Service -Filter "name='service1' or name='service2'" -ComputerName $hostname -Credential $cre1
Select Name, State, SystemName | Sort SystemName,name | Format-Table -AutoSize
}
#If ICMP fails
Else{New-Object PSObject -Property @{Name="N/A";State="OFFLINE";SystemName=$hostname}
}
The thing is, when it fails it's returning the columns in the wrong order (Name,State,SystemName): Name, SystemName, State.
How can I fix it?
EDITED:
When I do this inside a foreach and the select is outside the loop, at the end I export it to csv and it works just fine. The problem is only when I show it on the screen.
- Edited by H Souza Friday, August 21, 2015 4:50 AM


