header1   header
header
header : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
IMPORTANT: PowerShellCommunity.org is moving! - Wednesday, August 15, 2012

PowerShellCommunity.org is moving!  This community software, and the hardware that it sits on, are no longer serving the purposes of this community.  As a result, we have decided to move this community to a new home at PowerShell.org.  PowerShell.org is already up and running with the new community software and in its new location, so please post any new questions that you have on the forums over there instead of posting them on this site.  We've already started getting some great questions from members of the community over there so please, come on over and join us!

While we are going through this transition, this site will remain up for the short term.  New posts may no longer be created on these forums, however replies to existing posts are allowed so that users who posted questions don't have to re-post the same question on the new site.

[UPDATE 28/02/2013] New user registration has been disabled and forums have now been switched to read-only, including for existing posts since all threads that were started should now be completed. If you have a question about content on this site or about PowerShell in general, head over to PowerShell.org and ask it there where there are people actively using the site and answering questions.

If you have any questions, please let us know on the PowerShell.org site.

Thank you,

Kirk "Poshoholic" Munro

 
Hardware Inventory; same values displayed on one line
Last Post 15 Aug 2012 06:52 PM by RiffyRiot. 3 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
omega.redUser is Offline
New Member
New Member
Posts:8
Avatar

--
15 Aug 2012 08:52 AM
    $computerlist = Get-Content .\computer_list.txt
    $computername = @{name='ComputerName';expression={$_.__SERVER}}
    $BIOSSerialNumber = @{name='BIOSSerialNumber';expression={Get-WmiObject -class Win32_BIOS -computername $computerlist |
    Select-Object -ExpandProperty SerialNumber}}
    $ServicePack = @{name='Service Pack';expression={$_.csdversion}}
    $OperatingSystem = @{name='Operating System';expression={$_.caption}}

    Get-WmiObject -class Win32_OperatingSystem -ComputerName $computerlist |
    select -property $computername,$OperatingSystem,$ServicePack,$BIOSSerialNumber


    (This is what I get)
    ComputerName   Operating System         Service Pack                     BIOSSerialNumber
     ------------ ---------------- ------------        ----------------
    001 Microsoft Windows 7 Enterprise       Service Pack 1                 {?????H1, ?????D1}
     017 Microsoft® Windows Vista™ Enterprise Service Pack 2     {?????H1, ?????D1}

    (This is what I am expecting)
    ComputerName Operating System                   Service Pack                  BIOSSerialNumber
    ------------ ---------------- ------------                      ----------------
    001 Microsoft Windows 7 Enterprise                  Service Pack 1                 ?????H1
    017 Microsoft® Windows Vista™ Enterprise     Service Pack 2               ?????D1

    I am making a hardware inventory script, but encountered a small issue.

    Information is being pulled from each machine ,listed per line in computer_list.txt, but the serial numbers are listed together and on one line.

    What am I overlooking; is it something with nested objects?

    The same results are displayed when using the foreach-object enumerating method. The correct OS is displayed for each machine listed, so that tells me (Get-WmiObject -class Win32_OperatingSystem) is receiving a string from each line of the $computerlist variable. I am guessing the $BIOSSerialNumber variable's -computername parameter is causing the issue.
    RiffyRiotUser is Offline
    Basic Member
    Basic Member
    Posts:329
    Avatar

    --
    15 Aug 2012 01:21 PM
    This code...

    $BIOSSerialNumber = @{name='BIOSSerialNumber';expression={Get-WmiObject -class Win32_BIOS -computername $computerlist | Select-Object -ExpandProperty SerialNumber}}

    ... collects all the Bios Serial Numbers from all of the computers in $computerlist and puts them into one array... thats why you get {?????H1,?????D1} etc

    The code doesn't try and match the BIOS data with the Operating System data

    I would re-structure the code like this...

    
    $OSData=Get-WmiObject -class Win32_OperatingSystem -computername $computerlist
    $BIOSData=Get-WmiObject -class Win32_BIOS -computername $computerlist
    
    $CombinedData=@()
    
    $BIOSData | foreach-object{
        $computername=$_.__Server
        $CombinedData+=New-Object PSObject -Property @{
        'ComputerName'=$computername;
        'BiosSerialNumber'=$_.SerialNumber;
        'ServicePack'=($OSData | Where{$_.CSName -eq $computername}).CSDVersion;
        'OperatingSystem'=($OSData | Where{$_.CSName -eq $computername}).Caption
        }
    }
    $CombinedData
    


    Does that make sense?
    Rule #1: You should only use Format-Table as the final command in the pipeline and only when you're not using the output for anything else
    omega.redUser is Offline
    New Member
    New Member
    Posts:8
    Avatar

    --
    15 Aug 2012 06:32 PM
    If I am understanding this correctly, you created a custom object combined with a hashtable, correct?

    The hashtable starts here, correct?    -Property @{}

    I am not familiar with $CombinedData=@(); is it the start of an array?  What does the $CombinedData variable at the end do?

    Also, why did you combine a plus sign with the custom object here? --> $CombinedData+=New-Object 
    RiffyRiotUser is Offline
    Basic Member
    Basic Member
    Posts:329
    Avatar

    --
    15 Aug 2012 06:52 PM
    Not really, I created a custom object with "properties defined by a hash table".

    $CombinedData=@() defines an empty array. We create new-objects with properties and values we define, and add it to this array.

    $CombinedData+= adds the new object we create to the array

    Sometimes people use a temporary variable within the loop to create the new-object.

    $temp=New-Object PSObject -Property @{.....}

    Once the new object is created, then it's added it to the array.

    $CombinedData+=$temp

    I've decided not use a temporary variable, logically there's no reason for it.

    $CombinedData+=New-Object PSObject -Property @{.....}

    $CombinedData by itself at the end just displays what's in the array after we've finished adding our objects.

    Any clearer?
    Rule #1: You should only use Format-Table as the final command in the pipeline and only when you're not using the output for anything else
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Many thanks to our original sponsors: Quest Software • SAPIEN Technologies • Compellent • Microsoft footer
    footer   footer