这里是 Powershell 新手。很抱歉,如果有人过去问过这个问题,但我可以找到一个与 Abaqus 相关的示例。所以我计划做的是通过 Powershell 自动化依次运行几个作业,我的问题是 Start-Process 同步运行 Abaqus,它不会继续执行下一行代码,这将使脚本休眠 1杀死它之前一个小时,直到工作完全完成。然后我了解到我可以使用 Start-Job 异步运行 Abaqus,但是我没有运气通过下面的脚本得到任何东西。我在这里做错了什么吗?看来我的脚本块是错误的。
非常感谢!
#
# Script.ps1
#
# Choose a job file containing all the jobs
Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
InitialDirectory = [Environment]::GetFolderPath('Desktop')
Filter = 'Text File (*.txt)|*.txt|Batch File (*.bat)|*.bat'
}
$Null = $FileBrowser.ShowDialog()
# Assign job files directory to path
$myPath = Split-Path -Parent $FileBrowser.FileName
$jobsFile = Split-Path -Leaf $FileBrowser.FileName
Set-Location -Path $myPath -PassThru
Write-Host "Selected jobs file is <$jobsFile> located in $myPath" | Out-Host
#echo "My selected path is $($myPath)"
#Write-Host "My selected path is $($myPath)"
$jobsList = Get-Content -Path $myPath'\'$jobsFile
$jobCPUs = '-cpus ' + 4
foreach($job in $jobsList)
{
#Write-Host "Job = $job"
$jonINP = $job + ".inp"
$jobCommand = 'job=' + $job + ' -input ' + $job + ' ' + $jobCPUs #+ ' interactive'
Write-Host "Job Command is < $jobCommand >"
#$myProcess = Start-Process Abaqus -NoNewWindow -Wait -ArgumentList $jobCommand -PassThru
#Start-Sleep -Seconds 10
#Stop-Process Abaqus
#$p = Get-Process -Name $myProcess
#Stop-Process -InputObject $p
$scriptBlock = {
Start-Process Abaqus -ArgumentList $jobCommand
}
$myJob = Start-Job -ScriptBlock $scriptBlock
#Start-Job Abaqus -NoNewWindow -ArgumentList $jobCommand -PassThru
Start-Sleep -Seconds 15
$myJob | Remove-Job
}
Start-Process 未成功,因为作业是同步运行的。尝试将 Start-Job 作为后台作业执行,但没有成功让它运行。
想必答案很简单:
$jobs = foreach ($job in $jobsList) {
$jonINP = $job + '.inp' # <= This variable is assigned but never used
$jobCommand = 'job=' + $job + ' -input ' + $job + ' ' + $jobCPUs
Start-Job -ScriptBlock { Abaqus $using:jobCommand }
}
$jobs | Receive-Job -Wait -AutoRemoveJob
您不会从使用
Start-Process
启动的进程中获得任何输出,因此应放弃该选项。您还缺少 $using:
范围修饰符来引用工作范围之外的变量。