#PS2EXE doesnt support jobs or functions for me (?)

90 messages ยท Page 1 of 1 (latest)

shadow knoll
#

Greetings again guys, I'm having another issue

Is anyone here good with ps2exe? I have a script that works fine as a .ps1, but when converted to a exe via ps2exe, it spits out a million errors.

Some of them are related to write-host commands. Not sure why this is, but I didnt need those commands anyway so I disabled them

Some were related to how ps2exe handles pathing differently, and most of this is solved as well

but

PS2EXE doesnt seem to support powershell jobs or functions for me. This is weird, because I've found multiple examples people using jobs and functions in their ps2exe scripts with no issues. Any idea what might be causing this?

Attached is my code

trim dagger
#

So you have two immediate problems

#

first is you use this a lot: ```ps
#$customModuleD = Join-Path -Path $PSScriptRoot ".\PSWriteOffice.psd1"

#

well not that statement, but others

#

since ps2exe embeds the script as a string, you don't have PSScriptRoot you can use

#

you also do this: ```ps
[System.Reflection.Assembly]::LoadFrom(".\assembly\MahApps.Metro.dll") | out-null

#

that relative path is entirely dependent on the path PS starts in. Probably the working directory for an exe. It's hardly robust though.

shadow knoll
shadow knoll
trim dagger
#

sure, but if it's "I'm getting path errors", then the first thing one would do is fix the paths

shadow knoll
#

See thats fair, but would that relate to it not recognizing start-job as a command?

#

Or not recognizing functions as functions?

trim dagger
#

you would need to share full errors for these things

shadow knoll
#

Because of the way it compiles i dont think i can send like

A full error log yk it just gives me popups

#

but i can send screenshots of the popups

#

Give me a sec to get it up and running on my laptop rq

trim dagger
#

why do you bother using jobs anyway?

#

where you have this: ```ps
$job = Start-Job -ScriptBlock {
param($newname, $drive)
& $using:scriptPath -newname $newname -drive $drive
} -ArgumentList $newname, $drive

Write-Host "Switchboard job started with -newname $newname -drive $drive"

Wait-Job -Job $job
You may as well just run the script immediately. The job offers no benefit at all.
shadow knoll
#

It runs it on a separate thread, no?

trim dagger
#

it runs in a completely separate process. But you're blocking, so what's the point in the extra process?

shadow knoll
#

The idea is that it supposed to run separate so the UI doesnt freeze up during operation

Granted, the UI still does freeze up, but the idea is to eventually figure out why thats happening and fix that

#

Technically I know why its freezing

#

Its waiting for the job

#

But its only waiting because I couldnt get async write-host output

#

so its a temp little bandaid until I figure out a better method of doing that

trim dagger
#

okay, well it's your code, but I'd say it's pointless extra complexity.

#

either way, Start-Job will work unless your environment variables are messed up

#

that can very easily happen if you run the generated exe from inside a pwsh (Powershell 7) prompt.

shadow knoll
#

Can you elaborate? brains not braining

trim dagger
#

Child processes inherit the environment of the caller, and ps2exe (iirc) exclusively generates content that consumes Windows PowerShell.

#

so if you start a Windows PowerShell dependent process from a pwsh shell, it gets all PS 7's versions of the PSModulePath variable

#

if you directly start powershell.exe, there's some magic to clean that up. But the moment you stick "some other executable" in the middle it defeats all of that automagic fixing.

#

you can reproduce this.

  1. Start pwsh.exe.
    2 . Run cmd.exe (inside the current window).
  2. Run powershell.exe (inside the current window).
  3. Run Import-PowerShellDataFile (just a command that will fail), and observe that it complains it cannot find the command.

Look at $env:PSModulePath -split ';' and observe that all of the paths are pwsh specific, not win PS specific. That is, broken.

shadow knoll
#

I am a bit confused on how this relates to my problem ngl but I am trying my best to understand

#

@worn light please read this too when you get a chance

trim dagger
#

well you need to share some errors to be clear if it does or not. Descriptions of problems always tend to be vague (not you specifically, this is a common problem).

shadow knoll
trim dagger
#

yep, so one error leads to another

#

to be clear, are you running this from pwsh.exe?

#

or double-clicking it? Or something else?

shadow knoll
#

Im double clicking the exe file

#

If you want me to zip up the entire project and send it to you i can

trim dagger
#

not really :-p

shadow knoll
#

Alright ๐Ÿ‘

trim dagger
#

you need to take a look at $env:PSModulePath when this is running (message box, write-host, whatever you can see)

shadow knoll
#

Write-host wont work as you can see lol

And im not sure how to get ps2exe to display anything other than errors

#

in terms of pop-up boxes

trim dagger
#

[Console]::WriteLine($env:PSModulePath) maybe

#

otherwise ```ps
[System.Windows.Forms.MessageBox]::Show($env:PSModulePath)

