none
Task Sequence condition where WMI class doesnt exist

    Question

  • Greetings,

      I want to add a condition in my task sequence that does a WMI query looking for a property value of the WIN32_TPM class. How will that condition be evaluated if the WIN32_TPM class doesnt even exist on a given machine?

    Thanks

    David Z

    Wednesday, October 09, 2013 9:46 PM

Answers

  • OK - I finally ran the task sequence on a machine that doesnt have the WIN32_TPM class but had a condition which referred to that class. According to the TS log, the condition was evaluated as false.
    • Proposed as answer by Brian Gonzalez Friday, October 18, 2013 3:00 PM
    • Marked as answer by David Zemdegs Saturday, October 19, 2013 1:23 AM
    Thursday, October 17, 2013 1:31 AM

All replies

  • This is the kind of thing you need to test.

    Set up a test lab (or just a test task sequence) and try it out.

    I'm thinking that would return an error and cause your task sequence to fail. You'd then have to script it.


    Blog: http://scriptimus.wordpress.com

    Thursday, October 10, 2013 4:39 AM
  • Use wbemtest (press start > run > type "wbemtest" > press enter) there you can test all your WMI queries, and see the result of your query.

    Perhaps you can turn your query the other way around, and instead of continuing with something if exist/true, do nothing when it doesn't exist.


    If this post is helpful please click "Mark for answer", thanks! Kind regards

    Thursday, October 10, 2013 8:06 AM
  • Its the not query as such, its the class. On some machines win32_tpm doesnt exist and I was wondering if anyone has coded wmi queries for classes that may not exist and what the result would be. I guess Ill have to test it when I get the time....

    Thanks

    David Z

    Thursday, October 10, 2013 10:17 PM
  • Post back and share your results, please David.

    Blog: http://scriptimus.wordpress.com

    Sunday, October 13, 2013 12:21 PM
  • David,

    You could do something like this. I found two methods you could use.

    In this case I used VBScript. You can probably translate it yourself into PowerShell.

    Querying a non-existent class results in an error from which you can't conclude what's wrong. Below methods use either .SubClassesOf or .InstancesOf.

    For more understanding: http://technet.microsoft.com/en-us/magazine/2008.11.heyscriptingguy.aspx

    Or MSDN: http://msdn.microsoft.com/en-us/library/aa393262(v=vs.85).aspx

    Remember that for the second method, you need to use On Error Resume Next!

    '=======================================================
    '
    ' AUTHOR: Michiel Wouters
    ' DATE  : 13-10-2013
    '
    ' COMMENT: Check for existence of a WMI class
    '
    '=======================================================
    Option Explicit
    
    Const wbemFlagForwardOnly = 32
    Const wbemErrInvalidClass = -2147217392
    
    Dim strComputer
    Dim strClassTPM
    Dim colClasses
    Dim objClass
    Dim objWMIService
    Dim blnClassTPMExists : blnClassTPMExists = False
    
    strComputer = "."
    strClassTPM = "Win32_TPM"
    Set objWMIService=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
        strComputer & "\root\cimv2")
        
    '-- Method 1
    Call EnumerateAllClasses()
    
    '-- Method 2
    Call GetInstancesOfClass()
    
    Sub EnumerateAllClasses()
    	For Each objClass in objWMIService.SubclassesOf()
    	    objWMIService.SubclassesOf()
    	    If objClass.Path_.Class = strClassTPM Then
    	        blnClassTPMExists = True
    	        WScript.Echo "yeeha"
    	        Exit For
    	    End If
    	Next
    	Call ShowResult()
    End Sub
    
    Sub GetInstancesOfClass()
    	On Error Resume Next
    	Set colClasses = objWMIService.InstancesOf(strClassTPM,wbemFlagForwardOnly)
    	
    	If Err.Number = -2147217392 Then
    		blnClassTPMExists = False
    	ElseIf Err.Number = 0 Then
    		blnClassTPMExists = True
    	End If
    	
    	Call ShowResult()
    End Sub
    
    Sub ShowResult()
    	If blnClassTPMExists = True Then 
    		Wscript.Echo "Class found: " & strClassTPM
    	Else
    		WScript.Echo "Class not found: " & strClassTPM
    	End If
    End Sub

    Good luck!

    Best regards

    Michiel


    System Center tech blog: http://michielw.blogspot.com


    Sunday, October 13, 2013 1:10 PM
  • Thanks. I havent tried this script but this is what I've come up with - havent used TSEnv as yet...

    $Class = "win32_tpm"
    If (Get-WmiObject -List -Namespace "root\cimv2" | Where-Object {$_.Name -eq $Class})
    {
        TSEnv:TPMExists = "True"
    }
    else
    {
        TSEnv:TPMExists = "False"
    }

    Sunday, October 13, 2013 9:56 PM
  • Don't forget to declare the COM object to work with the Task Sequence  environment.

    $objTSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment 
    $objTSEnv.Value("TPMExists") = $True 
    # Or $False
    

    The negative side of listing all classes is that this script will slow down your Task Sequence a bit.

    Good luck!



    System Center tech blog: http://michielw.blogspot.com


    Monday, October 14, 2013 6:38 PM
  • OK - I finally ran the task sequence on a machine that doesnt have the WIN32_TPM class but had a condition which referred to that class. According to the TS log, the condition was evaluated as false.
    • Proposed as answer by Brian Gonzalez Friday, October 18, 2013 3:00 PM
    • Marked as answer by David Zemdegs Saturday, October 19, 2013 1:23 AM
    Thursday, October 17, 2013 1:31 AM
  • Rens,  Thanks for this tip! I never knew about wbemtest!  I used WMIC, but its not nearly as powerful as wbemtest.  Thanks.


    -BrianG (http://supportishere.com)

    Friday, October 18, 2013 3:00 PM
  • AIUI with MDT 2012 you do not need the comobject. $tsenv is a builtin drive for accessing variables with a TS.
    Sunday, October 20, 2013 11:13 PM