Help to Close a program after it finishes using PowerShell

Discussion in 'Scripting' started by chrisgun, Aug 19, 2017.

  1. chrisgun

    chrisgun MDL Novice

    Feb 28, 2012
    35
    2
    0
    Hi All,

    Thank you for reading. Im trying to create a power shell script that installs some exes. i have got it work to the point were it Runs Ninite. but once this finishes it hangs until the user clicks close. could any one help me with a command line to wait for ninites to finish then closes.

    <pre>

    # This starts the office install which doesnt need to be a part of the other command
    $p = New-Object System.Diagnostics.Process
    $pinfo = New-Object System.Diagnostics.ProcessStartInfo("C:\Install_File\Office.exe","");
    $p.StartInfo = $pinfo;
    $p.Start();
    $p.WaitForExit();
    Write-Host "end of ps1" + (Get-Date).DateTime


    # This starts Ninite and waits for it to be closed to carry on the script
    $p = New-Object System.Diagnostics.Process
    $pinfo = New-Object System.Diagnostics.ProcessStartInfo("C:\Install_File\Ninite.exe","");
    $p.StartInfo = $pinfo;
    $p.Start();
    $p.WaitForExit();
    Write-Host "end of ps1" + (Get-Date).DateTime

    # This deletes all the files
    # Remove-Item c:\Ninite.exe
    # Remove-Item c:\Office.exe
    Remove-Item C:\Install_File -Recurse
    </pre>
     
  2. mxman2k

    mxman2k MDL Developer

    Jun 20, 2007
    5,758
    19,298
    180
    There is:

    Code:
     Start-Process <pathtoexe>\<nameofFiletorun> -wait  
    which runs the file and waits until that program finishes, then carries on to the next powershell line.

    remove the -wait and powershell runs it and goes onto the next command without waiting.

    Im not a PS coder so only know basics.

    Very similar to the batch START /Wait way which i use a lot.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. chrisgun

    chrisgun MDL Novice

    Feb 28, 2012
    35
    2
    0
    Hi thank you for the reply I have tried this and fails.
    The command line I have works but at the end of the it running it does not close causing the script to freeze.

    I think there is two options
    1. When the program is idle it gets closed
    2.click option to click close

    But the problem is i have looked every were for help but have no idea. I beleave it can be done but believe I need some one more experience and I'm a beginner
     
  4. Michaela Joy

    Michaela Joy MDL Crazy Lady

    Jul 26, 2012
    4,071
    4,651
    150
    Powershell is not my area of expertise, but maybe it has something to do with ninite free versus ninite pro?

    https://ninite.com/help/features/silent.html

    Also, don't most programs designed to run in a CMD shell return a completion code of some kind?
    Is there a way to know this and wait for the code?

    These are just some thoughts. Hopefully, they'll help. :)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. chrisgun

    chrisgun MDL Novice

    Feb 28, 2012
    35
    2
    0
    Hi all

    Thank you for all your help i found a fix will a little coding added by me

    There software out there called AutoIT a person made a scripted that would run ninites but kept asking me to add the ninite exe. as i was looking for somthink to run at the begin of a fresh install this was not any good for me. After looking round i found that i could adapt this person script to do what i needed.

    So the finished script is

    Code:
    ; Tell the script to run the program
          Run("Ninite.exe", "", @SW_SHOWMAXIMIZED)
    
    ; Blocks any Mouse use
    BlockInput(1)
    
    ; Moves the mouse to a selected location
    MouseMove(0, 0)
    WinMove(WinWait("Preparing"), "", @DesktopWidth + 5, @DesktopHeight + 5)
    
    While 1
        WinSetTrans ( "Preparing", "", 255 )
        WinSetTrans ( "Ninite", "", 255 )
        $text = WinGetText("Ninite", "")
        If StringInStr($text, "Finished.",1) Then
            ExitLoop
        EndIf
        Sleep(500)
    WEnd
    
    ; Wait for 1 Seconds.
    Sleep(1000)
    ControlClick("Ninite", "", "[ID:2]")
    
    ; Enables mouse use
    BlockInput(0)
    it will run Ninites at this point it will block user input (mouse will not work) once it finishes it will select close and carry on.

    Please note that this will only work if UAC is off or and for preset up (as it get installed before a user is created)

    Ninite-Silent.exe
    Ninite-Silent.au3 (use this to change thinks to your liking and create new Ninite-silent.exe
    Ninite.exe (download new from there website and change the name to this and will work the same


    USE AT OWN RISK IT WORKS FOR ME BUT IM NOT A PROGRAMMER