powershell get-qadcomputer + wmi query

Please can someone tell me how to use

***Get-QADComputer -LdapFilter "(operatingsystem=*server*)"***

in my script below. (I can get it to work with the text file it just fails on the get-qadcomputer bit)

***function inventory {
PROCESS {
$os = gwmi win32_operatingsystem -comp $_
$cs = gwmi win32_computersystem -comp $_
$obj = new-object psobject
$obj | add-member noteproperty Name $os.csname
$obj | add-member noteproperty RebootDate $os.ConvertToDateTime($os.LastBootUpTime)
$obj | add-member noteproperty Manufacturer $cs.manufacturer
$obj | add-member noteproperty Model $cs.model
$obj | add-member noteproperty Operating_System $os.caption
$obj | add-member noteproperty Architecture $os.OSArchitecture
$obj | add-member noteproperty Service_Pack $os.ServicePackMajorVersion
write-output $obj
}
}
gc C:\scripts\lastreboot\syn_pc.txt -ErrorAction SilentlyContinue | inventory | Export-Csv c:\scripts\lastreboot\inventory2.csv***
2

1 Answer

This issue you're facing is that the inventory function you wrote was designed to use $_ directly, and you are assuming that the $_ Variable will only be a string. The command works initially because you are extracting a list of names from a file, which is giving you a list of names which are of type [String], and passing this collection/list of strings into the pipeline for inventory to use.

But when using Get-QADComputer, you are getting objects of type [Selected.Quest.ActiveRoles.ArsPowerShellSnapIn.Data.ArsComputerObject] which is so much more then just a list of names. The objects have a name property, which i assume you truly are after, along with other information. You need to pull just the name portion out and pass that along into the pipeline

So you have two options:

  • Change the inventory function to account for the difference in input types from the pipeline (simple strings which contain the just names, or objects that have a name property and use just the name portion)

  • Change the way in which you are calling the script

Here is the command, I think you want

 Get-QADComputer -LdapFilter "(operatingsystem=*server*)" | Select -ExpandProperty Name | inventory | Export-Csv c:\scripts\lastreboot\inventory2.csv

Note the Select -ExpandProperty Name, which takes the ADObject and will pass only the name along into the pipeline which your inventory function is expecting

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like