Displaying Objects

This topic contains 5 replies, has 3 voices, and was last updated by  Poshoholic 4 years, 3 months ago.

  • Author
    Posts
  • #6596

    veroli
    Participant

    Hello,

    I am using a WMI query to pull back fixed disk properties of remote server

    $disks = (Get-WmiObject -class win32_logicaldisk -computername $item -filter "drivetype=3")

    $item is the name of a remote machine

    i then create a new object and i want to add a member to that object that displays the drive id and the size in GB by doing the following.

    $output | Add-Member NoteProperty Hard_Disks $disks | select -property @{n='Drive';e={$_.DeviceID}}, @{n='size(GB)';e={"{0:N2}" -f $($_.Size  / 1GB)}}

    If  I try and then display this in a text box on a form instead a list of the disks and their sizes i get

    Hard_Disks=System.Object[]

    Instead of a list of the disks, if i just try and output this at the shell i get the root path and a device id instead of the disk information.

    This is a similar problem as requested in the scripting games i know, but it just happens to coincide with an inventory script i am working. I am not sure if i am refencing the object incorrectly. The type of output i am looking for is

    c:            250

    d:           10

    e:           50

    hope that makes sense any help appreciated.

    regards

    Dan

     

  • #6600

    Dave Garnar
    Participant

    I think you may be seeing something that I have had myself recently trying to output an object in to an event.  If you convert the object to a string it should display correctly in the text box.

    You can do this by piping the object in to the Out-String CMDlet.

  • #6602

    veroli
    Participant

    Thanks Dave,

     

    i tried piping it out-string but still having the same result in the text box

  • #6608

    Poshoholic
    Participant

    I would take a different approach.  Once you have the data you want in $disks, convert that into a string table with the values you care about, then strip off the header rows.  Like this:

    $tableAsString = $disks | ft @{Name='Drive';Expression={$_.DeviceId}},@{Name='Size(GB)';Expression={'{0:N2}' -f ($_.Size / 1GB)}} -AutoSize | Out-String

    $tableAsString -replace '^\s+|\s+$' -split "\r\n|\r|\n" | Select-Object -Skip 2

    The results of the second command are what you will want to assign to the text box on your form.

  • #6734

    veroli
    Participant

    Apologies for the delay this worked great, thanks poshoholic.

    My next question is :

    i have a number of WMI queries that put into a new object and then out put this to a fiel or screen. The problem i have is i want to just list the headings once, for example

    Servername, disks, memory, ipaddress

    and then under the heading list the information on a new line so if i want i can export to csv or just display the table. at the moment i do the following.

    $output = New-Object PSObject
    $output | Add-Member NoteProperty Name ($system.DNSHostName)
    $output | Add-Member NoteProperty CPU ($system.NumberOfProcessors)
    $output | Add-Member NoteProperty OS ($OS.Caption)
    $output | Add-Member NoteProperty OS_Serial_Number ($os.serialnumber)

     

    in a for loop but this then runs the above code for each server in the for loop and doesnt just list each element on one line .

    so what i want to see is

    Server           CPU        OS          OS_serial

    servera          1              2008    xxxxx

    serverb         4               2008    yyyyyy

    etc etc

     

    at the moment i get

    Server:  servera

    cpu : 1

    os: 2008

    os_serial: xxx

    Server: serverb

    cpu: 4

    os:2008

    os_serial: yyyy

     

    regards Dan

     

     

  • #6735

    Poshoholic
    Participant

    You can force your loop to output in a table like this:
    $(foreach {$a in $b) {
    # Do stuff in here
    }) | Format-Table

    If PowerShell is defaulting to list output, it is likely due to the number of columns that would appear in the output (PSObjects default to tabular output with 4 columns or less; list output otherwise).

You must be logged in to reply to this topic.