Share via


Passing parameter to Start-Job - How??

Question

Friday, December 9, 2016 9:52 PM

Hi all,

I can seem to figure out how to pass the variable down to the scriptblock?

The Script:

Function Asynchronous_Processing($asynFile,$asynParameter,$asynTimeSeconds,$asynJobName){
    If (Test-Path -Path "$asynFile" -PathType Leaf){
        Write-Host "Start-Process -FilePath ""$asynFile"" -ArgumentList ""$asynParameter"" -Wait"
        
        Start-Job -Name $asynJobName -ScriptBlock {
        #Start-Process -FilePath "$asynFile" -ArgumentList "$asynParameter" -Wait
        Start-Process -FilePath "C:\tmp\npp.7.2.2.Installer.exe" -ArgumentList "/S" -Wait
        }
    } Else {
        Write-Host "File missing: $asynFile"
    }
    

    for ($i=1; $i -le "$asynTimeSeconds"; $i++) {
    $i
    Start-Sleep -Seconds "1"
    }

    #Get-Job –Name $asynJobName | Stop-Job
    #Get-Job –Name $asynJobName | Receive-Job –Keep
    #Get-Job –Name $asynJobName | Remove-Job
}

Asynchronous_Processing -asynFile "C:\tmp\npp.7.2.2.Installer.exe" -asynParameter "/S" -asynTimeSeconds "15" -asynJobName "Notepad"

above work but it static 

I want to use this : #Start-Process -FilePath "$asynFile" -ArgumentList "$asynParameter" -Wait
the static way: Start-Process -FilePath "C:\tmp\npp.7.2.2.Installer.exe" -ArgumentList "/S" -Wait

How do i pass down variable from the function to to the start-job scriptblock

//Long

#LnQ¯\(ツ)_/¯

All replies (2)

Friday, December 9, 2016 10:17 PM ✅Answered

Start-Job -Name $asynJobName -ScriptBlock {
      Param(
           $param1,
           $param2
    )
        Start-Process -FilePath $asynFile -ArgumentList $$param1,$param2 -Wait
 } -ArgumentList $arg $arg2

\(ツ)_/


Saturday, December 10, 2016 11:32 AM

adopted above code and created this and it works.

Start-Job -Name $asynJobName -ScriptBlock { PARAM($asynFile,$asynParameter);
        Start-Process -FilePath "$asynFile" -ArgumentList "$($asynParameter)" -Wait
        } -ArgumentList $asynFile,$asynParameter

So the PARAM define the parameters allowed in the scriptblock. or?

What does the -ArgmentList do? link it together or?

The complete working script if other could use it.

Function Asynchronous_Processing($asynFile,$asynParameter,$asynTimeSeconds,$asynJobName){
    If (Test-Path -Path "$asynFile" -PathType Leaf){
        Write-Host "Start-Process -FilePath ""$asynFile"" -ArgumentList ""$asynParameter"" -Wait"
        Start-Job -Name $asynJobName -ScriptBlock { PARAM($asynFile,$asynParameter);
        Start-Process -FilePath "$asynFile" -ArgumentList "$($asynParameter)" -Wait
        } -ArgumentList $asynFile,$asynParameter
    } Else {
        Write-Host "File missing: $asynFile"
    }
    
    for ($i=1; $i -le "$asynTimeSeconds"; $i++) {
    $i
    Start-Sleep -Seconds "1"
    }
    Get-Job –Name "$asynJobName" | Stop-Job
    Get-Job –Name "$asynJobName" | Receive-Job –Keep
    Get-Job –Name "$asynJobName" | Remove-Job
}

Asynchronous_Processing -asynFile "C:\tmp\npp.7.2.2.Installer.exe" -asynParameter '/S' -asynTimeSeconds "10" -asynJobName "Notepad"

#LnQ¯\(ツ)_/¯