Powershell, converting a function to be called as a job

Viewed 38

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\regedit.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.

1 Answers

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
Related