Skip to main content

PowerShell + SCCM 2012 Tip : Get OS Inventory

Another post inspired by the daily job as a ConfigMgr Admin.

Usually while deploying applications as a best practice we check few details about the machine like the Operating System it's running on to ensure that we deploy the correct package targeted to the correct OS.

In past there have been cases where a package targeting XP was send to a Windows 7 machine or vice-versa.

So how do I check OS information :
  1. If Machine is online ;Use PowerShell or any other tool to query WMI to get the info.
  2. If Machine is offline ;Head over to SCCM Reports to see the info

We can use PowerShell to do perform the second part as well because ConfigMgr SMS namespace  provider stores this information in the class named SMS_G_System_Operating_System .

Note - The data in the class SMS_G_System_OPERATING_SYSTEM is stored as a result of the Hardware Inventory of the Win32_OperatingSystem class on a remote machine. So if the H/W Inventory has not run then this data is not available.

One can see the class and the properties on it using Get-WMIObject or Get-CIMInstance cmdlet like below:
PS C:\> Get-WmiObject -Class SMS_G_System_OPERATING_SYSTEM -Namespace Root/SMS/site_DEX -List -ComputerName DexSCCM


   NameSpace: ROOT\SMS\site_DEX

Name                                Methods              Properties
----                                -------              ----------
SMS_G_System_OPERATING_SYSTEM       {}                   {BootDevice, BuildNumber, Caption, CountryCode...}


PS C:\> Get-WmiObject -Class SMS_G_System_OPERATING_SYSTEM -Namespace Root/SMS/site_DEX -List -ComputerName DexSCCM| select -ExpandProperty properties | select Name

Name
----
BootDevice
BuildNumber
Caption
CountryCode
CSDVersion
Description
FreePhysicalMemory
FreeVirtualMemory
GroupID
InstallDate
LastBootUpTime
Locale
Manufacturer
MaxNumberOfProcesses
Name
Organization
OSLanguage
RegisteredUser
ResourceID
RevisionID
SystemDirectory
TimeStamp
TotalSwapSpaceSize
TotalVirtualMemorySize
TotalVisibleMemorySize
Version
WindowsDirectory



Note - One needs to pass the SCCM Server name to -ComputerName parameter and to -NameSpace parameter the correct namespace e.g. Root/SMS/Site_DEX (where DEX is the 3 letter site code)

So utilizing this information I wrote a function  Get-OSInfo  which works in the below order :

  • Check If Machine is online and the User running the Script has access to query WMI on the remote machine : If Yes then query Remote Machine for the OS information
  • If the Machine is offline or the User is denied access to query WMI on the remote machine then we query SCCM Server for the same.
Make a note of the property named PSComputername on the object written to pipeline this tell where the information came from (Remote machine or ConfigMgr Server).

Usage:
PS E:\> Get-OSinfofromSCCM -ComputerName dexterdc -Verbose
VERBOSE: [BEGIN]
VERBOSE: [PROCESS] Querying dexterdc for OSInfo


PSComputerName  : dexterdc
ComputerName    : dexterdc
OS              : Microsoft Windows Server 2012 R2 Datacenter Preview
ServicePack     : 
LastBootupTime  : 5/23/2014 6:06:05 AM
InstallDate     : 1/18/2014 3:29:37 PM
OSVersion       : 6.3.9431
SystemDirectory : C:\Windows\system32

VERBOSE: [END]



PS E:\> Get-OSinfofromSCCM -ComputerName dexterdc -SCCMServer dexsccm 


PSComputerName  : dexterdc
ComputerName    : dexterdc
OS              : Microsoft Windows Server 2012 R2 Datacenter Preview
ServicePack     : 
LastBootupTime  : 5/23/2014 6:06:05 AM
InstallDate     : 1/18/2014 3:29:37 PM
OSVersion       : 6.3.9431
SystemDirectory : C:\Windows\system32




