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

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?

PowerShell + SCCM : Run CM cmdlets remotely

Today I saw a tweet about using implicit remoting to load the Configuration Manager on my machine by Justin Mathews . It caught my eye as I have never really tried it, but theoretically it can be done. Note - The second tweet says "Cannot find a provider with the name CMSite", resolution to which is in the Troubleshooting section at the end.

PowerShell : Trust network share to load modules & ps1

Problem Do you have a central network share, where you store all the scripts or PowerShell modules ? What happens if you try to run the script from a network share ? or if you have scripts (local) which invoke scripts or import PowerShell modules stored on this network share ? Well you would see a security warning like below (Note - I have set execution policy as 'Unrestricted' not 'bypass' here): Run a .ps1 from the network share Well this is a similar warning, which you get when you download scripts from Internet. As the message says run Unblock-File cmdlet to unblock the script and then run it, let's try it.