Question

The script reads in a .TXT file of computer names. It then checks to see if Windows Update is set to Auto or Manual; the computers name and update type are then exported to a CSV. At first, it was only exporting the final computer and it's type. I did some research and saw that creating an object to hold the name and type and then adding that object to an array would work. However, exporting the array to a CSV results in interesting data which is related to the array instead of the computer name and type. On top of that, I have the array printed out in the console pre-export and it looks to be the last computer and its type listed 9 times, the amount of computers on the one .TXT file I'm reading in. I know export-CSV now has an append parameter that could take care of this but we have much more PS 1.0 and 2.0 in the office and need it to work on those version.

clear-host

$item = new-object PSObject
$list = @()
$c=Get-Credential
$a = gc "c:\Scripts\pcList.txt"

ForEach ($b in $a) 
{
    $pcName = Get-WmiObject win32_service -filter 'name="wuauserv"' -computer $b -credential $c -ErrorAction SilentlyContinue |
select-object name,
@{N="System Name";E={$_.Systemname}}
    $name = $pcName.'System Name'
    $item | Add-Member NoteProperty Name $name -Force

    $pcType = Get-WmiObject win32_service -filter 'name="wuauserv"' -computer $b -            credential $c -ErrorAction SilentlyContinue |
select-object name, @{N="Startup Type";E={$_.StartMode}}
    $type = $pcType.'Startup Type'
    $item | Add-Member NoteProperty StartupType $type -Force    

    $list += $item
}
Write-Host $list

export-csv -Path "c:\Scripts\pcListCSV" -inputobject $list -notype
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top