我知道有很多与我相似的例子。我尝试了许多没有真正成功的人。我将我的代码简化为基本问题,希望有人能指出我正确的方向。
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"
我正在尝试将运行.exe。
所需的参数传递给Start-Process cmdlet结果我如下:
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>
我从未见过写主机"启动过程......" 开火。如果我从 Send-DBBackupToS3 中删除params(硬编码值),它就可以正常工作。谢谢你的时间!
答案 0 :(得分:0)
您可以按如下方式传递多个参数:
Start-Process -FilePath $TransferAppExe -ArgumentList $argument1, $argument2, $argument3 -Wait -PassThru
或传递参数列表:
$arguments = $argument1, $argument2, $argument3
Start-Process -FilePath $TransferAppExe -ArgumentList $arguments -Wait -PassThru
或传递包含所有参数的单个字符串:
Start-Process -FilePath $TransferAppExe -ArgumentList "$argument1 $argument2 $argument3" -Wait -PassThru
另外,如果您有WaitForExit
参数,则不需要-Wait
,因为他们做同样的事情。
答案 1 :(得分:0)
我不想回答我自己的问题......但是,如果有人碰到这个问题,我希望他们有解决方案。
$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"