locked
How to have one powershell make a system call to another, without the first script waiting for the second to finish RRS feed

  • Question

  • Hi,

    I want to have one powershell script (script A, let's say) call another powershell script (script B, let's say) where script A doesn't need to wait for script B to finish before it executes.

    How do I go about doing this?

    Thursday, October 27, 2016 6:06 PM

Answers

  • This should do the trick.
    Start-Process powershell -ArgumentList "-NoExit -File c:\test.ps1",$arg1,$arg2,$arg3


    Thursday, October 27, 2016 6:41 PM
  • Remove the -noexit switch and the second window will disappear after running and completing.
    • Marked as answer by bronxbomber2k7 Thursday, October 27, 2016 7:23 PM
    Thursday, October 27, 2016 7:22 PM

All replies

  • One way:


    PS C:\> help about_Jobs
    


    -- Bill Stewart [Bill_Stewart]

    Thursday, October 27, 2016 6:33 PM
  • Thanks for the reply. I tried Start-Job already and it did not work. Here's what I had tried : 

    Script 1 : 

    param (
        [Parameter(Mandatory=$true)][string]$id,
        [Parameter(Mandatory=$true)][string]$recType
    )

    Add-Content c:\test\test1.txt "$($id),$($recType)"

    Start-Job -ScriptBlock { C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe C:\test\script2.ps1 -id $id -recType $recType }

    #Start-Sleep -s 10

    ========

    Script 2 :

    param (
        [Parameter(Mandatory=$true)][string]$id,
        [Parameter(Mandatory=$true)][string]$recType
    )


    Add-Content c:\test\test2.txt "$($id),$($recType)"

    ===============

    If I uncomment the Start-Sleep -s 10 from script1, I get 2 text files. If I leave it commented, I get one.

    Thursday, October 27, 2016 6:36 PM
  • This should do the trick.
    Start-Process powershell -ArgumentList "-NoExit -File c:\test.ps1",$arg1,$arg2,$arg3


    Thursday, October 27, 2016 6:41 PM
  • Thank you, that worked! It did have one side effect. When I run it, it opens up an empty powershell window. Is there any way to stop that?

    *EDIT*

    FWIW, adding the -NoNewWindow prevents the second text file from being created. So that's not the answer.

    Thursday, October 27, 2016 6:45 PM
  • Start-Job -ScriptBlock { C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe C:\test\script2.ps1 -id $id -recType $recType }

    You don't need to spin up a new copy of powershell.exe. You can run your script directly:


    $job = Start-Job { C:\test\script2.ps1 -id $id -recType $recType }


    -- Bill Stewart [Bill_Stewart]

    Thursday, October 27, 2016 6:58 PM
  • Hi Bill,

    That didn't work. I didn't get the second text file. Even if I uncommented the sleep line, it still didn't work.

    Thursday, October 27, 2016 7:01 PM
  • param ( [Parameter(Mandatory=$true)][string]$id, [Parameter(Mandatory=$true)][string]$recType ) invoke-command -ComputerName $env:COMPUTERNAME { Start-Process powershell -ArgumentList -NoNewWindow "-NoExit -File c:\script2.ps1",$arg1,$arg2,$arg3 } Add-Content c:\test\test1.txt "$($id),$($recType)"


    Could try this.
    Thursday, October 27, 2016 7:03 PM
  • That didn't work at all. Just hung, and didn't get any text files.

    *edit*

    here's the error msg:


    Connecting to remote server XXX failed with the following error message : The client cannot connect to
    the destination specified in the request. Verify that the service on the destination is running and is accepting
    requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly
    IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and
    configure the WinRM service: "winrm quickconfig". For more information, see the about_Remote_Troubleshooting Help
    topic.

    Thursday, October 27, 2016 7:06 PM
  • The problem is using variables in a scriptblock.

    You can work around this by creating the scriptblock using the Create method; e.g.:


    $scriptBlock = [ScriptBlock]::Create("C:\Test\Script2.ps1 $Id $RecType")
    Start-Job $scriptBlock
    

    By the way: Thanks for posting the short, minimal test case. This reduces the problem to its minimal form and makes it easy to reproduce. (Other forum readers, take note!)


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Bill_Stewart Thursday, October 27, 2016 7:23 PM
    Thursday, October 27, 2016 7:21 PM
  • Remove the -noexit switch and the second window will disappear after running and completing.
    • Marked as answer by bronxbomber2k7 Thursday, October 27, 2016 7:23 PM
    Thursday, October 27, 2016 7:22 PM
  • This did it. Thank you so much, you rock!
    Thursday, October 27, 2016 7:23 PM
  • No problem.  Happy to help.
    Thursday, October 27, 2016 7:25 PM
  • I recommend using Start-Job and a script block rather than spinning up another copy of powershell.exe. It will be faster and more efficient.

    -- Bill Stewart [Bill_Stewart]

    Thursday, October 27, 2016 7:27 PM