I'm working on a PowerShell script that provides console output and restarts the system several times during the script's operation. As soon as it starts, it registers itself as a Scheduled Job so that it can pick up where it left off after restarts:
$JobArgs = @(
$ScriptPath
($ArgumentList -join ' ')
)
$JobScript = {
param(
[string] $ScriptPath,
[string] $Arguments
)
$PSArgs = @(
'-NoExit'
'-NoLogo'
'-NoProfile'
"-File $ScriptPath $Arguments"
)
Start-Process powershell.exe -ArgumentList $PSArgs -Wait
}
$AtLogonTrigger = New-JobTrigger -AtLogOn -User $LoginUser
$JobProperties = @{
'Name' = $JobName
'Trigger' = $AtLogonTrigger
'ScheduledJobOption' = New-ScheduledJobOption -RunElevated
'ScriptBlock' = $JobScript
'ArgumentList' = $JobArgs
}
$Job = Register-ScheduledJob @JobProperties -ErrorAction Stop
However, while this does do what I want, it does so in the background when I would prefer that the PowerShell window be visible. It doesn't seem to be an intrinsic of PowerShell either; simply starting notepad.exe
via Start-Process
provides a hidden notepad instance as well.
While I'm aware that I probably shouldn't be relying on console output, it's good enough for my purposes.
Is there a way to invoke a foreground process from a background job?