【问题标题】:Powershell, converting a function to be called as a jobPowershell,将函数转换为作业
【发布时间】:2021-01-08 23:29:46
【问题描述】:

我有这个函数,我想将它作为开始工作调用传递。

function Write-Registry {
param($RegFileContents, $UserSid)
$TempRegFile = Get-TempRegFilePath
$regFileContents = $regFileContents -replace 'HKEY_CURRENT_USER', "HKEY_USERS\$userSid"
$regFileContents | Out-File -FilePath $TempRegFile    
$p = Start-Process -FilePath C:\Windows\regedit.exe -ArgumentList @('/s', $TempRegFile) -PassThru
do { Start-Sleep -Seconds 1 } while (-not $p.HasExited)    
Remove-Item -Path $TempRegFile -Force
}

到目前为止,我使用两个参数调用此函数,效果很好。

Write-Registry -RegFileContents $regFileContents -UserSid $userid

但我想将其称为 Start-Job;并且似乎无法找到正确的方法。

如果有人可以帮助我, 谢谢。

【问题讨论】:

  • This might be relevant 如果你想在你的函数中添加一个-AsJob 参数
  • 谢谢@js2010!还有 Mathias 的足智多谋的阅读。
  • 不客气!如果@js2010 的回答解决了您的问题,请考虑通过单击左侧的复选标记将其标记为“已接受”:-)

标签: powershell


【解决方案1】:

因为函数和脚本块是一回事:

function hi ($a, $b) { echo $a $b }                         

start-job $function:hi -args 1,2 | Receive-Job -wait -auto

1
2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多