locked
Calling powershell.exe from Start-Process with a scriptblock RRS feed

  • Question

  • Hey Folks,

    I am working on a piece of code where I need to call a .ps1 script to do a background job and need the job to continue working while the .ps1 exits. I tried to make this work with Start-Process powershell.exe -ArgumentList "-Command  `"&{$ScriptBlock}`"" where I defined my whole process in the $ScriptBlock. Anyhelp on this is appreciated

    It terminates with an error (I can see this when i do a -NoExit)

    The string is missing the terminator: ".
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordEx
       ception
        + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString


    If you find this helpful, kindly mark as answer. If you have any queries, please post back as a reply. Will look forward to your feedback. Thanking You Soumyajyoti Biswas

    • Moved by Bill_Stewart Friday, July 27, 2018 8:15 PM Unanswerable drive-by question
    Friday, April 27, 2018 10:49 PM

All replies

  • Standard jobs will not work after PS exits.  Use "Wait-Job" to prevent premature exit.

    Errors in this scriptblock are preventing execution.


    \_(ツ)_/


    • Edited by jrv Friday, April 27, 2018 11:20 PM
    Friday, April 27, 2018 11:19 PM
  • You can try 

    $credentials = Get-Credential
    
    Invoke-Command -ComputerName "Your Computer" -Credential $credentials -FilePath "your_path\script.ps1"

    Tuesday, May 15, 2018 9:31 AM