Link to home
Start Free TrialLog in
Avatar of Jason Crawford
Jason CrawfordFlag for United States of America

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:

$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

Open in new window


It worked except the results overwrote each other and the resulting .csv file only contained one server.  Any ideas?
SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
Avatar of Jason Crawford

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.
All the heavy hitters in one thread, thanks again to everyone who contributed.