[2.0-a134] Run PowerShell Without PowerShell EXE!

Post your working scripts, libraries and tools.
xroot
Posts: 40
Joined: 21 Jun 2019, 08:45

[2.0-a134] Run PowerShell Without PowerShell EXE!

Post by xroot » 17 May 2021, 08:56

Found this on github https://gist.github.com/rhmoult/d77452ea70504fbb8635b4b2d17de6cb

Follow the instruction steps, or use my "DLL" that make it much simpler to start playing.

Changed the name from "Empire.COMPosh" an EXE to "psScript" a 64bit DLL.

"psScript" only has 1 method called "PS_Script()".

Example of a Powershell cmdlet (get longdate).

Code: Select all

ps := ComObject("psScript")

psCode := "Get-Date -format D"

msgbox "LongDate = " ps.PS_Script(psCode),"PowerShell Get-Date","iconi"
Run batch file to register the 64bit Dll "psScript.dll".
This creates the type library file "psScript.tlb".
Run batch file as administrator.

RegPShell.bat

Code: Select all

:: register 64bit
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm /codebase /tlb %~dp0\psScript.dll
pause
UnRegPShell.bat

Code: Select all

:: unregister 64bit
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm %~dp0\psScript.dll /tlb /u
pause
Powershell has lots of functionality, make PS Gui scripts, or C# Gui scripts, executed in Powershell with AHK.

Powershell Gui Script:

Code: Select all

psCode := Format("
(
    Add-Type -AssemblyName System.Windows.Forms
    function iClicked(){
        switch($statusbar.panels.IndexOf($_.StatusBarPanel)){
            0 {$api::MessageBoxTimeout(0,$statusbar.panels[0].text,"StatusBar 1 Clicked",64,0,2000);break}
            1 {$api::MessageBoxTimeout(0,$statusbar.panels[1].text,"StatusBar 2 Clicked",64,0,2000);break}
            2 {$api::MessageBoxTimeout(0,$statusbar.panels[2].text,"StatusBar 3 Clicked",64,0,2000);break}
            3 {$api::MessageBoxTimeout(0,$statusbar.panels[3].text,"StatusBar 4 Clicked",64,0,2000);break}
            4 {$gui.Close();return}
            default {break}}}
    $gui                 = New-Object windows.forms.form
    $gui.icon            = "crapper.ico"
    $gui.text            = "PowerShell Gui In AHK {}"
    $gui.size            = "700,300"
    $gui.StartPosition   = "CenterScreen"
    $gui.backcolor       = "lightblue"
    $gui.KeyPreview      = $true
    $gui.formborderstyle = "FixedSingle"
    $gui.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$gui.Close()}})

    $label           = New-Object Windows.Forms.Label
    $label.text      = "Click StatusBar Panels"
    $label.textalign = "MiddleCenter"
    $label.font      = "Times New Roman,22,style=Bold,underline"
    $label.forecolor = "blue"
    $label.Location  = "0,50"
    $label.Size      = "700,30"
    $gui.Controls.Add($label)

    $button                           = New-Object Windows.Forms.Button
    $button.text                      = "Exit"
    $button.font                      = "Times New Roman,24,style=Bold,Underline"
    $button.backcolor                 = "Blue"
    $button.forecolor                 = "Yellow"
    $button.FlatStyle                 = "Flat"
    $button.FlatAppearance.BorderSize = 0
    $button.Location                  = "550,150"
    $button.Size                      = "100,50"
    $button.Cursor                    = "Hand"
    $button.Add_Click({$gui.Close()})
    $button.Add_MouseEnter({$button.BackColor="Red"
                            $button.ForeColor="Cyan"})
    $button.Add_MouseLeave({$button.BackColor="Blue"
                            $button.ForeColor="Yellow"})
    $gui.Controls.Add($Button)

    $statusbar            = New-Object Windows.Forms.StatusBar
    $statusbar.height     = 40
    $statusbar.font       = "Times New Roman,14,style=bold"
    $statusbar.SizingGrip = $false
    $statusbar.ShowPanels = $true
    $statusbar.cursor     = "Hand"
    for($i = 0;$i -le 4;$i++){
        $panel = New-Object Windows.Forms.StatusBarPanel
        $statusbar.Panels.Add($panel)|out-null
        $statusbar.panels[$i].alignment   = "center"
        $statusbar.panels[$i].BorderStyle = "Raised"
        $statusbar.panels[$i].autosize    = "Contents"}
    $statusbar.panels[3].autosize    = "spring"
    $statusbar.panels[4].autosize    = "spring"
    $statusbar.panels[0].ToolTipText = "StatusBar Handle"
    $statusbar.panels[0].text        = $statusbar.Handle.ToString("X")
    $statusbar.panels[1].icon        = "lex1.ico"
    $statusbar.panels[1].text        = "AHK Script"
    $statusbar.panels[2].text        = (Get-Date -format D).toString().replace(" 0"," ")
    $statusbar.panels[3].text        = get-date -format t
    $statusbar.panels[4].icon        = "Burn.ico"
    $statusbar.panels[4].text        = "EXIT"
    $statusbar.add_PanelClick({iClicked})
    $gui.Controls.Add($StatusBar)

    $api = Add-Type -MemberDefinition @"
        [DllImport("user32")]
        public static extern int MessageBoxTimeout(IntPtr hwnd,String text,String title,uint type,Int16 wLanguageId,Int32 milliseconds);
        [DllImport("user32")]
        public static extern int SetWindowRgn(IntPtr hWnd,IntPtr hRgn,bool bRedraw);
        [DllImport("gdi32")]
        public static extern IntPtr CreateRoundRectRgn(int x1,int y1,int x2,int y2,int cx,int cy);
        [DllImport("user32")]
        public static extern IntPtr SendMessage(IntPtr hWnd,UInt32 Msg,Int32 wParam,Int32 lParam);
        [DllImport("uxtheme")]
        public static extern int SetWindowTheme(IntPtr hWnd,string pszSubAppName,string pszSubIdList);
    "@ -Name "api" -Namespace "Win32" -PassThru

    $api::SetWindowRgn($button.Handle,$api::CreateRoundRectRgn(0,0,100,50,45,45),$true)
    $api::SendMessage($statusbar.Handle,8193,0,0xE6D8AD)
    $api::SetWindowTheme($statusbar.Handle,0,0)

    $timer = new-object Windows.Forms.Timer
    $timer.Interval = 999
    $timer.add_tick({$statusbar.panels[3].Text = get-date -format t})
    $timer.start()

    $gui.ShowDialog()|out-null
)",A_AHKVERSION " 64-Bit")

