// Win-PS2EXE v1.0.1.2 // Front end to Powershell-Script-to-EXE-Compiler PS2EXE.ps1: https://github.com/MScholtes/PS2EXE // Markus Scholtes, 2023 // // WPF "all in one file" program, no Visual Studio or MSBuild is needed to compile // Version for .Net 4.x /* compile with: %WINDIR%\Microsoft.NET\Framework\v4.0.30319\csc.exe /target:winexe Win-PS2EXE.cs /r:"%WINDIR%\Microsoft.NET\Framework64\v4.0.30319\WPF\presentationframework.dll" /r:"%WINDIR%\Microsoft.NET\Framework64\v4.0.30319\WPF\windowsbase.dll" /r:"%WINDIR%\Microsoft.NET\Framework64\v4.0.30319\WPF\presentationcore.dll" /r:"%WINDIR%\Microsoft.NET\Framework64\v4.0.30319\System.Xaml.dll" /win32icon:MScholtes.ico */ using System; using System.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Markup; using System.Xml; // set attributes using System.Reflection; [assembly:AssemblyTitle("Graphical front end to Invoke-PS2EXE")] [assembly:AssemblyDescription("Graphical front end to Invoke-PS2EXE")] [assembly:AssemblyConfiguration("")] [assembly:AssemblyCompany("MS")] [assembly:AssemblyProduct("Win-PS2EXE")] [assembly:AssemblyCopyright("© Markus Scholtes 2023")] [assembly:AssemblyTrademark("")] [assembly:AssemblyCulture("")] [assembly:AssemblyVersion("1.0.1.2")] [assembly:AssemblyFileVersion("1.0.1.2")] namespace WPFApplication { public class CustomWindow : Window { // create window object out of XAML string public static CustomWindow LoadWindowFromXaml(string xamlString) { // Get the XAML content from a string. // prepare XML document XmlDocument XAML = new XmlDocument(); // read XAML string XAML.LoadXml(xamlString); // and convert to XML XmlNodeReader XMLReader = new XmlNodeReader(XAML); // generate WPF object tree CustomWindow objWindow = (CustomWindow)XamlReader.Load(XMLReader); // return CustomWindow object return objWindow; } // helper function that "climbs up" the parent object chain from a window object until the root window object is reached private FrameworkElement FindParentWindow(object sender) { FrameworkElement GUIControl = (FrameworkElement)sender; while ((GUIControl.Parent != null) && (GUIControl.GetType() != typeof(CustomWindow))) { GUIControl = (FrameworkElement)GUIControl.Parent; } if (GUIControl.GetType() == typeof(CustomWindow)) return GUIControl; else return null; } // event handlers // left mouse click private void Button_Click(object sender, RoutedEventArgs e) { // event is handled afterwards e.Handled = true; // retrieve window parent object Window objWindow = (Window)FindParentWindow(sender); // if not found then end if (objWindow == null) { return; } if (((Button)sender).Name == "Cancel") { // button "Cancel" -> close window objWindow.Close(); } else { // button "Compile" -> call PS2EXE // read content of TextBox control TextBox objSourceFile = (TextBox)objWindow.FindName("SourceFile"); if (objSourceFile.Text == "") { MessageBox.Show("No source file specified", "Compile", MessageBoxButton.OK, MessageBoxImage.Error); return; } string arguments = "-NoProfile -NoLogo -EP Bypass -Command \"Invoke-ps2exe -inputFile '" + objSourceFile.Text + "'"; // read content of TextBox control TextBox objTargetFile = (TextBox)objWindow.FindName("TargetFile"); if (objTargetFile.Text != "") { if (System.IO.Directory.Exists(objTargetFile.Text)) { // if directory then append source file name arguments += " -outputFile '" + System.IO.Path.Combine(objTargetFile.Text, System.IO.Path.GetFileNameWithoutExtension(objSourceFile.Text)) + ".exe'"; } else arguments += " -outputFile '" + objTargetFile.Text + "'"; } // read content of TextBox control TextBox objIconFile = (TextBox)objWindow.FindName("IconFile"); if (objIconFile.Text != "") { arguments += " -iconFile '" + objIconFile.Text + "'"; } // read content of TextBox control TextBox objFileVersion = (TextBox)objWindow.FindName("FileVersion"); if (objFileVersion.Text != "") { arguments += " -version '" + objFileVersion.Text + "'"; } // read content of TextBox control TextBox objFileDescription = (TextBox)objWindow.FindName("FileDescription"); if (objFileDescription.Text != "") { arguments += " -title '" + objFileDescription.Text + "'"; } // read content of TextBox control TextBox objProductName = (TextBox)objWindow.FindName("ProductName"); if (objProductName.Text != "") { arguments += " -product '" + objProductName.Text + "'"; } // read content of TextBox control TextBox objCopyright = (TextBox)objWindow.FindName("Copyright"); if (objCopyright.Text != "") { arguments += " -copyright '" + objCopyright.Text + "'"; } // read state of CheckBox control CheckBox objCheckBox = (CheckBox)objWindow.FindName("noConsole"); if (objCheckBox.IsChecked.Value) { arguments += " -noConsole"; } // read state of CheckBox control CheckBox objCheckBox2 = (CheckBox)objWindow.FindName("noOutput"); if (objCheckBox2.IsChecked.Value) { arguments += " -noOutput"; } // read state of CheckBox control CheckBox objCheckBox3 = (CheckBox)objWindow.FindName("noError"); if (objCheckBox3.IsChecked.Value) { arguments += " -noError"; } // read state of CheckBox control CheckBox objCheckBox4 = (CheckBox)objWindow.FindName("requireAdmin"); if (objCheckBox4.IsChecked.Value) { arguments += " -requireAdmin"; } // read state of CheckBox control CheckBox objCheckBox5 = (CheckBox)objWindow.FindName("configFile"); if (objCheckBox5.IsChecked.Value) { arguments += " -configFile"; } // read state of RadioButton control RadioButton objRadioButton = (RadioButton)objWindow.FindName("STA"); if (objRadioButton.IsChecked.Value) { arguments += " -STA"; } else { arguments += " -MTA"; } // read content of ComboBox control ComboBox objComboBox = (ComboBox)objWindow.FindName("Platform"); ComboBoxItem objComboBoxItem = (ComboBoxItem)objComboBox.SelectedItem; string selectedItem = objComboBoxItem.Content.ToString(); if (selectedItem != "AnyCPU") { if (selectedItem == "x64") { arguments += " -x64"; } else { arguments += " -x86"; } } // read content of TextBox control TextBox objAdditionalParameters = (TextBox)objWindow.FindName("AdditionParameters"); if (objAdditionalParameters.Text != "") { arguments += " " + objAdditionalParameters.Text.Replace("\"", "\\\""); } // create powershell process with ps2exe command line ProcessStartInfo psi = new ProcessStartInfo("powershell.exe", arguments + " -verbose; Read-Host \\\"`nPress Enter to leave\\\"\""); // working directory is the directory of the source file psi.WorkingDirectory = System.IO.Path.GetDirectoryName(System.IO.Path.GetFullPath(objSourceFile.Text)); psi.UseShellExecute = false; try { // start process Process.Start(psi); } catch (System.ComponentModel.Win32Exception ex) { // error MessageBox.Show("Error " + ex.NativeErrorCode + " starting the process\r\n" + ex.Message + "\r\n", "Compile", MessageBoxButton.OK, MessageBoxImage.Error); } catch (System.InvalidOperationException ex) { // error MessageBox.Show("Error starting the process\r\n" + ex.Message + "\r\n", "Compile", MessageBoxButton.OK, MessageBoxImage.Error); } } } // mouse moves into button area private void Button_MouseEnter(object sender, MouseEventArgs e) { // retrieve window parent object Window objWindow = (Window)FindParentWindow(sender); // if found change mouse form if (objWindow != null) { objWindow.Cursor = System.Windows.Input.Cursors.Hand; } } // mouse moves out of button area private void Button_MouseLeave(object sender, MouseEventArgs e) { // retrieve window parent object Window objWindow = (Window)FindParentWindow(sender); // if found change mouse form if (objWindow != null) { objWindow.Cursor = System.Windows.Input.Cursors.Arrow; } } // click on file picker button ("...") private void FilePicker_Click(object sender, RoutedEventArgs e) { // retrieve window parent object Window objWindow = (Window)FindParentWindow(sender); // if not found then end if (objWindow == null) { return; } if (((Button)sender).Name != "TargetFilePicker") { // create OpenFileDialog control Microsoft.Win32.OpenFileDialog objFileDialog = new Microsoft.Win32.OpenFileDialog(); // set file extension filters if (((Button)sender).Name == "SourceFilePicker") { // button to TextBox "SourceFile" objFileDialog.DefaultExt = ".ps1"; objFileDialog.Filter = "PS1 Files (*.ps1)|*.ps1|All Files (*.*)|*.*"; } else { // button to TextBox "IconFile" objFileDialog.DefaultExt = ".ico"; objFileDialog.Filter = "Icon Files (*.ico)|*.ico|All Files (*.*)|*.*"; } // display file picker dialog Nullable result = objFileDialog.ShowDialog(); // file selected? if (result.HasValue && result.Value) { // fill Texbox with file name if (((Button)sender).Name == "SourceFilePicker") { // button to TextBox "SourceFile" TextBox objSourceFile = (TextBox)objWindow.FindName("SourceFile"); objSourceFile.Text = objFileDialog.FileName; } else { // button to TextBox "IconFile" TextBox objIconFile = (TextBox)objWindow.FindName("IconFile"); objIconFile.Text = objFileDialog.FileName; } } } else { // use custom dialog for folder selection because there is no WPF folder dialog!!! TextBox objTargetFile = (TextBox)objWindow.FindName("TargetFile"); // create OpenFolderDialog control OpenFolderDialog.OpenFolderDialog objOpenFolderDialog = new OpenFolderDialog.OpenFolderDialog(); if (objTargetFile.Text != "") { // set starting directory for folder picker if (System.IO.Directory.Exists(objTargetFile.Text)) objOpenFolderDialog.InitialFolder = objTargetFile.Text; else objOpenFolderDialog.InitialFolder = System.IO.Path.GetDirectoryName(objTargetFile.Text); } else { // no starting directory for folder picker objOpenFolderDialog.InitialFolder = ""; } // display folder picker dialog System.Windows.Interop.WindowInteropHelper windowHwnd = new System.Windows.Interop.WindowInteropHelper(this); Nullable result = objOpenFolderDialog.ShowDialog(windowHwnd.Handle); if ((result.HasValue) && (result == true)) { // get result only if a folder was selected objTargetFile.Text = objOpenFolderDialog.Folder; } } } // "empty" drag handler private void TextBox_PreviewDragOver(object sender, DragEventArgs e) { e.Effects = DragDropEffects.All; e.Handled = true; } // drop handler: insert filename to textbox private void TextBox_PreviewDrop(object sender, DragEventArgs e) { object objText = e.Data.GetData(DataFormats.FileDrop); TextBox objTextBox = sender as TextBox; if ((objTextBox != null) && (objText != null)) { objTextBox.Text = string.Format("{0}",((string[])objText)[0]); } } } // end of CustomWindow public class Program { // WPF requires STA model, since C# default to MTA threading, the following directive is mandatory [STAThread] public static void Main() { // XAML string defining the window controls string strXAML = @" Win-PS2EXE: Graphical front-end to PS2EXE