#Running a powershell script in the background

1 messages · Page 1 of 1 (latest)

split dock
#

without waiting for output etc...

mild dagger
#

Start-ThreadJob

split dock
#

#powershell-help message

mild dagger
#

you can run whole script in it

#

in a different thread

split dock
#

in 1 command

#

with no user interaction

graceful current
#

Start-Job or Start-ThreadJob are the only ways to run in the background.

delicate kelp
#

there is no user interaction in the screenshot though

#

there's progress...

graceful current
#

Set-ExecutionPolicy RemoteSigned to enable scripts

delicate kelp
#

but if you push it all into the background, and let PS end, it'll all be torn down

graceful current
#

Or, make sure you start PowerShell with -ExecutionPolicy RemoteSigned

#

(or use "Unrestricted" in place of RemoteSigned)

split dock
#

also Start-ThreadJob : The term 'Start-ThreadJob' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

split dock
delicate kelp
#

sure

#

but putting it in the background is no fix for this

#

you need to figure out why it stays, assuming you never get back control of the console

split dock
#

well id assume the startup script makes the setup wait

graceful current
#

If you're on Windows PowerShell, Start-ThreadJob is in the Microsoft.PowerShell.ThreadJob module now, but you have to install it

split dock
#

so maybe making it be in the background

#

then sleeping for the duration of the setup

#

and then making it run would work

delicate kelp
#

by magic?

graceful current
#

There's nothing in that script that would sleep

delicate kelp
#

putting what you have into the background is no solution for it hanging.

graceful current
#

I mean, obviously Add-WindowsCapability can take some time ...

#

But it wouldn't lock up forever

split dock
#

windows doesn't setup as well

#

...

delicate kelp
#

I don't understand what you mean by that

split dock
#

and nothing happened ...

#

its just like this

graceful current
#

Right, so my point is ... it's getting locked up on something, and you need to add some lines like: Write-Host "I got as far as line 4" to it

split dock
#

for hours

delicate kelp
#

so why do you think making it run in the background is a fix for this?

#

is that triggered by autounattend or something?

mild dagger
#

it will only make it worse 😛

#

harder to debug

graceful current
#

I can't tell which thing is causing it to lock up

split dock
#

here is why I think it will work

mild dagger
#

I dont think its an issue with the script

#

the code looks good

split dock
#

if I run it as is, it just doesn't work. if I run it after setup manually, it does.
Running this in the background:

#ps1
Start-Sleep -Seconds 900
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Start-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'
if (!(Get-NetFirewallRule -Name ""OpenSSH-Server-In-TCP"" -ErrorAction SilentlyContinue | Select-Object Name, Enabled)) {
    Write-Output ""Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it...""
    New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
} else {
    Write-Output ""Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists.""
}
Set-MpPreference -DisableRealtimeMonitoring $true

will wait for the setup to complete and then run the program, by that the two won't collide

mild dagger
#

I think its an issue with the server

split dock
#

wdym?

delicate kelp
#

I mean it will not wait for setup to complete then run the program

split dock
#

setup takes 800 seconds

#

...

delicate kelp
#

if you push it into the background it still runs immediately, you just don't see it

split dock
#

well the commands that get it stuck

#

will run.,..

delicate kelp
#

if you need to wait for setup to complete then you need to wait for setup to complete before starting your other stuff

split dock
#

lieka fter

graceful current
#

What "Setup" are we talking about? Windows first-run stuff?

#

Or some other script you're running?

split dock
delicate kelp
#

unattended installation of Windows?

split dock
#

don't u need to setup unattended before in the iso file?

#

its an rdp from a cloud provider

delicate kelp
#

I'm not sure that's a description of a thing...

#

how do you get that cmd.exe prompt while windows is still "Getting ready"?

split dock
#

how would I run this without user input?

mild dagger
#

I think I still dont understand whats the actual problem

delicate kelp
#

Set-ExecutionPolicy RemoteSigned -Confirm:$false

#

either that or -Force, cannot remember which

split dock
#

Set-ExecutionPolicy Bypass -scope Process -Force

graceful current
#

I think -Force

split dock
#

works

#
#ps1
Set-ExecutionPolicy Bypass -scope Process -Force
$cmd = "#ps1
Start-Sleep -Seconds 900
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Start-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'
if (!(Get-NetFirewallRule -Name ""OpenSSH-Server-In-TCP"" -ErrorAction SilentlyContinue | Select-Object Name, Enabled)) {
    Write-Output ""Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it...""
    New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
} else {
    Write-Output ""Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists.""
}
Set-MpPreference -DisableRealtimeMonitoring $true"

$cmd | Out-File -FilePath script.ps1
Start-Job -FilePath .\script.ps1
graceful current
#

If you do -Scope Process you probably don't need force

#

But it won't stick

split dock
#

will the start job run this as admin?

delicate kelp
#

it's probably already elevated since DISM isn't kicking you out right away; but since you don't disclose what cloud provider or how the original process is started it's not possible to definitively say

#

moving it into start job will certainly "work" in the sense that it will start a job

graceful current
#

If you're elevated, it is

delicate kelp
#

it won't magically fix anything else though

split dock
#

ig we will find out in 1000 seconds

#

lol

graceful current
#

Actually

#

Maybe that's not true 😕

split dock
#

wdym?

graceful current
#

Oh, nothing. Just not sure about the elevation

#

But it turned out my check was a typo

mild dagger
#

jobs run with the same permission as the parent process

graceful current
#

Unless you remote

mild dagger
#