; msgbox psCode

ps := ComObject("psScript")

ps.PS_Script(psCode)
C# Gui Script:

Code: Select all

psCode := Format("
(
    $using = ("System","System.Drawing","System.Windows.Forms","System.Runtime.InteropServices")
    $cSharp = @"
        using System;
        using System.Drawing;
        using System.Runtime.InteropServices;
        using System.Windows.Forms;
        namespace statusbar
        {
            public class Gui : Form
            {
                [DllImport("user32")]
                public static extern int MessageBoxTimeout(IntPtr hwnd,String text,String title,uint type,Int16 wLanguageId,Int32 milliseconds);
                [DllImport("user32")]
                public static extern int SetWindowRgn(IntPtr hWnd,IntPtr hRgn,bool bRedraw);
                [DllImport("gdi32")]
                public static extern IntPtr CreateRoundRectRgn(int x1,int y1,int x2,int y2,int cx,int cy);
                [DllImport("user32")]
                public static extern IntPtr SendMessage(IntPtr hWnd,UInt32 Msg,Int32 wParam,Int32 lParam);
                [DllImport("uxtheme")]
                public static extern int SetWindowTheme(IntPtr hWnd,string pszSubAppName,string pszSubIdList);

                public Button button;
                public StatusBar sb;

                public Gui()
                {
                    this.Text            = "C# Gui In AHK {}";
                    this.Icon            = new Icon("crapper.ico");
                    this.Size            = new Size(650,300);
                    this.StartPosition   = FormStartPosition.CenterScreen;
                    this.BackColor       = Color.LightBlue;
                    this.KeyPreview      = true;
                    this.FormBorderStyle = FormBorderStyle.FixedSingle;
                    this.KeyDown        += new KeyEventHandler(Gui_KeyDown);

                    Label label      = new Label();
                    label.Text       = "Click StatusBar Panels";
                    label.ForeColor  = Color.Blue;
                    label.TextAlign  = ContentAlignment.MiddleCenter;
                    label.Location   = new Point(10,50);
                    label.Size       = new Size(600,25);
                    label.Font       = new Font("Times New Roman",22.0f,FontStyle.Bold | FontStyle.Underline);
                    this.Controls.Add(label);

                    button                           = new Button();
                    button.BackColor                 = Color.Blue;
                    button.ForeColor                 = Color.Yellow;
                    button.FlatStyle                 = FlatStyle.Flat;
                    button.FlatAppearance.BorderSize = 0;
                    button.Text                      = "Exit";
                    button.Size                      = new Size(100,50);
                    button.Location                  = new Point(500,150);
                    button.Font                      = new Font("Times New Roman",22.0f,FontStyle.Bold | FontStyle.Underline);
                    button.Cursor                    = Cursors.Hand;
                    button.Click                    += new EventHandler(button_Click);
                    button.MouseEnter               += new EventHandler(button_MouseEnter);
                    button.MouseLeave               += new EventHandler(button_MouseLeave);
                    this.Controls.Add(button);

                    sb            = new StatusBar();
                    sb.Height     = 45;
                    sb.Font       = new Font("Times New Roman",14.0f,FontStyle.Bold);
                    sb.SizingGrip = false;
                    sb.ShowPanels = true;
                    sb.BackColor  = Color.LightBlue;  //not working
                    sb.Cursor     = Cursors.Hand;
                    for(int i=0;i<5;i++){
                        StatusBarPanel panel = new StatusBarPanel();
                        panel.Alignment      = HorizontalAlignment.Center;
                        panel.AutoSize       = StatusBarPanelAutoSize.Contents;
                        panel.BorderStyle    = StatusBarPanelBorderStyle.Sunken;
                        panel.Tag            = i+1;
                        sb.Panels.Add(panel);
                    }
                    sb.Panels[0].Text     = sb.Handle.ToString("X");
                    sb.Panels[1].Icon     = new Icon("lex1.ico");
                    sb.Panels[1].Text     = "AHK Script";
                    sb.Panels[2].Text     = DateTime.Today.ToLongDateString();
                    sb.Panels[3].Text     = DateTime.Now.ToShortTimeString();
                    sb.Panels[4].Text     = "Exit";
                    sb.Panels[4].AutoSize = StatusBarPanelAutoSize.Spring;
                    sb.Panels[4].Icon     = new Icon("Burn.ico");
                    sb.PanelClick        += new StatusBarPanelClickEventHandler(sb_PanelClick);
                    this.Controls.Add(sb);

                    SetWindowRgn(button.Handle,CreateRoundRectRgn(0,0,100,50,45,45),true);
                    SendMessage(sb.Handle,8193,0,0xE6D8AD);
                    SetWindowTheme(sb.Handle,"","");

                    Timer tm    = new Timer();
                    tm.Interval = 999;
                    tm.Tick    += new EventHandler(tm_Tick);
                    tm.Start();
                }
                private void tm_Tick(Object sender,EventArgs e)
                {
                    sb.Panels[3].Text = DateTime.Now.ToShortTimeString();
                }
                private void Gui_KeyDown(object sender,KeyEventArgs e)
                {
                    if(e.KeyCode == Keys.Escape){Application.Exit();}
                }
                private void button_Click(object sender, EventArgs e)
                {
                    Application.Exit();
                }
                private void button_MouseLeave(object sender, EventArgs e)
                {
                    button.BackColor = Color.Blue;
                    button.ForeColor = Color.Yellow;
                }
                private void button_MouseEnter(object sender, EventArgs e)
                {
                    button.BackColor = Color.Red;
                    button.ForeColor = Color.Cyan;
                }
                private void sb_PanelClick(Object sender, StatusBarPanelClickEventArgs e)
                {
                    if(sb.Panels.IndexOf(e.StatusBarPanel)==4){Application.Exit();}
                    MessageBoxTimeout(IntPtr.Zero,e.StatusBarPanel.Text,"StatusBar "+e.StatusBarPanel.Tag,64,0,2000);
                }
                [STAThread]
                public static void Main()
                {
                    new Gui().ShowDialog();
                }
            }
        }
    "@
    Add-Type -ReferencedAssemblies $using -TypeDefinition $cSharp
    [statusbar.Gui]::Main()
)",A_AHKVERSION " 64-Bit")

; msgbox psCode

ps := ComObject("psScript")

ps.PS_Script(psCode)
All tested on 64bit Window 7, PowerShell Version 4.
psScript.zip
(30.21 KiB) Downloaded 68 times

Return to “Scripts and Functions (v2)”