none
PowerShell Script to launch a process and keep MDT waiting until it completes. RRS feed

  • Question

  • We use Radia for software distribution.  I need to kick-off a Radia "connect" so it will install software before ending my task sequence.

    The way it works, a command line is run to have it call back to the server.  It launches a few different processes, but RADPINIT.exe is the only one that is open for the duration of the package installs, and closes when complete, with a caveat.

    This RADPINIT process closes and reopens at the end each package, so my attempts to make MDT wait until RADPINIT closes have not worked.

    I got some help from jrv in the Scripting Guy forums, but that forum isn't very kind to scripting novices.  I was essentially kicked out and told to go learn PowerShell better. :)

    This is the script he helped me with:

    $CMD =  'c:\Program Files (x86)\Novadigm\radntfyc.exe'
    $arg1 =  'LOCALHOST'
    $arg2 =  'Radskman.exe'
    $arg3 =  'cat=prompt,ulogon=n,hreboot=y,context=m,mname=radia,dname=SOFTWARE,ip=radia.domain.com,port=3464,uid=$MACHINE,startdir=SYSTEM,ind=y,cop=y,log=software_connect.log'
    $Retries = 5
    
    
    & $CMD $arg1 $arg2 $arg3
    
    Sleep 5
    $Retries = 5
    while(1){ 
         if(Get-process Radpinit)
         {sleep 5}
         else{exit}
    }

    This script has a couple of flaws and if anyone can help me make some modifications I would appreciate it.

    What this script does:

    1. Starts a While loop.  

    2. If Get-Process finds Radpinit it sleeps for 5, then checks again, and again, etc.

    3. If it isn't running, it launches my CMD, sleeps 5, then exits.

    The first problem is that it launches my CMD AFTER running the while loop (Function before CMD / non-liner script processing).

    The 2nd problem is that it doesn't have any method of dealing with the brief period when RADPINIT may be between packages.

    What I need it to do:

    Essentially, I need to put the While loop, inside a Retry loop of some kind.  But I can't figure it out.  Every example that seems similar doesn't work the way I expect when I try to adapt it to this script.

    Run my CMD first, then sleep for 5. (Or just let the proposed retry loop take care of this)

    Start the while loop.  If Get-Process finds RADPINIT, sleep 5 and start over.  It it DOESN'T find RADPINIT running,  sleep 5 and try again 3 times.  If found on one of those 3 retries, restart the retry counter.

    So, for ex:

    1. Script Start

    2. Run Radia CMD

    3. Sleep 5

    4. Start While Loop, Get-Process = RADPINIT running, sleep 5.

    5. Restart While Loop, Get-Process = RADPINIT running, sleep 5.

    6. Restart While Loop, Get-Process = RADPINIT = NOT running, sleep 5.

    7. Retry 1 in While Loop, Get-Process = RADPINIT running, sleep 5.

    8. Restart While Loop, Get-Process = RADPINIT = running, sleep 5.

    9. Restart While Loop, Get-Process = RADPINIT = NOT running, sleep 5.

    10. Retry 1 in While Loop, Get-Process = RADPINIT NOT running, sleep 5.

    11. Retry 2 in While Loop, Get-Process = RADPINIT NOT running, sleep 5.

    12. Retry 3 in While Loop, Get-Process = RADPINIT NOT running, EXIT.

    Any assistance appreciated!

    -Matt


    There's no place like 127.0.0.1

    Thursday, November 12, 2015 8:29 PM

All replies

  • Do you know how many times it starts a new radpinit process?

    If so, you could write something like this:

    & $cmd $arg1 $arg2 $args
    # Get process radntfyc and wait until it stops
    $process = Get-Process -Name *radntfyc*
    $process.WaitForExit()
    
    # number of retries
    $nbRetries = 10
    do {
      $process = (Get-Process -Name *Radprint*)
      $process.WaitForExit()
      $count--
    } while ($nbRetries -ge 0)
    
    
    Code provided AS IS

    • Edited by Gaëtan Hermann Thursday, November 12, 2015 9:18 PM
    • Proposed as answer by Gaëtan Hermann Thursday, November 12, 2015 9:20 PM
    • Unproposed as answer by Matt5150 Tuesday, December 8, 2015 8:43 PM
    Thursday, November 12, 2015 9:08 PM
  • Do you know how many times it starts a new radpinit process?

    If so, you could write something like this:

    & $cmd $arg1 $arg2 $args
    # Get process radntfyc and wait until it stops
    $process = Get-Process -Name *radntfyc*
    $process.WaitForExit()
    
    # number of retries
    $nbRetries = 10
    do {
      $process = (Get-Process -Name *Radprint*)
      $process.WaitForExit()
      $count--
    } while ($nbRetries -ge 0)
    
    Code provided AS IS

    It's completely dependent on how many packages are tied to that computer.  Could be 1, could be 50 or more.

    From testing, it takes a second or two for the initial RADPINIT process to start. Then it will run for the duration of the package install, then stop and launch another one for the next package less than a second later.  But that brief absence was causing problems.

    That's where the idea putting the loop, in a loop with retries, came from.


    There's no place like 127.0.0.1

    Thursday, November 12, 2015 10:14 PM
  • Is it possible to get number of packages to be installed? WMI or another interface provided? PowerShell? Where is the information about that.

    I'm sorry I don't know radia software...

    If you can't retrieve number of package you must try more times... Perhaps 1000 times. This script doesn't wait forever. If no radpinit processes are running, it takes maximum 1000 seconds (about 16 minutes) to finish.

    $CMD =  'c:\Program Files (x86)\Novadigm\radntfyc.exe'
    $arg1 =  'LOCALHOST'
    $arg2 =  'Radskman.exe'
    $arg3 =  'cat=prompt,ulogon=n,hreboot=y,context=m,mname=radia,dname=SOFTWARE,ip=radia.domain.com,port=3464,uid=$MACHINE,startdir=SYSTEM,ind=y,cop=y,log=software_connect.log'
    & $cmd $arg1 $arg2 $args # Get process radntfyc and wait until it stops $process = Get-Process -Name *radntfyc* -ea SilentlyContinue if ($process.Count -gt 0) { $process.WaitForExit(); Sleep -s 1; } # number of retries $nbRetries = 1000 # Wait until radpinit process stop a given number of times do { try { $process = (Get-Process -Name *Radpinit* -ea SilentlyContinue) if ($process.Count -gt 0) { $process.WaitForExit(); Sleep -s 1; } } catch { # Continue } $nbRetries-- } while ($nbRetries -gt 0)

    Thursday, November 12, 2015 10:39 PM
  • Not that I can see.  The AD groups packages are tied to are not named where one could collect the total easily through additional scripting.

    There's no place like 127.0.0.1

    Tuesday, December 8, 2015 8:44 PM
  • Not that I can see.  The AD groups packages are tied to are not named where one could collect the total easily through additional scripting.

    There's no place like 127.0.0.1


    Have you tried the code I provided (Thursday, November 12, 2015 10:46 PM)?
    Tuesday, December 15, 2015 8:15 PM