Author Posts

April 26, 2017 at 7:38 pm

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

April 26, 2017 at 7:52 pm

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

April 26, 2017 at 8:02 pm

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.

May 2, 2017 at 1:41 pm

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?

May 2, 2017 at 1:44 pm

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

May 2, 2017 at 2:25 pm

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
    })