0

I am trying to figure out a way of gathering 5 pieces of info using WMI and place that information into an array and then export it to a csv file. Any help would be greatly appreciated. Here is the code :

$Servers = get-content "c:\temp\HH_Servers.txt"
foreach ($Server in $Servers){
    $GenItems2 = gwmi Win32_OperatingSystem -Comp $Server
    $Software = gwmi Win32_Product -Comp $Server
    $systeminfo = foreach ($objItem in $GenItems2){
    $objItem.Caption
}

#Populate Software Sheet
$softwareinfo = foreach ($objItem in $Software){
    $Server
    $objItem.Name
    $objItem.Vendor
    $objItem.Version
    }
}
1
  • Please don't use the Code Snippet button when adding your code; it's for JavaScript (I know the hint doesn't say so). Instead, use the Code Sample button (two to the left on the toolbar - it looks like {}) or simply select all your code and press Ctrl+K. I've fixed the formatting here (as I did with your previous question). :-) Commented May 22, 2015 at 23:46

1 Answer 1

1

This should get the job done if i interpreted the intention of your code correctly:

$Servers = Get-Content "c:\temp\HH_Servers.txt"

$arr = @()

foreach ($Server in $Servers){

    $GenItems2 = gwmi Win32_OperatingSystem -Comp $Server
    $Software = gwmi Win32_Product -Comp $Server

    #Populate Software Sheet
    foreach ($objItem in $Software){
        $temp = New-Object psobject
        $temp | Add-Member -MemberType NoteProperty -Name "Server" -Value $objItem.__SERVER
        $temp | Add-Member -MemberType NoteProperty -Name "Caption" -Value $GenItems2.Caption
        $temp | Add-Member -MemberType NoteProperty -Name "ServicePack" -Value $GenItems2.CsdVersion
        $temp | Add-Member -MemberType NoteProperty -Name "Name" -Value $objItem.Name
        $temp | Add-Member -MemberType NoteProperty -Name "Vendor" -Value $objItem.Vendor
        $temp | Add-Member -MemberType NoteProperty -Name "Version" -Value $objItem.Version
        $arr += $temp
    }

}

$arr | Export-Csv C:\whatever\SoftwareInfo.csv -NoTypeInformation -Encoding ASCII

I have also allowed myself to add an additional property which includes the service pack level of the operating system (might be of interest as well). If you dont need it just remove the line that adds the ServicePack property.

In case performance matters you might also reconsider using wmi completely since its super slow. If you want to learn about alternatives here is a good article from the Scripting guy that discusses the matter: Use PowerShell to Find Installed Software

Sign up to request clarification or add additional context in comments.

2 Comments

Paul, thanks for your time! It works perfectly and I am looking at the link you sent me as well.
I would suggest using $arr += New-Object -TypeName psobject -Property @{ Caption = $GenItems2.Caption; ServicePack = $GenItems2.CsdVersion } so you could get rid of 6 Add-Member calls per software.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.