urgent help on looping !

This topic contains 10 replies, has 3 voices, and was last updated by  Graham Beer 2 years, 6 months ago.

  • Author
    Posts
  • #24619

    Graham Beer
    Participant

    Hi,

    Got a work issue where by during a task sequence we need a WMI class to exist before carrying out a command.

    What i want to do is a do loop until the class exists and if not wait 30 seconds and try again and again if not loop 30 seconds and try again... you get the jist ! 🙂

    Is this possible and can someone help give me a starter for 10 ? Would be so grateful !

  • #24621

    Graham Beer
    Participant

    This is what i have so far...

    $WMIClass = Get-CimInstance -Namespace Root\ccm -ClassName SMS_Client

    Do
    {
    $check = [boolean]$WMIClass
    } Until ($check -eq $true) Write-Output " " "$WMIClass does exist"

    Basically that will do a job....

  • #24622

    Graham Beer
    Participant

    Think i'm getting closer...

    $WMIClass = Get-CimInstance -Namespace Root\ccm -ClassName SMS_Client
    $timeout = new-timespan -Minutes 5
    $sw = [diagnostics.stopwatch]::StartNew()
    while ($sw.elapsed -lt $timeout){
    if ([boolean]$WMIClass){
    write-output "Class has been found!"
    return
    }

    start-sleep -seconds 5
    }

    write-output "SMS_Client still does not exist, timed out"

  • #24623

    Matt Bloomfield
    Participant

    I think this should do it

    do {
    
        $WMIClass = Get-CimInstance -Namespace Root\ccm -ClassName SMS_Client
        if (-not $WMIClass) {
            Write-Warning "Unable to get class. Sleeping for 30 seconds."
            Start-Sleep -Seconds 30
        }
    
    } 
    
    while (-not $WMIClass)
    

    I put the if statement in so that you don't have to wait 30 seconds for the script to continue if it already exists.

  • #24624

    Graham Beer
    Participant

    Matt you are a legend ! Thank you.

  • #24625

    Graham Beer
    Participant

    Matt,

    how can i put a timer on for 5 minutes and timeout if it doesn't exist after that time ?

    $timeout = new-timespan -Minutes 5
    $sw = [diagnostics.stopwatch]::StartNew()
    while ($sw.elapsed -lt $timeout){
    do {

    $WMIClass = Get-CimInstance -Namespace Root\ccm -ClassName SMS_Client
    if (-not $WMIClass) {
    Write-Warning "Unable to get class. Sleeping for 30 seconds."
    Start-Sleep -Seconds 30
    }

    }

    while (-not $WMIClass)

  • #24626

    Matt Bloomfield
    Participant

    I'd be inclined to add a counter and return if the count is exceed. 5 minutes = 300 seconds = 10 tries.

    
    $tries = 0
    
    do {
     
        if ($tries -lt 10) {
    
            $WMIClass = Get-CimInstance -Namespace Root\ccm -ClassName SMS_Client
    
            if (-not $WMIClass) {
                Write-Warning "Unable to get class. Sleeping for 30 seconds."
                Start-Sleep -Seconds 30
                $tries++
            }
        }
    
         else {
            return
        }
     
    } 
     
    while (-not $WMIClass)
    
    
  • #24627

    Graham Beer
    Participant

    Matt, again thank you. Your knowledge is brilliant. I'm learning so much as i go.

  • #24633

    Rob Simmers
    Participant

    Can I ask why you are writing a script to wait for the SMS client to be installed? The task sequence is a ordered sequence, so if you wanted to do something after the client installed, you should just put that step after the SMS client installation in the Task Sequence. By default, you should not need a lot of scripts because most logic can be handled by the Task Sequence, but it sounds like you are adding a lot of complexity. I've built hundreds of Task Sequences in SCCM and you should definently use K.I.S.S (Keep It Super Simple). You will already need to dig through a ton of logs to figure why something isn't working, so try to keep it simple.

  • #24639

    Graham Beer
    Participant

    Hi Rob,

    Good question !

    So basically as part of our build we are setting departments by variable to move a machine to a certain collection to pull down apps. The variable "tattoo's" the registry. The collection runs a query to check against variable from the hardware inventory.

    The issue is that the SMS_Client class is not fully populated by the end of the build, so the hardware inventory does not run and move the PC. The purpose of the loop script is to wait for the SMS_Client (TriggerSchedule method) to be available before running.

    Yes, this is getting more complicated, but my learning of PowerShell is getting a helping hand !

  • #24642

    Graham Beer
    Participant

    Rob,

    So this is what happens straight after the build if i run this command, WMIC /namespace:\\root\ccm path sms_client CALL TriggerSchedule "{00000000-0000-0000-0000-000000000001}" /NOINTERACTIVE, to force an inventory scan:

    C:\WINDOWS\system32>WMIC /namespace:\\root\ccm path sms_client CALL TriggerSched
    ule "{00000000-0000-0000-0000-000000000001}" /NOINTERACTIVE
    Executing (sms_client)->TriggerSchedule()
    ERROR:
    Description = Not found

    Try that command 5-20mins later and it works:

    C:\WINDOWS\system32>WMIC /namespace:\\root\ccm path sms_client CALL TriggerSched
    ule "{00000000-0000-0000-0000-000000000001}" /NOINTERACTIVE
    Executing (sms_client)->TriggerSchedule()
    Method execution successful.
    Out Parameters:[abstract]
    class __PARAMETERS
    {
    [out] uint32 ReturnValue;
    };

    This is my big headache !

You must be logged in to reply to this topic.