none
run application asynchronously using powershell RRS feed

  • Question

  • Hello all,

    I am trying to run an application asynchronously per each subfolder within a given directory, but I have not been able to create a script that behaves exactly as I want...

    This is what I have so far:

    $dir = dir C:\test | ?{$_.PSISContainer}
    
    foreach ($d in $dir){
    start-job -scriptblock {C:\test.exe /b $d.fullname}
    }

    Can you give me a hand to make it work and let me know why the $d.fullname is not being resolved by the full folder path?

    note: $d.fullname is being resolved if for example i use "echo"

    Many thanks in advance.

    Thursday, September 18, 2014 10:38 PM

Answers

  • Hello all,

    I am trying to run an application asynchronously per each subfolder within a given directory, but I have not been able to create a script that behaves exactly as I want...

    This is what I have so far:

    $dir = dir C:\test | ?{$_.PSISContainer}
    
    foreach ($d in $dir){
    start-job -scriptblock {C:\test.exe /b $d.fullname}
    }

    Can you give me a hand to make it work and let me know why the $d.fullname is not being resolved by the full folder path?

    note: $d.fullname is being resolved if for example i use "echo"

    Many thanks in advance.

    You guys like to gues about things. No.  Read TFM.  It is fullof info bout how this works.

    HELP Start-Job - Full

    read all of it.  Read all of the examples.

    The problem is right here:

    start-job -scriptblock {C:\test.exe /b $d.fullname}

    You cannot use variables in a scriptblock handed to a job.  It executes on a separate thread Which has no access to your session.

    Oh yes. Sessions are like waffles.  They are crispy and unmanageablt without syrup.

    Try this:

    start-job -scriptblock {C:\test.exe /b $args[0]} -ArgumentList $d.fullname

    'tis the reason for arguments.



    ¯\_(ツ)_/¯



    • Edited by jrv Friday, September 19, 2014 12:07 AM
    • Marked as answer by yeayu Friday, September 19, 2014 9:04 PM
    Thursday, September 18, 2014 10:58 PM

All replies

  • Hello all,

    I am trying to run an application asynchronously per each subfolder within a given directory, but I have not been able to create a script that behaves exactly as I want...

    This is what I have so far:

    $dir = dir C:\test | ?{$_.PSISContainer}
    
    foreach ($d in $dir){
    start-job -scriptblock {C:\test.exe /b $d.fullname}
    }

    Can you give me a hand to make it work and let me know why the $d.fullname is not being resolved by the full folder path?

    note: $d.fullname is being resolved if for example i use "echo"

    Many thanks in advance.

    You guys like to gues about things. No.  Read TFM.  It is fullof info bout how this works.

    HELP Start-Job - Full

    read all of it.  Read all of the examples.

    The problem is right here:

    start-job -scriptblock {C:\test.exe /b $d.fullname}

    You cannot use variables in a scriptblock handed to a job.  It executes on a separate thread Which has no access to your session.

    Oh yes. Sessions are like waffles.  They are crispy and unmanageablt without syrup.

    Try this:

    start-job -scriptblock {C:\test.exe /b $args[0]} -ArgumentList $d.fullname

    'tis the reason for arguments.



    ¯\_(ツ)_/¯



    • Edited by jrv Friday, September 19, 2014 12:07 AM
    • Marked as answer by yeayu Friday, September 19, 2014 9:04 PM
    Thursday, September 18, 2014 10:58 PM
  • One quibble. That parameter name is -Argumentlist, not -Arguments

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Thursday, September 18, 2014 11:40 PM
    Moderator
  • BNot a quibble. It makes all the difference.

    Thanks.


    ¯\_(ツ)_/¯

    Friday, September 19, 2014 12:07 AM
  • I was torn between typing -argument and -args

    Got stuck in the middle.


    ¯\_(ツ)_/¯

    Friday, September 19, 2014 12:08 AM

  • You cannot use variables in a scriptblock handed to a job.  It executes on a separate thread Which has no access to your session.

    Oh yes. Sessions are like waffles.  They are crispy and unmanageablt without syrup.

    ¯\_(ツ)_/¯

    Not quite, there is a way. 

    Contents of c:\Test.ps1  #demonstrates parameter is passed 

    param ($Str)
    add-content "C:\Test.txt" $Str

    Here is the main script passing the variable to the job.

    PS C:\Test> 
    Add-Content "C:\Test.txt" "Contents of Test.txt"
    
    $dir = dir C:\test | ?{$_.PSISContainer}
    foreach ($d in $dir){
    $FullName=$D.FullName
    "FullName=$FullName"
    Invoke-Expression "start-job -ScriptBlock {C:\Test.ps1 $FullName}"
    }
    Wait-Event -Timeout 3  # delay to allow for jobs to run
    Get-content "c:\Test.txt"
    
    FullName=C:\test\-Folder
    
    Id              Name            State      HasMoreData     Location             Command                  
    --              ----            -----      -----------     --------             -------                  
    414             Job414          Running    True            localhost            C:\Test.ps1 C:\test\-F...
    FullName=C:\test\Test1
    416             Job416          Running    True            localhost            C:\Test.ps1 C:\test\Test1
    FullName=C:\test\Test2
    418             Job418          Running    True            localhost            C:\Test.ps1 C:\test\Test2
    FullName=C:\test\Test3
    420             Job420          Running    True            localhost            C:\Test.ps1 C:\test\Test3
    Contents of Test.txt
    C:\test\-Folder
    C:\test\Test1
    C:\test\Test2
    C:\test\Test3
    


    Saturday, September 20, 2014 4:03 AM
  • No - don't make things up when you have PowerShell.  PS was designed to do all of these things.

    $j=start-job {"You passed $args[0]"}  -ArgumentList 9999
    $j | Receive-Job

    It is that simple.


    ¯\_(ツ)_/¯

    Saturday, September 20, 2014 6:26 AM