Help Requested: Get-ADComputer + Get-WmiObject

This topic contains 5 replies, has 4 voices, and was last updated by  Graham Beer 7 months ago.

  • Author
    Posts
  • #64590

    Dean
    Participant

    Hi PowerShell.org.

    I am a new to PowerShell and have no coding, scripting or developer experience.
    I wish to generate a csv file that contains an inventory of workstations on our domain.
    I have used the following that gives me approximately half the information I require:

    Get-ADComputer -Filter * -Property * | Select-Object Name,OperatingSystem,OperatingSystemServicePack,OperatingSystemVersion | Export-CSV MachineList.csv -NoTypeInformation -Encoding UTF8

    I would like to add some hardware information to this query and have it included in the csv file, specifically

    Get-WmiObject Win32_Computersystem manufacturer, model

    I have discovered that Get-WmiObject cmdlet doesn't accept pipeline input so I am at a loss how to take the output of Get-ADComputer and query it for the required hardware info.

    Any assistance would be appreciated.

  • #64617

    Wei-Yen Tan
    Participant

    Hi there dean,

    The way I see it is that There are two parts to this question.
    The first is that you are trying to extract the values from the CSV and pass that into the wmiobject.
    Here is something that you can do in powershell. You can make use of parantheticals () to get the code to run first and then pass that in.

    For example

    get-wmiobject -class win32_bios -computername (Get-WmiObject -Class win32_service -ComputerName (Get-ADComputer -Filter {operatingsystem -like "*server*"} | select -ExpandProperty name)

    What this will basically do is run whatever is in my script block first (which will produce strings...then that will be passed to get-wmiobject).

    The second requirement is combining the data, and that is a bit more work.

    If I were you I would make use of variables and new-object and hashtable going through the collection.

    I can go through with that for you...but at this stage I will leave you with the fun of exploring that first.

    Keep in touch and we can touch base later. 🙂

    Regards,

    WY

  • #64620

    Dean
    Participant

    Thank you for your response and input WY.

    As a newbie to PowerShell I appreciate you taking the time to help me out in what is, I assume, to an experienced user quite a simple problem- indicative of my lack of understanding of the technology rather than any major technical hurdle.

    I will work with what you have provided and see if I can create a suitable script.

    Cheers,
    Dean

  • #64660

    You should start thinking what you want to do not how and that will guide you. Pipelines are an implementation detail.

    Now, what you want is to get a list of AD Computers. Then, for each of them you want to get some computer system information, and then construct a new object that has properties from both the AD Computer and the Bios info and output as csv. Thinking like that the PowerShell script is almost obvious

    Get-ADComputer -Filter * -Property * | 
        ForEach-Object {
            $ad = $_    # Keep AD Computer object
            $system = Get-WmiObject Win32_Computersystem -ComputerName $ad.name # Get wmi info for the current ad computer
            $prop = @{  # create a hash table with the required properties
                'Name' = $ad.name;
                'OperatingSystem' = $ad.OperatingSystem;
                'manufacturer = $system.manufacturer;
                # ... add all the required properties as 'name' = value
            }
            new-object -type PSObject -property $prop # Create a custom object with all the above properties, and output it to pipeline
        } |  # foreach ends
        Export-CSV MachineList.csv -NoTypeInformation -Encoding UTF8
    

    Hope it helps

    Thanassis
    Based on this script you can add more information (doing more wmi calls)

  • #64692

    Graham Beer
    Participant

    Hi Dean,

    This option does require a bit more typing but it does display the results nicely in a CSV. I've used PowerShell's custom objects to create the information you were after in list.

    $Computers = 'Computer1','Computer2','Computer3'
    
    $Objects = $Computers |
        foreach {
            $AD = Get-ADComputer -Filter { Name -eq $_ } -Property Name,OperatingSystem,OperatingSystemServicePack,OperatingSystemVersion
            $CIM = Get-CimInstance Win32_Computersystem -Filter "Name = '$_'" -Property manufacturer, model
    
            # Set $properties in HashTable
            $properties = @{
                DistinguishedName = $_.DistinguishedName
                DNSHostName = $AD.DNSHostName
                Enabled = $AD.Enabled
                Name = $AD.Name
                ObjectClass = $AD.ObjectClass
                ObjectGUID = $AD.ObjectGUID
                OperatingSystem = $AD.OperatingSystem
                OperatingSystemServicePack = $AD.OperatingSystemServicePack
                OperatingSystemVersion = $AD.OperatingSystemVersion
                SamAccountName = $_.SamAccountName
                SID = $AD.SID
                UserPrincipalName = $AD.UserPrincipalName
                manufacturer = $CIM.manufacturer
                model = $CIM.model
            }
            # create custom objects
            New-Object -TypeName PSObject -Property $properties
    }
    # Return Results
    $Objects | Export-CSV c:\temp\MachineList.csv -NoTypeInformation -Encoding UTF8
    

    Have a go at that if you like and any questions you might have feel free to ask me and i'll happily do my best to explain ! 🙂

  • #64693

    Graham Beer
    Participant

    * Removed Duplicate post *

You must be logged in to reply to this topic.