Friday, May 23, 2014

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

4 comments:

  1. Any help on how to adjust this to get the Manufacturer / Model of the machine, rather than the OS information?

    ReplyDelete
    Replies
    1. Hi Geoff,

      If the machine is up then we can query the Win32_computerSystem class to get those details:
      PS>Get-WmiObject -Class Win32_ComputerSystem | select Manufacturer,Model

      Manufacturer Model
      ------------ -----
      Dell Inc. Latitude E6440

      But will have to check on which SMS Class is that information stored on the SCCM server.

      Delete
  2. Hi, and thanks a lot for your script which saved me hours... But, in fact, in the table you query, no information about OSArchitecture 'x86' or 'x64'. Can't find the way in wql to query and join information with the table System0 which contains the information.
    I made in SQL a : select SystemType0,Netbios_Name0,CPUType0 from dbo.v_GS_System0 JOIN dbo.v_R_System ON dbo.v_R_System.ResourceID = dbo.v_GS_System0.ResourceID where dbo.v_R_System.Netbios_Name0='$computer' which work, but can't find the "system0" table in wql format. Any idea ?
    Thanks a lot again for the function with cimsession :-)

    ReplyDelete
    Replies
    1. You could just query the other CIM class that contains that information and add it to the object later on.

      There was a way to actually create joins in the WQL too, but I do not recall it.

      Delete