win32_computersystem and win32_bios in 1 command

This topic contains 9 replies, has 4 voices, and was last updated by Profile photo of Matt Saunders Matt Saunders 2 years, 1 month ago.

  • Author
    Posts
  • #26367
    Profile photo of Matt Saunders
    Matt Saunders
    Participant

    Hey guys

    I am very new to PowerShell, I need to get information about our PCs at work

    Manufacturer
    Model
    Name
    Serial

    I have used this and it works but I do not get the serial

    Invoke-Command -ComputerName 10.100.2.77,10.100.2.83 -Credential domain\administrator { Get-WMIObject Win32_Computersystem } | Format-Table Name,Manufacturer,Model

    How I know I can get this information from win32_computersystem and from win32_bios

    The problem I am having it getting all the information in one command, so far I have created two variables

    $ComputerSystem = Get-WmiObject win32_computersystem 
    $bios = Get-WmiObject win32_bios

    Now I think I need to create a foreach loop to put all this together, any help would be fantastic

    Thanks Guys

  • #26369
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

    This is quite easy to do with the following function (and I'm assuming you have administrative access to those computers):

    #requires -Version 1
    Function Get-ComputerDetails
    {
        [cmdletbinding()]
        Param([string[]]$Computer)
        
        $result = @()
    
        foreach ($node in $Computer)
        {
            try
            {
                $systemInfo = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $node -ErrorAction Stop
                $systemBios = Get-WmiObject -Class Win32_Bios -ComputerName $node -ErrorAction Stop
                
                $customObject = [pscustomobject][ordered]@{
                    Name         = $systemInfo.Name
                    Manufacturer = $systemInfo.Manufacturer
                    Model        = $systemInfo.Model
                    Serial       = $systemBios.SerialNumber
                }
            }
            catch 
            {
                Write-Error -Message "The command failed for computer $node. Message: $_.Exception.Message"
                break
            }
            $result += $customObject
        }
        $result
    }
    

    You use this function as following:

    Get-ComputerDetails -Computer or01,dc01,ipam01

    If you want to output to a csv file, just as easy:

    Get-ComputerDetails -Computer or01,dc01,ipam01 | Export-Csv -Path 'C:\Temp\Export.csv' -NoTypeInformation
  • #26371
    Profile photo of Matt Saunders
    Matt Saunders
    Participant

    Thank you so much for the reply

    I think I may be doing something wrong since I don't get any output from the command, this is what I have done

    Copied the code and saved it as Get-ComputerDetails.ps1

    ran the following command

    .\Get-ComputerDetails.ps1 -computer localhost | Export-Csv C:\Users\admin\Desktop\Details.csv

    The file is empty

  • #26372
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

    It's a function. So in order to use it you either have to load it in your PowerShell User profile, or creating a new script, and dot source the Get-ComputerDetails.ps1 file.

    To use it immediately, try to open up PowerShell ISE, and paste the function in the script pane. After that select all the code and then press F8. The function is now loaded in memory, and you can use it in the current PowerShell session (the console below the scriptpane).

    If you want to use it in all your PowerShell sessions, put the function in your PowerShell profile. Remember, PowerShell.exe and PowerShell_ise.exe use different profiles.

    The profiles exists in this path: %username%\My Documents\WindowsPowerShell\

    If you want to dot source it in a script, you have to do this:

    . C:\Get-ComputerDetails.ps

    Get-ComputerDetails -computer localhost

  • #26374
    Profile photo of Matt Saunders
    Matt Saunders
    Participant

    Thank you so much, it is working, is it possible to add the name of the user who last logged onto the PC?

  • #26375
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Windows doesn't specifically track the last logged-on user. On a server computer, people are authenticating constantly to file shares and other resources, so the OS in general doesn't keep track. If you've enabled security auditing, you could query the most recent event log entry for logons.

    I'll note, with bias, that a lot of what you're trying to do with functions and such is very thoroughly covered in "Learn PowerShell Toolmaking in a Month of Lunches." That might be an easier way to learn some of this stuff without feeling like you're banging your head into a wall :).

  • #26376
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

    In a way, yes. But it's not really trustable. As long as you didn't specify in your GPO's that the last logged on user should not be cleared in the login box (the one you see when you need to login into the computer), then this addition is more or less acceptable:

    #requires -Version 1
    Function Get-ComputerDetails
    {
        [cmdletbinding()]
        Param([string[]]$Computer)
        
        $result = @()
    
        foreach ($node in $Computer)
        {
            try
            {
                $systemInfo = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $node -ErrorAction Stop
                $systemBios = Get-WmiObject -Class Win32_Bios -ComputerName $node -ErrorAction Stop
                
                $customObject = [pscustomobject][ordered]@{
                    Name         = $systemInfo.Name
                    Manufacturer = $systemInfo.Manufacturer
                    Model        = $systemInfo.Model
                    Serial       = $systemBios.SerialNumber
                    LastLoggedOn = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI' -Name 'LastLoggedOnSAMUser' | Select-Object -ExpandProperty LastLoggedOnSAMUser
                }
            }
            catch 
            {
                Write-Error -Message "The command failed for computer $node. Message: $_.Exception.Message"
                break
            }
            $result += $customObject
        }
        $result
    }
    
  • #26377
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

    And Don is right.

    I'm throwing this functions to you assuming you understand.

    But follow Don's advice and read that book. (-:

    And to be honest, I'm also learning how to create proper functions.

  • #26400
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    There are many ways to do the same thing, your end goal is to create a single object with all properties. I see that system info has the most properties, so I just use Select-Object to get those properties and use calculated properties to cover the last 3 properties. I try to use this method first and if there is a need to order the results or it's a bunch of single properties from separate sources, then I would use New-Object or a similar method. Here is the excerpt:

    $node = "."
    
    $systemBios = Get-WmiObject -Class Win32_Bios -ComputerName $node -ErrorAction Stop
    Get-WmiObject -Class Win32_ComputerSystem -ComputerName $node -ErrorAction Stop | 
    Select Name,
           Manufacturer,
           Model,
           @{Label="Serial";Expression={$systemBios.SerialNumber}},
           @{Label="LastLoggedOn";Expression={Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI" -Name "LastLoggedOnSAMUser" | Select-Object -ExpandProperty LastLoggedOnSAMUser}}
    
    
  • #26406
    Profile photo of Matt Saunders
    Matt Saunders
    Participant

    Guys you have been very helpful, it's great to find a PowerShell community where I can get help so quickly 🙂

You must be logged in to reply to this topic.