PS E:\> Get-OSinfofromSCCM -ComputerName dexterdc -SCCMServer dexsccm -QuerySCCMOnly -Verbose
VERBOSE: [BEGIN]
VERBOSE: SCCM Server was supplied as an argument ...trying to open a CIM Session
VERBOSE: [BEGIN] WSMAN is responsive
VERBOSE: Operation '' complete.
VERBOSE: [BEGIN] [WSMAN] CIM SESSION - Opened
VERBOSE: Perform operation 'Query CimInstances' with following parameters, ''queryExpression' = select * from SMS_ProviderLocation where ProviderForLocalSite = true,
'queryDialect' = WQL,'namespaceName' = root\sms'.
VERBOSE: Operation 'Query CimInstances' complete.
VERBOSE: [BEGIN] Provider is located on DexSCCM.dexter.com in namespace root\sms\site_DEX
VERBOSE: [PROCESS] Querying dexsccm for OSInfo
VERBOSE: Perform operation 'Query CimInstances' with following parameters, ''queryExpression' = Select Version,CSDVersion,SystemDirectory,Installdate,LastBootuptime,
installdate,caption,Description from SMS_G_System_OPERATING_SYSTEM JOIN SMS_R_System ON SMS_R_System.ResourceID = SMS_G_System_OPERATING_SYSTEM.ResourceID where SMS_
R_System.NetbiosName='dexterdc','queryDialect' = WQL,'namespaceName' = root\sms\site_DEX'.
VERBOSE: Operation 'Query CimInstances' complete.


PSComputerName  : dexsccm
ComputerName    : dexterdc
OS              : Microsoft Windows Server 2012 R2 Datacenter Preview
ServicePack     : 
LastBootupTime  : 5/22/2014 4:24:50 PM
InstallDate     : 1/18/2014 8:59:37 PM
OSVersion       : 6.3.9431
SystemDirectory : C:\Windows\system32

VERBOSE: [END] Removing the CIM Session
VERBOSE: [END]

The code is self explanatory and there is a cool trick where I explicitly thow an exception to get inside catch block where nested try-catch block is used again.
Also have a look at the WQL query used to get the info from the ConfigMgr Server. Pretty neat ! :-B

There are a lot of Hardware inventory classes in ConfigMgr which can be used by us to inventory our environment and this should serve as a base to build upon :)

That's it for the post. If you have any suggestions or feedback please do drop a comment.


Below is the script link and gist:
http://gallery.technet.microsoft.com/Get-OS-Info-from-SCCM-d4e2858a

Popular posts from this blog

Azure DevOps Tips & Tricks - Find private REST APIs

Often working with Azure DevOps, I hit a wall trying to automate some tasks but there are no REST API's made public yet. It was one of those task of automating creation of Environments in multi-stage YAML based pipelines in AzDO. Quick research reveals that this has been requested in uservoice  (please upvote). Let's see one of the very simple ways to discover some of these APIs.

PowerShell + SCCM : WMI Scripting

Why should I use WMI, when there is a PowerShell module available for Configuration Manager (CM Module) already? Well the cmdlets behind the scene interact with the WMI layer and if you know which WMI classes the corresponding cmdlet work with , it can be of help in future by : Switching to native WMI calls when the CM cmdlets fail for some reason (probably bug in the CM Module). Making your scripts more efficient by optimizing the WMI (WQL query) calls, the cmdlet will query all the properties for an Object (select *) you can select only ones you need.  Lastly no dependency on the CM Module, you can run these automation scripts from a machine not having the CM console installed (needed for CM module). Moreover ConfigMgr uses WMI extensively, you already have this knowledge leveraging it with PowerShell shouldn't surprise you. This post assumes you have been working with CM cmdlets (you already are versed with PowerShell), know where the WMI namespace for ConfigMgr resides

Test connectivity via a specific network interface

Recently while working on a Private cloud implementation, I came across a scenario where I needed to test connectivity of a node to the AD/DNS via multiple network adapters.  Many of us would know that having multiple network routes is usually done to take care of redundancy. So that if a network adapter goes down, one can use the other network interface to reach out to the node. In order to make it easy for everyone to follow along, below is an analogy for the above scenario: My laptop has multiple network adapters (say Wi-Fi and Ethernet) connected to the same network. Now how do I test connectivity to a Server on the network only over say Wi-Fi network adapter?