Multiple WMI queries into has table

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 6 months ago.

  • Author
    Posts
  • #5210

    by habs3 at 2012-10-18 10:13:55

    Hi,

    am trying to query wmi for a few different properties and stuff them into a hash table to send to csv. Here is what i have so far but do not get any data in the has table.

    $itm = read-host "enter server to query"
    #$itm = get-content c:\servers.txt
    $a = {foreach ($srv in $itm){
    $CompSys = Get-WmiObject Win32_ComputerSystem -computer $srv
    $Proc = get-wmiobject Win32_Processor -computer $Srv
    $OS = Get-WmiObject Win32_OperatingSystem -computer $srv
    #$LDisk = get-wmiobject win32_logicaldisk -filter "drivetype=3" -computer $srv | foreach-object { $_.deviceid; ($_.freespace/1GB).tostring("0.00"); ($_.size/1GB).tostring("0.00") }
    }} Select-object @{Label="Server Name"; E={$Srv}},
    @{Label="MFG"; E={$CompSys.Name}}

    i will add to the hash table to get many more properties.

    any help would be great

    by Jason_Yoder_MCT at 2012-10-18 11:38:00

    Hey Hab3,

    Going through your posted code, you have a few extra closing curly braces "}" so I'm just guessing at where you are in the code. One thing that I have noticed since your desired output is to a CSV, we need to make sure that each logical disk has its properties listing in the PowerShell pipeline. We could send the entire contents to the pipeline, but in a CSV you will see your data listed as System.Object[] if you opened it in Excel. If your intent is to view this in Excel, that would be bad. If I'm heading in the wrong direction, please let me know and I'll see what I can do to help.

    I rewrote your code. I hope I did not cannibalized it to the point that it is not useful to you. I left it as the function that I used to test, but you can copy and paste out what you need.

    Jason

    Function Get-Details
    {
    Param (
    $ComputerName = "LocalHost"
    )

    ForEach ($Srv in $ComputerName)
    {
    $CompSys = Get-WmiObject Win32_ComputerSystem -computerName $Srv
    $Proc = get-wmiobject Win32_Processor -ComputerName $Srv
    $OS = Get-WmiObject Win32_OperatingSystem -computerName $Srv

    $LDisk = Get-WmiObject win32_LogicalDisk -Filter "DriveType = 3" -ComputerName $Srv

    # Create a blank object.
    $Obj = New-Object -TypeName PSObject

    # Add the objects properties.
    $Obj | Add-Member -MemberType NoteProperty `
    -Name "Server_Name" `
    -Value $Srv

    $Obj | Add-Member -MemberType NoteProperty `
    -Name "MFG" `
    -Value $CompSys.Name
    # Continue to fill in your property data here

    # The $Index will prevent the creation of Property names of the
    # same type. Since the designed output is to a CSV, we need
    # to not save an object inside of an object or the CSV will not
    # have the designed data.

    $Index = 0

    # Create a property for each property of the logical disk that
    # needs to be in the output. Take note of how the $Index value
    # is used in the -Name parameter.
    ForEach ($Disk in $LDisk)
    {
    $Obj | Add-Member -MemberType NoteProperty `
    -Name "DeviceID_$Index" `
    -Value $Disk.DeviceID

    $Obj | Add-Member -MemberType NoteProperty `
    -Name "FreeSpace(GB)_$Index" `
    -Value ($Disk.FreeSpace/1GB).ToString("0.00")

    $Index++
    }

    # Write the object to the pipeline.
    Write-Output $Obj

    }

    }

    Get-Details | Export-csv c:\users\jason\documents\temp\test1.csv

You must be logged in to reply to this topic.