none
Assistance with creating a break in a loop RRS feed

  • Question

  • Hi everyone, I have a loop script I created in a bat file to continue once a certain cert gets installed onto a laptop. This is mainly to automate the process due to sometimes the laptops not grabbing the scripts with either something on the laptop causing it or something on the server side. With the amount of people that go through imaging our laptops most people will not manually look to see if it gets installed which is why I created the loop but if there is a problem with either the laptop or server I want to be able to have an option to get out of the loop (also if possible to send our a message to an email address stating the computername that the issue happened on but that is just a bonus). Maybe have it try a certain amount of times and then give a prompt asking do you want to skip this or try another certain amount of times.
    Here is the loop I created:

    :certloop
    powershell.exe Get-ChildItem -Path cert:\LocalMachine\My -Recurse >c:\setup\certs.txt
    type c:\setup\certs.txt |find "%COMPUTERNAME%"
    if %errorlevel%==0 goto :sccm
    goto :certloop

    Any help would be greatly appreciated!
    Tuesday, November 25, 2014 3:08 PM

Answers

All replies

  • First I would recommend you try to construct this in Powershell.  You can use the exe to run commands but you're not really getting the full benefit of PS or it's object control the way you used it here. 

    As I see it you seem to be looking for a %computername% certificate, so lets take a look first at how you're getting your certificates on the system.  Instead of running the passthrough command into the exe, consider putting it into an array :

    $certs = get-childitem cert:\localmachine\my

    Now that we have our items, we can check to see if they exist or not :

    (!($certs)) #CERTS DO NOT EXIST
    ($certs)    #CERTS DO EXIST


    Now seeing as you have an array you can check for if a certificate exists on the system -- as you said you're checking for existence -- we can then create conditional statements for these states.

    do{
    #SLEEP 5 SECONDS
    start-sleep -s 5
    }
    while (!($certs))

    And now you have a basic loop to sleep 5 seconds while certs do not exist.  This is extremely basic and not meant to be a turnkey solution for you, it's meant moreso to help you understand how to use get-childitem to your advantage instead of just wrapping CMD around it :)  Hope this helps a bit.

    Ryan 

    Tuesday, November 25, 2014 3:33 PM
  • Thanks this is useful info but I am actually using this as a stop gap rather then looking to convert things into powershell. When we move to Windows 10 I am going to do an overhaul and start from scratch and just work with powershell from the start as I only inherited it and just making improvements on top of what is already there which is why I need to stick with the batch file if possible.
    Tuesday, November 25, 2014 3:53 PM
  • Everything you're asking for is easier in Powershell, though if you want to stick to batch, you should start here on how to create loops - http://ss64.com/nt/syntax-loops.html and how to create a count variable in batch - http://ss64.com/nt/for.html .  Good luck.
    • Edited by MrBrooks Tuesday, November 25, 2014 4:08 PM
    • Proposed as answer by jrv Tuesday, November 25, 2014 4:53 PM
    • Marked as answer by Bill_StewartModerator Tuesday, December 30, 2014 2:40 AM
    Tuesday, November 25, 2014 4:06 PM
  • Actually managed to create the following after some hit or miss testing and works like a charm for anybody who would ever need it

    set /a x=1
    :certloop
    SLEEP 60
    powershell.exe Get-ChildItem -Path cert:\LocalMachine\My -Recurse >c:\setup\certs.txt
    type c:\setup\certs.txt |find "%COMPUTERNAME%"
    if %errorlevel%==0 goto :sccm
    if %x% lss 5 (
    echo %x%
    set /a x+=1
    goto :certloop)
    
    set /a x=1
    echo The certificate is not being installed properly and SCCM cannot install properly after trying for 5 minutes.
    
    :start
    echo.
    echo	1 - Yes
    echo	2 - No
    echo.
    
    rem For user input
    set choice=
    set /p choice=Would you like to retry?: 
    if not '%choice%'=='' set choice=%choice:~0,1%
    if '%choice%'=='1' goto certloop
    if '%choice%'=='2' goto sccm
    
    ECHO "%choice%" is not valid, try again
    ECHO.
    pause
    cls
    goto start

    Wednesday, November 26, 2014 3:17 PM