简体   繁体   中英

How do I pass multiple paramaters to PowerShell Start-Job to run a process in the background?

I know there are many examples out there that are similar to mine. I have tried many with no real success. I simplified my code to the basic issue in the hopes that someone could point me in the right direction.

function Send-DBBackupToS3
{
  param(
    [Parameter(Mandatory=$true)][string]$p1, 
    [Parameter(Mandatory=$true)][string]$p2, 
    [Parameter(Mandatory=$true)][string]$p3
  )
  try
  { 
    Write-Host "starting process..."
    $TransferAppExe = $p1
    $arguments = '-OnDiskPath', $p2, '-NotificationEmailAddress', $p3
    $ps = Start-Process -FilePath $TransferAppExe -ArgumentList $arguments -Wait -PassThru               
  }
  catch
  {
      # get error record
      [Exception]$e = $_

      # retrieve information about runtime error
      $info = [PSCustomObject]@{
          Exception = $e.Exception.Message
          Reason    = $e.CategoryInfo.Reason
          Target    = $e.CategoryInfo.TargetName
          Script    = $e.InvocationInfo.ScriptName
          Line      = $e.InvocationInfo.ScriptLineNumber
          Column    = $e.InvocationInfo.OffsetInLine
          }
      # output information. Post-process collected info, and log info (optional)
      $info
  }        
}
function Start-DBCopyAndTransfer
{        
    param(     
      [Parameter(Mandatory)]   
      [string]$AppPath,
      [Parameter(Mandatory)]
      [string]$UploadFilePath,
      [Parameter(Mandatory)]
      [string[]]$EmailAddress
    )               
    Write-Host "calling job..."
    Start-Job -Name Send2S3 -ScriptBlock {param($p1, $p2, $p3) Send-DBBackupToS3 -p1 $p1 -p2 $p2 -p3 $p3} -ArgumentList $AppPath,$UploadFilePath,$EmailAddress          
}
Clear-Host

Write-Host "calling function..."
Start-DBCopyAndTransfer -AppPath "C:\FileToS3.exe" -UploadFilePath "C:\chron.cti" -EmailAddress "4321@gmail.com"

I am trying to pass into a Start-Process cmdlet the parameters needed to run the .exe.

The results I are as follows:

calling function...
calling job...

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
1      Send2S3         BackgroundJob   Running       True            localhost            param($p1, $p2, $p3) S...



PS C:\WINDOWS\system32> Get-Job

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
1      Send2S3         BackgroundJob   Failed        False           localhost            param($p1, $p2, $p3) S...



PS C:\WINDOWS\system32> 

I never see the Write-Host "starting process..." fire. If I take away the params(Hard code the values) from Send-DBBackupToS3 it works just fine. Thanks for your time!

You can pass multiple arguments as follows:

Start-Process -FilePath $TransferAppExe -ArgumentList $argument1, $argument2, $argument3 -Wait -PassThru

Or pass a list of arguments:

$arguments = $argument1, $argument2, $argument3
Start-Process -FilePath $TransferAppExe -ArgumentList $arguments -Wait -PassThru

Or pass a single string with all arguments:

Start-Process -FilePath $TransferAppExe -ArgumentList "$argument1 $argument2 $argument3" -Wait -PassThru

Also, you don't need to WaitForExit if you have the -Wait argument, as they do the same thing.

I hate to answer my own questions...However, if someone runs across this I want them to have the solution.

$func = {
  function Send-DBBackupToS3
  {
    param(
      [Parameter(Mandatory=$true)][string]$p1, 
      [Parameter(Mandatory=$true)][string]$p2, 
      [Parameter(Mandatory=$true)][string]$p3
    )
    try
    { 
      Write-Host "starting process..."
      $TransferAppExe = $p1
      $arguments = '-OnDiskPath', $p2, '-NotificationEmailAddress', $p3
      Start-Process -FilePath $TransferAppExe -ArgumentList $arguments -Wait -PassThru            
    }
    catch
    {
      # get error record
      [Exception]$e = $_

      # retrieve information about runtime error
      $info = [PSCustomObject]@{
          Exception = $e.Exception.Message
          Reason    = $e.CategoryInfo.Reason
          Target    = $e.CategoryInfo.TargetName
          Script    = $e.InvocationInfo.ScriptName
          Line      = $e.InvocationInfo.ScriptLineNumber
          Column    = $e.InvocationInfo.OffsetInLine
          }
      # output information. Post-process collected info, and log info (optional)
      $info
    }        
  }
}
function Start-DBCopyAndTransfer
{        
    param(     
      [Parameter(Mandatory)]   
      [string]$AppPath,
      [Parameter(Mandatory)]
      [string]$UploadFilePath,
      [Parameter(Mandatory)]
      [string[]]$EmailAddress
    )               
    Write-Host "calling job..."
    $job = Start-Job -Name Send2S3 -InitializationScript $func -ScriptBlock {param($p1, $p2, $p3) Send-DBBackupToS3 -p1 $p1 -p2 $p2 -p3 $p3} -ArgumentList $AppPath,$UploadFilePath,$EmailAddress          
    Receive-Job -Job $job
    Write-Host ('State: {0}' -f $job.State)
}
Clear-Host

Write-Host "calling function..."
Start-DBCopyAndTransfer -AppPath "C:\FileToS3.exe" -UploadFilePath "C:\chron.cti" -EmailAddress "4321@gmail.com"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM