PowerShell and WMI query

by n3rdbiker at 2012-09-14 10:16:06

I have been very fortunate so far… crossing my fingers that my luck continues.

I would like to be able to use a PowerShell WMI query to determine if a system has a configuration manager client ie. SCCM, SMS installed - if the system does have a client, then I would like to pull the sitecode that the system belongs to.

Any helpful tools for searching WMI would be greatly welcomed as well…

Thanks,

J
by poshoholic at 2012-09-14 10:39:39
Sure, that’s easy. For SCCM you want to start with the root\sms namespace. Here is what you’ll be looking for:
if ($smsProviderLocation = Get-WmiObject -Namespace root\sms -Class SMS_ProviderLocation -Filter 'ProviderForLocalSite=True' -ErrorAction SilentlyContinue) {
$smsProviderLocation.SiteCode
}

That will give you back the sitecode if SCCM is installed on a system.

As for searching WMI, you can interrogate WMI using Get-WmiObject, which is what I do. For example:

Get-WmiObject -Namespace root\sms -List # Look at all classes in the root\sms namespace (or any other namespace)

Once you get a class you want to see more of, you can browse into the properties and methods of that class using PowerShell.

You can also use graphical WMI browsers. There is one written in PowerShell by Marc van Oursow called WMI Explorer. That’s only one of many however.
by n3rdbiker at 2012-09-14 12:03:08
Posh I apprecaite the tip but have ran into a snag. I’m not finding the root/sms namespace on my system. I know the SCCM client is on the system, but the namespace doesn’t appear to be there. Any idea what I am missing?
by poshoholic at 2012-09-14 12:29:00
Whoops, my mistake. I went for the SCCM server namespace, not the SCCM client namespace. SCCM has both server and client interface extensions for WMI.

My SCCM lab is offline (I need to bring it back into my new Hyper-V install post HD crash/replace), so I’m going on memory here, but I think you need to do something like this:

#region Test if the client is installed by checking for the root\ccm namespace and the SMS_Client class.

$ccmNamespace = Get-WmiObject -Namespace root -Class __NAMESPACE -Filter 'name="ccm"'
if (-not $ccmNamespace) {
Write-Error 'Client is not installed.'
return
}

$smsClient = Get-WmiObject -Namespace root\ccm -Class SMS_Client -ErrorAction SilentlyContinue
if (-not $smsClient) {
Write-Error 'Client is not installed.'
return
}

#endregion

#region Now get the assigned site name for the client.

$smsClient.GetAssignedSite()

#endregion


Give that a try and see if that gives you what you’re looking for.
by n3rdbiker at 2012-09-14 12:50:01
Very close POSH
now getting an error that the method doesn’t exist.
Method Invocation Failed… [System.Management.ManagementObject#ROOT\ccm\SMS_Client] does not contain a method ‘GetAssignedSite’

Thanks again for all of your help,

j
by poshoholic at 2012-09-14 12:56:19
Very close is good. :slight_smile:

What about a property, AssignedSite, like this:
$smsClient.AssignedSite
You should be able to see all property values by invoking this:
$smsClient | Format-List *
and all properties and methods (not values) by invoking this:
$smsClient | Get-Member
by n3rdbiker at 2012-09-14 13:03:26
no sir, neither there is a "site" property but it has no value.
I did fine an example using that method on the "google machine" they were calling a COM object of the SCCM client and then running the method… a little too deep for me.

I found that root/ccm sms_authority has a name property which is the site code - using a little bit of regex magic I was able to trim off the SMS: and return only the site code. I wouldn’t have gotten there if it weren’t for your help. Thank you very much.
by poshoholic at 2012-09-14 13:14:30
Excellent, I’m glad you found a way to work it out. My past approach was more server focused, which it seems is more straightforward. There is a ton of information in the WMI provider for SCCM…it’s just not always easy to find it. :slight_smile:
by Makovec at 2012-10-11 07:58:52
Back to using of COM. I used it in the past, if you want to return just SiteCode, you can use following piece of code:
(New-Object -com Microsoft.SMS.Client).GetAssignedSite()
by Makovec at 2012-11-20 02:57:14
Hi,

Can you specify what’s not working and what error message you received?

Thanks,
David