Your strategic advantage starts here

Configuration Manager with Powershell and WQL

Configuration Manager with Powershell and WQL

Instead of using Configuration Manager PowerShell Cmdlets, we can use WMI query language (WQL) to search our configuration manager inventory instead.

This blog provides and example of how we can search configuration manager for information on all devices, including name, manufacturer, model, free disk space, organisational unit and last hardware scan.

$configManagerServer = "xxx.yyy.local"
$configManagerSiteCode = "xxx"

$WQLquery = @"
select OS.Caption,S.Name,S.SystemOUName,LD.FreeSpace,WS.LastHardwareScan,CS.Manufacturer,CS.Model
from SMS_R_System AS S
inner join SMS_G_System_OPERATING_SYSTEM AS OS on OS.ResourceId = S.ResourceId 
inner join SMS_G_System_LOGICAL_DISK AS LD on LD.ResourceId = S.ResourceId
inner join SMS_G_System_WORKSTATION_STATUS AS WS on WS.ResourceId = S.ResourceId
inner join SMS_G_System_COMPUTER_SYSTEM AS CS on CS.ResourceId = S.ResourceId
"@

$allResults = Get-WmiObject -Query $WQLquery -ComputerName $configManagerServer -Namespace "root\sms\site_$configManagerSiteCode"

foreach($result in $allResults) {

    $name = $result.S.Name
    $ou = $result.S.SystemOUName
    if ($null -ne $ou) {
        $ou = $ou[$ou.Count-1]
    }
    $freeBytes = $result.LD.Freespace #in MB
    $gb = [math]::Round(($freeBytes / 1024),2) #convert to GB
    $os = $result.OS.Caption
    $lhs = $result.WS.LastHardwareScan
    if ($null -ne $lhs) {
        $lhs = [System.Management.ManagementDateTimeConverter]::ToDateTime($lhs).toString("dd/MM/yyyy")
    }
    $man = $result.CS.Manufacturer
    $mod = $result.CS.Model

    write-host $name
    write-host $ou
    write-host $gb
    write-host $os
    write-host $lhs
    write-host $man
    write-host $mod


}
Related Posts
Leave a Reply
Give us a call

Available from 9am to 8pm, Monday to Friday.

Send us a message

Send your message any time you want.

Our usual reply time: 1 Business day
Give us a call

Available from 9am to 8pm, Monday to Friday.

Send us a message

Send your message any time you want.

Our usual reply time: 1 Business day