Jason Crawford
asked on
PowerShell script appending to PSObject
I need to identify which servers in my environment have WSMan enabled. When querying the servers manually they break down into three groups:
1. WSMan is enabled and the server is pingable
2. WSMan not enabled and the server is pingable
3. The server is not pingable
I played around with outputting the results into three separate files, but that seemed stupid so I tried this thinking I could append to the PSObject:
It worked except the results overwrote each other and the resulting .csv file only contained one server. Any ideas?
1. WSMan is enabled and the server is pingable
2. WSMan not enabled and the server is pingable
3. The server is not pingable
I played around with outputting the results into three separate files, but that seemed stupid so I tried this thinking I could append to the PSObject:
$results = @()
foreach ($server in (Get-Content .\servers.txt)) {
if (Test-Connection $server -Count 1 -Quiet) {
if (Test-WSMan -ComputerName $server) {
$results += New-Object -TypeName PSObject -Property @{
Name = $server
Result= 'Success'
}
}
else {
$results += New-Object -TypeName PSObject -Property @{
Name = $server
Result = 'NoWSMan'
}
}
}
else {
$results += New-Object -TypeName PSObject -Property {
Name = $server
Result = 'NotPingable'
}
}
}
$results | Select-Object name,result | Export-Csv "$env:USERPROFILE\Desktop\results.csv" -NoTypeInformation
It worked except the results overwrote each other and the resulting .csv file only contained one server. Any ideas?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Though I would write it more like oBdA did (with only minimal changes), your code should work as footech stated. Are you certain you posted the correct code?
ASKER
Well I forgot to factor in the fact that I'm retarded and forgot the @ so thank you footech. Odba and David, you scripts are much more intelligent than mine...I still have a long way to go. Thanks, fellas.
ASKER
All the heavy hitters in one thread, thanks again to everyone who contributed.