Get-Adcomputer output

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Olaf Soyk Olaf Soyk 2 months, 3 weeks ago.

  • Author
    Posts
  • #69613
    Profile photo of Jason
    Jason
    Participant

    I'm using Get-Adcomputer to pull computers from AD that have 133 in the name. The command works but I need to put each computer's name into a variable so that I can use it to get information from each computer.

    $computername =get-adcomputer -filter {Name -like "sd133*"} | select -expandproperty name 
    $bios = gwmi -class win32_bios | select -expand smbiosbiosversion
    $model  = gwmi -class win32_computersystem | select -expand model
    
    foreach ($Name in $ComputerName) {
    
    $objAverage = New-Object System.Object
    $objAverage | Add-Member -type NoteProperty -name Name -value $computername
    $objAverage | Add-Member -type NoteProperty -name Bios -value $bios
    $objAverage | Add-Member -type NoteProperty -Name Model -Value $model
    
    
    }
    

    Output:

    Name Bios Model
    —- —- —–
    {SD133-01, SD133-02, SD133-03, SD133-04...} A16 OptiPlex 760

  • #69618
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    I suspect you're looking for something like this:

    $computers = get-adcomputer -filter {Name -like "sd133*"} | Select-Object -ExpandProperty name
    $ResultObject = New-Object System.Collections.Generic.List[object]
    
    foreach ($ComputerName in $computers){
        $bios = Get-WmiObject -Class win32_bios -ComputerName $ComputerName | Select-Object -ExpandProperty smbiosbiosversion
        $model  = Get-WmiObject -Class win32_computersystem -ComputerName $ComputerName | Select-Object -ExpandProperty model
        $newobj = [PSCustomObject]@{
            ComputerName = $ComputerName
            BIOS = $bios
            Model = $model
        }
        $ResultObject.Add($newobj)
    }
    
    $ResultObject
    
    • #69966
      Profile photo of Jason
      Jason
      Participant

      Thanks! This will get what I need and I do see my mistakes.

  • #69621
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    You've got several things wrong. Your WMI calls are getting the local computer information, not the remote computer, so they have to be inside your loop. Next you are referencing $ComputerName, which is an array of objects versus $Name being the current object in your loop. Start with this basic example:

    $computername =get-adcomputer -filter {Name -like "sd133*"} | select -expandproperty name 
    
    $results = foreach ($Name in $ComputerName) {
    
        $bios = gwmi -class win32_bios -ComputerName $Name| select -expandproperty smbiosbiosversion
        $model  = gwmi -class win32_computersystem -ComputerName $Name| select -expandproperty model
    
        New-Object -TypeName PSObject -Property @{
            ComputerName = $Name
            Bios         = $bios
            Model        = $model
        }
    
    }
    
    $results
    

    What you are attempting to do is a very common Powershell task. I would recommend getting a book on Powershell or minimally looking at other examples. The example above does not have any error handling if a computer is offline, but as I was saying this a very common task so examples should be easy to find.

    • #69964
      Profile photo of Jason
      Jason
      Participant

      The script works and I've added error handling but the output is in the wrong order.

      I get Bios Name Model where I'd like to have the Name first. I'm not sure how to change that?

    • #69967
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

      Objects are by default "unsorted" and "unordered". If you like to maintain a special order you will have to sort it when you output it or you can create it already ordered:

      New-Object -TypeName PSObject -Property ([ORDERED]@{
              ComputerName = $Name
              Bios         = $bios
              Model        = $model
          })

You must be logged in to reply to this topic.