yeah thats always elevated

#

was talking about Start-Job and Start-ThreadJob

split dock
#

made it send webhooks so I know if something failed and where

#

not the best way to do it but yeah lol

split dock
#

it seems like it never runs the
Start-Job -FilePath .\script.ps1

#

any idea on why that would happen?

delicate kelp
#

do you feel like you're going in circles yet?

#

Start-Job will start a job... but if the process that started it ends, so does the job

delicate kelp
#

change when you run the script in the first place

split dock
#

wdym?

#
#ps1
Set-ExecutionPolicy Bypass -scope Process -Force
$cmd = "#ps1
Start-Sleep -Seconds 900
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Start-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'
if (!(Get-NetFirewallRule -Name ""OpenSSH-Server-In-TCP"" -ErrorAction SilentlyContinue | Select-Object Name, Enabled)) {
    Write-Output ""Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it...""
    New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
} else {
    Write-Output ""Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists.""
}
Set-MpPreference -DisableRealtimeMonitoring $true"

$cmd | Out-File -FilePath script.ps1
Start-Job -FilePath .\script.ps1
#

what should I change in here?

delicate kelp
#

something so it runs after setup is finished rather than during

split dock
#

u think that would work?

delicate kelp
#

no, not really

#

not because the idea won't work, but because the problem and running context isn't very well defined

split dock
#

ehhhhhhhhh

#

do u know of any commands that will keep the process alive after the main program is finished?

#

@delicate kelp does Start-Thread keep running after main program has finished?

ancient crater
#

so much assumptions while little to no understanding...

split dock
#

ik im a noobie

ancient crater
#

stop assuming, process what is being said. Don't ask leading questions.

delicate kelp
#

you need to do this another way

graceful current
#

the way to keep it running is to run powershell ....

#

😉

split dock
#

wdym

#

do I just compile a py script with a subprocess to do this ;-;

#

💀

#

and wget and run

graceful current
#

uh

#

you don't need to run python

#

you can run powershell

#

The point is, if you want it to stay running, you need to leave it running

#

If your problem is that your whole script is just Start-Job ...
Then pass that to Wait-Job

#

You can't just put it on a background thread and then exit

#

because exiting kills the background

split dock
#

well waiting stops it and the setup

graceful current
#

Are you running two jobs?

#

Is the setup in PowerShell?

split dock
graceful current
#

OK, let me ask a different way: how does waiting stop the setup?

split dock
#
#ps1
Set-ExecutionPolicy Bypass -scope Process -Force
$cmd = "#ps1
Start-Sleep -Seconds 900
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Start-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'
if (!(Get-NetFirewallRule -Name ""OpenSSH-Server-In-TCP"" -ErrorAction SilentlyContinue | Select-Object Name, Enabled)) {
    Write-Output ""Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it...""
    New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
} else {
    Write-Output ""Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists.""
}

Set-MpPreference -DisableRealtimeMonitoring $true
"

$cmd | Out-File -FilePath script.ps1
Start-Sleep -Seconds 5
Start-Job -FilePath .\script.ps1
Wait-Job -Id 1
#

its just an initial script that starts like 5m into the rdp setup

graceful current
#

Can you not put this into Packer or DSC or whatever you use to initialize the box?

split dock
#

nope

#

its stupid

#

I would've just made a complete screenshot of what I need and load it on each server

graceful current
#

Seems like your only option would be to create a scheduled task to run it in then

split dock
#

how do I do that?

#

im a full on windows noob

graceful current
#

That way, you can schedule it to run "later"

#

I mean, that ... or figure out what's actually causing the hang up

split dock
#

seems like its the

#

Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

#

that its getting stuck on

graceful current
#

It makes sense that if other feature installs are running, that would cause problems

#

Well, since it sounds like you're in Windows PowerShell, you can just use Register-ScheduledJob

ancient crater
#

This is a Windows 10 vm right?

split dock
#

yes

graceful current
#

$Later = New-JobTrigger -Once -At ([DateTime]::Now.AddMinutes(5))

ancient crater
#

How is this script being fed to the machine?

graceful current
#

Register-ScheduledJob -Name AddOpenSSH -Trigger $later -ScriptBlock { ... }

#

and shove your $cmd into the scriptblock

split dock
#
#ps1
Set-ExecutionPolicy Bypass -scope Process -Force
$cmd = {
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Start-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'
if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue | Select-Object Name, Enabled)) {
    Write-Output "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it..."
    New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
} else {
    Write-Output "Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists."
}

Set-MpPreference -DisableRealtimeMonitoring $true
}
$Later = New-JobTrigger -Once -At ([DateTime]::Now.AddMinutes(10))
Register-ScheduledJob -Name AddOpenSSH -Trigger $later -ScriptBlock $cmd
#

?

graceful current
#

no ... the code in your $cmd should be inside the { } instead

#

I do feel like it would be better to tack this on to the end of whatever other setup is running ... rather than hoping for luck with the timing.

#

This is definitely something we would do with DSC.

#

If you do $cmd = { .. }
Then do -ScriptBlock $cmd

split dock
#

like this?

graceful current
#

yeah

#

I would just move the code to the bottom, and loose the $cmd variable, but that should do the trick

#

At the very least, now you can "get" the results of the job later

split dock
#

Hmm for some reason the script fails

#

Like as itself

#

But the task works fine

graceful current
#

I don't follow

split dock
#

could the script maybe not start because im not connected to the rdp?

#

and its considered to be on idle or something?

#

seems like the job was never there