# urgent help on looping !

**URL:** <https://forums.powershell.org/t/urgent-help-on-looping/4101>\
**Category:** PowerShell Help\
**Created:** [April 23, 2015, 6:39pm UTC](https://forums.powershell.org/t/urgent-help-on-looping/4101 "2015-04-23T18:39:59Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![graham-beer](https://avatars.discourse-cdn.com/v4/letter/g/49beb7/32.png) [@graham-beer](https://forums.powershell.org/u/graham-beer)\
**Post date:** [April 23, 2015, 6:39pm UTC](https://forums.powershell.org/t/urgent-help-on-looping/4101/1 "2015-04-23T18:39:59Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![graham-beer](https://avatars.discourse-cdn.com/v4/letter/g/49beb7/32.png) [@graham-beer](https://forums.powershell.org/u/graham-beer)\
**Post date:** [April 23, 2015, 7:37pm UTC](https://forums.powershell.org/t/urgent-help-on-looping/4101/2 "2015-04-23T19:37:04Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![graham-beer](https://avatars.discourse-cdn.com/v4/letter/g/49beb7/32.png) [@graham-beer](https://forums.powershell.org/u/graham-beer)\
**Post date:** [April 23, 2015, 7:51pm UTC](https://forums.powershell.org/t/urgent-help-on-looping/4101/3 "2015-04-23T19:51:00Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [April 23, 2015, 7:57pm UTC](https://forums.powershell.org/t/urgent-help-on-looping/4101/4 "2015-04-23T19:57:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![graham-beer](https://avatars.discourse-cdn.com/v4/letter/g/49beb7/32.png) [@graham-beer](https://forums.powershell.org/u/graham-beer)\
**Post date:** [April 23, 2015, 8:04pm UTC](https://forums.powershell.org/t/urgent-help-on-looping/4101/5 "2015-04-23T20:04:21Z")

</div>

Matt you are a legend ! Thank you.

---

<div class="post-metadata">

**Author:** ![graham-beer](https://avatars.discourse-cdn.com/v4/letter/g/49beb7/32.png) [@graham-beer](https://forums.powershell.org/u/graham-beer)\
**Post date:** [April 23, 2015, 8:06pm UTC](https://forums.powershell.org/t/urgent-help-on-looping/4101/6 "2015-04-23T20:06:45Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [April 23, 2015, 8:27pm UTC](https://forums.powershell.org/t/urgent-help-on-looping/4101/7 "2015-04-23T20:27:14Z")

</div>

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)
```

---

<div class="post-metadata">

**Author:** ![graham-beer](https://avatars.discourse-cdn.com/v4/letter/g/49beb7/32.png) [@graham-beer](https://forums.powershell.org/u/graham-beer)\
**Post date:** [April 23, 2015, 8:35pm UTC](https://forums.powershell.org/t/urgent-help-on-looping/4101/8 "2015-04-23T20:35:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [April 23, 2015, 11:24pm UTC](https://forums.powershell.org/t/urgent-help-on-looping/4101/9 "2015-04-23T23:24:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![graham-beer](https://avatars.discourse-cdn.com/v4/letter/g/49beb7/32.png) [@graham-beer](https://forums.powershell.org/u/graham-beer)\
**Post date:** [April 24, 2015, 12:07am UTC](https://forums.powershell.org/t/urgent-help-on-looping/4101/10 "2015-04-24T00:07:16Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![graham-beer](https://avatars.discourse-cdn.com/v4/letter/g/49beb7/32.png) [@graham-beer](https://forums.powershell.org/u/graham-beer)\
**Post date:** [April 24, 2015, 12:22am UTC](https://forums.powershell.org/t/urgent-help-on-looping/4101/11 "2015-04-24T00:22:37Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:45pm UTC](https://forums.powershell.org/t/urgent-help-on-looping/4101/12 "2024-05-16T20:45:25Z")

</div>


