locked
Lengthy process inside Foreach looping RRS feed

  • Question

  • Hi,

    In order to check whether some processes have completed or not, I created a foreach loop as follows:

    $sources=@("Source1","Source2","Source3","Source4","Source5","Source6","Source7","Source8","Source9","Source10","Source11","Source12","Source13","Source14","Source15","Source16");
    
    foreach($source in $sources){
        # check whether timer job has completed or not
        do{
            Write-Host "Processing " $source
            Start-Sleep 480
    
            # Get new reference to the process, i.e.
            # $process = Get-xxxxxx -Name $source
    
        }
        while ($process.NotCompleted)
        $process = $null; 
    }

    It was working fine until Source11 (took about 16 hours). After that, there is no output on the screen and it does not start Source12. I waited for quite sometime but nothing happened afterwards. Followings are the outputs:

    Processing Source 10
    Processing Source 11
    Processing Source 11
    Processing Source 11
    Processing Source 11
    Processing Source 11

    Did anyone experience this kind of issue before? It seems that it "lost" the iterator -- would it be safe if I use "for(initialization; condition; repeat)" statement rather than foreach loop?

    Thank you


    • Edited by RWin97 Sunday, January 20, 2013 3:42 AM
    Sunday, January 20, 2013 3:40 AM

Answers

  • Hi,

    I assume the 'sources' mean remote computers of some sort.

    I have a couple of scripts working remotely with servers (1000+ servers usually) and in reality, these scripts do not work properly if there's no pre-checkouts implemented, like ping check, wmi check...etc. because there's always 1 or 2 servers which do not respond or have some sort of issue which would slow down the script.

    I have a post on my blog about this, check it out:
    http://tompaps.blogspot.hu/2012/12/useful-script-functions-powershell.html

    The pingcheck and checkwmi functions have timeouts in them to make sure the script goes to the next host in a timely manner.

    If it's not about remote hosts, then just put some logging into the loop and into the Get-xxxxxx function so you can see where it gets hung. Same blog post, writelog function.

    Hope it helps.


    ---
    tompa
    http://tompaps.blogspot.com

    • Proposed as answer by CountryStyle Sunday, January 20, 2013 10:52 PM
    • Marked as answer by Yan Li_ Thursday, January 24, 2013 2:44 AM
    Sunday, January 20, 2013 6:42 PM

All replies

  • Hi,

    I assume the 'sources' mean remote computers of some sort.

    I have a couple of scripts working remotely with servers (1000+ servers usually) and in reality, these scripts do not work properly if there's no pre-checkouts implemented, like ping check, wmi check...etc. because there's always 1 or 2 servers which do not respond or have some sort of issue which would slow down the script.

    I have a post on my blog about this, check it out:
    http://tompaps.blogspot.hu/2012/12/useful-script-functions-powershell.html

    The pingcheck and checkwmi functions have timeouts in them to make sure the script goes to the next host in a timely manner.

    If it's not about remote hosts, then just put some logging into the loop and into the Get-xxxxxx function so you can see where it gets hung. Same blog post, writelog function.

    Hope it helps.


    ---
    tompa
    http://tompaps.blogspot.com

    • Proposed as answer by CountryStyle Sunday, January 20, 2013 10:52 PM
    • Marked as answer by Yan Li_ Thursday, January 24, 2013 2:44 AM
    Sunday, January 20, 2013 6:42 PM
  • A time-out and multi threading get my vote.

    I also make sure a computer is a live before doing anything with it.

    Sunday, January 20, 2013 10:51 PM