Ok, a couple things. The script looks fairly good really, all things considered, and a couple little tweaks and it should work fine. Let's approach this a tad different and just construct the object all at once instead of making it then adding noteproperties.
Also, we'll just make 1 call to GWMI (short for Get-WMIObject). I think this will work better for you. First off, making the GWMI call once means we have to select more than one thing when we make it. Since you are really making the exact same call twice, and selecting two different things from it each time, this just kind of makes sense. We're going to skip defining things in a select, and just let $pcName
store all the data from the GWMI call. Though, first we're going to make sure that there's no old data in $pcName
just to make sure we don't get duplicates.
clear-host
$item = new-object PSObject
$list = @()
$c=Get-Credential
$a = gc "c:\Scripts\pcList.txt"
ForEach ($b in $a)
{
if(Get-Variable pcName -ea SilentlyContinue){Remove-Variable pcName}
$pcName = Get-WmiObject win32_service -filter 'name="wuauserv"' -computer $b -credential $c -ErrorAction SilentlyContinue
Ok, now we create the object, and we'll just add it directly to the array. Here's where we define things like Name and StartupType as object properties, so when you display the array later it shows those two properties for each item. After that we'll close the ForEach loop and both display the list to the screen and pipe it to Export-CSV to save the results.
$list += New-Object PSObject -Property @{
Name = $pcName.SystemName
StartupType = $pcName.StartMode
}
}
$list
$list | Export-Csv "C:\Scripts\pcList.CSV" -notype
So, this doesn't check to make sure that the systems are online, or do any error checking like that, but if that's OK with you then I think this should do exactly what you need.