shadow knoll
#

Ok. Itll take me a bit to get back to you, I'm messing with ps12exe rn to see if thats gonna not s**tthe bed like ps2exe does
But i will get back to you

#

Ok well uh

I compiled it with ps12exe instead and it works just fine

#

The way I coded something must not be compatible with how ps2exe compiles

#

If for some reason ps12exe doesnt work out and i need to switch back to ps2exe I will revive this thread

shadow knoll
#

@trim dagger I need help with a separate thing thats within the same script, should i create a new forum or just post it here?

distant lily
shadow knoll
shadow knoll
#

@distant lily Alright, question

I have two scripts, both compiled into exes with ps12exe. One of them is compiled with no console (its a GUI), and the other with no console (Its the main script but the GUI runs it, and i dont want a console window to pop up). Both ask to run admin by default.

In my gui script, I want to grab a variable from the main script after the main script is done running. How would I do that since both are EXEs?

#

@worn light the creator of ps12exe is here to help us lol

worn light
#

great!

#

wait why am i smug-douche thats a little disheartening

shadow knoll
#

i dont think swear words are allowed

distant lily
trim dagger
#

fwiw, nothing stops you writing content to files / registry / etc

shadow knoll
shadow knoll
#

its just a lot of extra weight on the cpu

trim dagger
#

the file is vast?

shadow knoll
shadow knoll
shadow knoll
trim dagger
#

its just a lot of extra weight on the cpu

#

that implies the data set is truly huge

shadow knoll
#

Ohh

#

No no

#

to clarify, the operation isnt that heavy, but theres already so many heavy operations that im trying not to make the run time even longer

trim dagger
#

then it's not a lot of extra weight at all :-p You're going to have to serialize to get it between processes no matter what

shadow knoll
#

Fair enough

#

Its certainly something we can do

distant lily
#

Honestly, you can skip optimization if it's not particularly important.
With at least 50 or 60 browser kernels and countless background programs on today's average home computer, a little lack of optimization is no big deal.
Even PowerShell's Add-Type doesn't compile in memory, it writes temporary files.๐Ÿคฃ

shadow knoll
#

Theres two forks: One is public, and one is private and being made for a private company

#

the ladder is what im working on (though all the code will be in the public version, literally the only difference is that the private one has the company logo lol)

distant lily
#

Cool, nice to see ps12exe being used in these places
And good luck with your work.๐Ÿ’–

shadow knoll
#

Ty!

#

If you think of any other suggestions on how we could pass through variables from target to gui lmk

left geyser
#

pass through variables from target to gui lmk

What exactly are you trying to pass, or what are you stuck on?

A simple way to pass your state is a json file.
They are easier to use because because they aren't flat like csv
Often you get decent properties if you limit the depth ... | ConvertTo-Json -depth 1

Before piping to that, you can tweak dropping properties to include/exclude only what you want.

Select -Prop wanted -Exclude uwanted, *somePattern*

Select-Object -Exclude *unwanted*, toIgnore

For example your first exe:

$Dest = Join-path 'temp:' 'lastConfig.json'
@{ 
   Names = 'bob', 'jen'
   SomePath = (gi . ).FullName  
} | ConvertTo-Json -Depth 1 | Set-Content -passthru -path $Dest

outputs

{
  "Names": [ "bob", "jen" ],
  "SomePath": "G:\\2024-git\\pwsh"
}

Then

start -path SecondExe.exe -Args $Dest
#

that's easy if they are synchronous. then you don't have to implement real IPC

shadow knoll
#

@distant lily Is there any way to convert a .psm1 to a .exe using ps12exe?

distant lily
# shadow knoll <@650767556067459103> Is there any way to convert a .psm1 to a .exe using ps12ex...

Considering the complexity of parsing modules it might not be very doable. If you're using multiple modules, I'd suggest making the exe as an entry point for downloading/updating/and launching the module. If you're using a single module, I'd suggest writing a separate ps1 file as an entry point for compiling the module.
I'm going to be busy today, so if you really want this functionality, maybe I'll try to make an update about it tomorrow
How would you like to run psm1, exporting a main function? Or run it directly as ps1?