Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

Powershell, converting a function to be called as a job

I have this function that I would like to pass as a start-job call.

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
egedit.exe -ArgumentList @('/s', $TempRegFile) -PassThru
do { Start-Sleep -Seconds 1 } while (-not $p.HasExited)    
Remove-Item -Path $TempRegFile -Force
}

As of now I call this function using two parameters, which works fine.

Write-Registry -RegFileContents $regFileContents -UserSid $userid

But I'd like to call it as a Start-Job; and can't seem to find how to do it properly.

If someone can help me, thanks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since a function and a scriptblock are the same thing:

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

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

1
2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...