PS2EXE-GUI: "Convert" PowerShell Scripts to EXE Files with GUI

Overworking of the great script of Ingo Karstein with GUI support. The GUI output and input is activated with one switch, real windows executables are generated. With Powershell 5.x support and graphical front end. Now here: https://github.com/MScholtes/TechNet-Gallery

PS2EXE-GUI.zip
 
 
 
 
 
4.7 Star
(38)
36,815 times
Add to favorites
7/11/2020
E-mail Twitter del.icio.us Digg Facebook
  • Doesn't seem to work for me, don't know why
    9 Posts | Last post May 12, 2019
    • Hi, this is a very interesting tool for us but I can't get it to work.
      When I compile everything seems fine but when I then run the .exe like I do the .ps1 I get these errors that I don't understand. The powershell script runs fine.
      
      C:\abc>abc.exe settings.ps1
      ERROR: At line:632 char:41
      +  cat c:\abc\allvms1.list | %{$_ -replace ?Name : ?,??} | %{$_ -replac ...
      +                                         ~
      You must provide a value expression following the '-replace' operator.
      
      At line:632 char:42
      +  cat c:\abc\allvms1.list | %{$_ -replace ?Name : ?,??} | %{$_ -replac ...
      +                                          ~~~~~
      Unexpected token '?Name' in expression or statement.
      
      At line:632 char:71
      + ... bc\allvms1.list | %{$_ -replace ?Name : ?,??} | %{$_ -replace ?Name?, ...
      +                                                                  ~
      You must provide a value expression following the '-replace' operator.
      
      At line:632 char:72
      + ... ms1.list | %{$_ -replace ?Name : ?,??} | %{$_ -replace ?Name?,??} | % ...
      +                                                            ~~~~~~
      Unexpected token '?Name?' in expression or statement.
      
      At line:632 char:78
      + ... s1.list | %{$_ -replace ?Name : ?,??} | %{$_ -replace ?Name?,??} | %{ ...
      +                                                                 ~
      Missing argument in parameter list.
      
      At line:632 char:98
      + ... lace ?Name : ?,??} | %{$_ -replace ?Name?,??} | %{$_ -replace ?----?, ...
      +                                                                  ~
      You must provide a value expression following the '-replace' operator.
      
      At line:632 char:99
      + ... ame : ?,??} | %{$_ -replace ?Name?,??} | %{$_ -replace ?----?,??}> c: ...
      +                                                            ~~~~~~
      Unexpected token '?----?' in expression or statement.
      
      And that goes on for a while.
      
      Any help would be very welcome.
      
      Regards,
        Stefan
    • Hello StefanBPS,
      
      you have some non-ASCII apostrophs in your scripts like “Name : ”,“” (copy-and-paste-error form Word or a webbrowser?).
      PowerShell is nice if they appear in an ANSI encoded file, but PS2EXE converts the script internally to UNICODE. So you get an error then.
      
      Please replace the wrong apostrophs with the correct ones: ' or ". Then everything will be fine.
      
      Greetings
      
      Markus
      
    • Markus, that helped a lot, thank you.
      Could it be that I can't load the Veeam snapin for Powershell.
      
      This is my code:
      \\
      
      Add-PSSnapin VeeamPSSnapin
      
      if( -not $?)
      {
      Write-Output "$(Get-TimeStamp) restorevm function : error loading the the Veeam powershell snapin" | Out-file $tracefile -append
      mailandcleanup
      }
      \\
      
      And my trace files shows that the it's giving an error on the RC so it starts the mailandcleanup function.
      Is this a possible limitation of the PS2EXE? I felt like I was so close to having it work! :)
      
    • Oh, sorry, it gave that error because it was already loaded.
      
      The real problem is this part:
      
      ERROR: Cannot convert 'System.Object[]' to the type 'Veeam.Backup.Core.Infrastructure.CViDatastoreItem' required by parameter 'Datastore'. Specified method is not supported.
      
      
    • Hello StefanBPS,
      
      I guess I cannot help in this case since Veeam is a Commercial product I don not own. But it might be helpful if you post the command that leads to the error message.
      
      Greetings
      
      Markus
    • I understand, maybe you have an idea what the problem could be.
      I have this powershell script that I would really like to compile to .exe, it works fine in powershell and restores a VM, it doesn't work after it is compiled.
      
      
      Veeam needs some variables to be filled before the restore can start:
      
      $datastore = Find-VBRViDatastore -Server $abchost -Name ($abcdatastore)
      $resourcepool = Find-VBRViResourcePool -Server "$abchost" -Name "$abcresourcepool"
      $restorepoint = Get-VBRRestorePoint -Backup $job -Name $line | Select -Last 1
      
      This is what's in the variables at my test run with Powershell (I did a write-host)
      
      restorepoint is Veeam.Backup.Core.COib
      server is esx01.demo.local
      resourcepool is Veeam.Backup.Core.Infrastructure.CViResourcePoolItem
      datastore is STORE12_cpty_vol02_V3700demo
      vmname is Vyos05-abctmp
      <restore starts and works fine>
      
      This is what's in the variables at my test run with the compiled .exe (write-host again)
      
      restorepoint is Veeam.Backup.Core.COib
      server is esx01.demo.local
      resourcepool is Veeam.Backup.Core.Infrastructure.CViResourcePoolItem
      datastore is STORE12_cpty_vol02_V3700demo
      vmname is Vyos05-abctmp
      <restore error as seen below>
      
      but the restore command gives this error:
      
      ERROR: Cannot convert 'System.Object[]' to the type 'Veeam.Backup.Core.Infrastructure.CViDatastoreItem' required by parameter 'Datastore'. Specified method is not supported.
      
      
      Used restore command:
      
      restore command: Start-VBRRestoreVM –RestorePoint $restorepoint –Server $abchost -Resourcepool $resourcepool –Datastore $datastore -VMName "$line-abctmp" | Tee-Object -FilePath "c:\abc\logs\$line-vmrestorecommandoutput.log" -append
      
      Any help would be great, if not that's ok.
      
      Stefan
    • The problem seems to be the type of variables passed tot he Start-VBRRestoreVM command. Powershell has no problems with these and executes the restore but once you compile the powershell script it comes with the "ERROR: Cannot convert 'System.Object[]' to the type 'Veeam.Backup.Core.Infrastructure.CViDatastoreItem' required by parameter 'Datastore'. Specified method is not supported." error.
      
      I can't seem to work around this problem yet.
      
    • Hello StefanBPS,
      
      seems to me a similiar problem like the "apostroph issue". In the command line with Start-uVBRRestoreVM there are some "EN DASH" characters (ASCII code 0x96) instead of the regular "hyphen minus" (ASCII code 0x2d). Please copy the "minus" from –Datastore to the clipboard and use it to replace it with a correct minus.
      
      Did you copy commands from MS Word, this character is used there as a optional hyphen?
      
      Greetings
      
      Markus
    • This was it, you fixed it Markus.
      I'm so happy, I've been wanting to get this code into .exe for a while now and you made it possible.
      Thank you very much, I know these questions where very much powershell code related and not really related to your software so I'm extra grateful that you helped me like you did.
      Thanks again.
      
      Regards,
        Stefan
  • Run As Admin
    2 Posts | Last post May 08, 2019
    • Having problem running my .exe as Admin.
      I can right click the .exe and run as admin and it opens just fine.
      However in my windows form I have various buttons configured to do different things that need admin rights.  Any button I click on has no admin rights even though I do a run as admin to launch it.  Suggestions??
    • Hello BFrisan,
      
      can you tell what kind of actions are invoked when a button is pressed in your application and how do you invoke them?
      
      Greetings
      
      Markus
  • Overwriting ps1 files
    6 Posts | Last post April 29, 2019
    • I was testing this script earlier today and it works great - thanks! I did make a mistake by setting my output file to be the ps1 script I wrote. Unfortunately, ps2exe overwrote my ps1 script, but luckily I had backups and was able to restore it.
      
      I recommend adding the following lines to prevent this:
      
      if ($outputFile -like "*.ps1")
      {
          Write-Host "Input file cannot be a ps1 file."
          exit -1
      }
    • Had a typo in there! The Write-Host line should say "Output file cannot be a ps1 file.".
    • Hello sbirdsill,
      
      for this reason there is the check:
      
      if ($inputFile -eq $outputFile)
      {
      	Write-Host "Input file is identical to output file!"
      	exit -1
      }
      
      in PS2EXE. This should prevent what you describe, are you using the latest version?
      
      Greetings
      
      Markus
    • That would only work if your input file is verbatim to your output file. In my case, I did something like -inputfile test.exe -outputfile test.ps1 (I already had test.exe in the folder from a previous attempt).
      
      But I found out that you can "decompile" it by running the exe file with the "extract" flag. So I could do "./test.exe -extract:test.ps1" and that would extract the PS1 script out of the exe.
      
      Pretty neat! Luckily I was just experimenting with it, so it wasn't a big concern in the first place.
    • Hello sbirdsill,
      
      I ask for your understanding that PS2EXE cannot intercept every wrong parameter. But the next version will check if the output parameter is an executable file. This will also solve your issue.
      
      Greetings
      
      Markus
    • Hello sbirdsill,
      
      the new version v0.5.0.15 of PS2EXE I released today checks for the output file's extension (.exe or .com). This should solve your issue.
      
      Greetings
      
      Markus
  • Progress Bar Icon
    3 Posts | Last post April 29, 2019
    • I notice the progress bar message box will not display the Icon specified on the task bar.
    • Hello jackofalltech,
      
      there is no icon in the progress bar or input box window, since there is no easy way to combine it with the application's icon.
      
      Simply it is to expensive to provide the code for this (icon file had to be read at compile time and converted to a binary variable that is converted back from variable to icon at runtime of the window).
      
      Greetings
      
      Markus
    • Hello jackofalltech,
      
      I investigated your request further and learned that it is quite easy to place the application's icon in a custom form. The new version v0.5.0.15 of PS2EXE I released today has this feature.
      
      Greetings
      
      Markus
  • Add dependencies to compiled script
    2 Posts | Last post April 28, 2019
    • Would it be possible to add a switch so that required dependencies are compiled along with the main scrip? This would let me compile into a single .exe rather than having to place the script/file dependencies in the same directory as the exe and/or code the logic to specify the location.
      
      e.g. A script which loads file paths from a csv, or calls another script could be complied with "-addfile ./filename.ps1 ./filename2.csv and when these files are required, they can be called from the script without a path.
      
      
      This facility is available in the now-discontinued PowerGUI and still works in that for now, but I anticipate this will no longer work at some point.
    • Hello Scepticyn,
      
      I'm sorry to say that I do not see a way to fulfill your wish. Unlike most of the other PS1 compilers PS2EXE does not create a self extracting archive that unpacks the script and accompanying files to the temporary directory but creates a C# standalone application (a PowerShell host) containing the script.
      
      Maybe a self extracting utility is rather what you need?
      
      Greetings
      
      Markus
      
  • Exiting the environment
    2 Posts | Last post April 11, 2019
    • For some reason, when loading the exe with an external program the following code loop causes the script in executable format to lock. I'm not sure if this is a bug, or the Powershell script itself, but if i remove this logic, the script runs fine. I'm calling the PS.exe from within another program.
      
       while (true)
                 {
                     process.WaitForExit(_waitForMS);
                     if (process.HasExited)
                     {
                         break;
                     }
      
                 }
      
      
      any ideas here?
    • Hello Zesty01,
      
      there is not enough Information in your question to answer it.
      
      - how did you obtain the process object?
      - what is the value of _waitForMS?
      - is your script compiled to console or noconsole app?
      
      Greetings
      
      Markus
  • BitsTransfer and runtime20
    3 Posts | Last post April 08, 2019
    • Hi Markus, 
      Thanks for the great tool. I am quite new t powershell programming but learnt a great deal working with this code and trying to convert a mini project to exe.
      
      In my code, I am importing BitsTransfer to download some microsoft updates and since I am deploying the code to be compatible with powershell 2 (applicable tool to Windows 7,8, and 10), I am running with runtime20 flag.
      
      However in doing so, my EXE runs properly in Windows 7 (powershell 2) while it fails under Windows 10 (powershell 5). The error complains that module cannot be imported because its manifest contains one or more members that are not valid.
      
      I was wondering if this is maybe a bug or I am doing something wrong (quite possible) or maybe not clear on "runtime20" flag? 
      
      Would appreciate your feedback
    • Hello moe01,
      
      this seems to be a problem or a bug with PowerShell V2 on Windows 10.
      
      On your Windows 10 Computer please start your PowerShell manually in Version 2.0 (same as -runtime20) with:
        powershell.exe -Version 2
      and then execute
        Import-Module BitsTransfer
      
      You get the same error. It has nothing to do with PS2EXE. Additionally I found no advice on Google to this.
      
      Sorry for that
      
      Greetings
      
      Markus
      
    • Markus,
      Huh Interesting, I probably should have tried it myself before posting.!
      Thanks a lot for clarification!
      
      Cheers,
      Moe
  • Exiting the environment and PSScriptRoot
    2 Posts | Last post April 05, 2019
    • Hi!
      
      two things: It seems that the variable PSScriptroot as well as the $Myinvocation.mycommand variable do not work properly when using this tool to convert the script to exe.
      
      Any thoughts on how to combat that?
      
      Also,
      It could just be me, but exiting the environment does not deliver the exit code outward.
      
      Thoughts?
    • Hello Zesty01,
      
      see chapter "Remarks" on the "Description" tab for this topic.
      
      Greetings
      
      Markus
  • Compilation limitation?
    3 Posts | Last post April 04, 2019
    • Hello Markus,
      
      I'm getting an compilation error :
      
      Could not create the PowerShell .exe file because of compilation errors. Use -verbose parameter to see details.
      COMMENTAIRES : error CS0013: Erreur inattendue lors de l'écriture des métadonnées dans le fichier 'd:\PS2EXEGUI\EXE\MyExe.exe' -- 'Pas d'espace logique restant pour la création de chaînes utilisateur supplémentaires.'
      
      I investigated, and it's due to some binaries integrated in my script by converted Base64 strings (picture, pdf, ...). Whatever the Base64 string, it looks like it has some limitation. There are about ten Base64 strings in my script for binaries sized 70ko up to 2Mo. And my script is sized 6200Ko, and about 16Mo once compiled.
      
      I found more information here:
      https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/ef/performance-considerations#strategies-for-improving-performance
      It's talking about the size limit with large and complex models. But I don't know how to do. 
      
      Thanks for your Help.
    • Hello AG_G1,
      
      you have a similiar problem that jackofalltech has (see question "Over Come 16777214" below), a string in C# is limited to a size of 16 MByte. Since PS2EXE-GUI compiles to C# and the PS1 script is held in a string variable in C#, this is a fixed limit!
      
      C# is storing ist strings UTF16 encoded, so the size of your script will be doubled. Additionally it is converted to a base64 string which leads to a another growth in size of 33 percent. This is the reason why your script raises from 6 MByte to over 16 MByte when compiled.
      
      I thought about it, but I found no way to get along with it. It is a hard limit. Please try to build smaller scripts.
      
      Greetings
      
      Markus
      
      
    • AG_G1 and Markus, 
      
      I never found a solution to the issue but i did find that if you remove some of the comments in the ps2exe script that helps, Also in the script you are converting if you use Vs Code run the convert indentations to tab this will help. its not just the size of the script but also the number of characters in the script. 
  • hang over hash table
    6 Posts | Last post March 27, 2019
    • I compiled the ps1 file which get hash table from another ps1 program.
      but, compiled program didnt work.
      
      "ERROR: cant attach Index to type System.String Object" (transfered from Japanese, so it may be some difference)
      
      How to solve this problem ?
      thank you.
      
      *test_program1.ps1
       ---------------------- 
      $HashTable = @{
          "001" = "test1"
          "002" = "test2"
      }
      ./test_program2.exe $HashTable 
      -------------------------
      
      *test_program2.exe
      -------------------------
      Param($HashTable)
      Write-Host $HashTable["test1"]
      Write-Host $HashTable["test2"]
      --------------------------
    • sorrry, there is mistake.
      
      *test_program2.exe
      -------------------------
      Param($HashTable)
      Write-Host $HashTable["001"]
      Write-Host $HashTable["002"]
      --------------------------
    • Hello papamanman,
      
      I'm sorry to say that this cannot work. PS2EXE compiles scripts to real executables, and all executables have only access to parameters of the type string! Parameters of a complex type like HashTable can only be passed to Commandlets or PowerShell functions.
      In your case your HashTable is implicitly converted to a string so the compiled script only gets the string "System.Collections.Hashtable" as parameter.
      
      There is nothing I can do about this since this is a limitation of the shell.
      
      Greetings
      
      Markus
      
    • Hello papamanman,
      
      you might work around the limitation if you store the object in a XML file:
      
      *test_program1.ps1
      ---------------------- 
      Export-Clixml Hashtable.xml
      ./test_program2.exe "Hashtable.xml" 
      
      -------------------------
      
      
      *test_program2.exe
      -------------------------
      Param($XmlFile)
      $HashTable = Import-Clixml $XmlFile
      Write-Host $HashTable["test1"]
      Write-Host $HashTable["test2"]
      
      Greetings
      
      Markus
    • Sorry, a mistake in *test_program1.ps1, correct is
      $HashTable | Export-Clixml Hashtable.xml
      
      
      
    • Hi Markus,
      
      I understood PS2EXE cant convert HashTable.
      Your alternative idea is good for my purpose.
      
      Thank you so much! 
71 - 80 of 184 Items