Automation with PowerShell


It is now the time A.I. to automate difficult tasks, but I also like old-fashioned code-based automation. PowerShell will operate computers automatically instead of human from here!

Key Emulation

PowerShell sends key code to textarea or something. It uses .Net functionality so it is easy to code.
#send key code
$instruction = "{ENTER}"
[system.windows.forms.sendkeys]::sendwait($instruction)

PowerShell sends key event to machine. It uses Win32 API keybd_event via C# code. I would like to use sendInput that is reccomended function in current Windows rather than keybd_event, but it did not work well.
#send key signal
$source=@"
using System;
using System.Runtime.InterropServices;
using Sytem.Windows.Forms;
public class Win32KeyEvt{
    [DllImport("user32.dll")]
    extern static void keybd_event(
        byte bVk,
        byte bScan,
        int dwFlags,
        int dwExtraInfo
    );
    public static void sendKey(byte key, int flag){
        keybd_event(key, 0, flag, 0);
    }
}
"@
$assemblies = @(
    "system.windows.forms"
)
add-type -language csharp -typedefinition $source -referencedassemblies $assemblies
[Win32KeyEvt]::sendkey(0x5B,0) #key down
[Win32KeyEvt]::sendkey(0x5B,2) #key release

Mouse Emulation

PowerShell sends mouse event.
#click event
$source=@"
using System;
using System.Runtime.InterropServices;
using Sytem.Windows.Forms;
public class Win32MouseEvt{
    [DllImport("user32.dll")]
    extern static uint SendInput(
        uint nInputs,
        INPUT[] pInputs.
        int cbSize
    );
    [StructLayout(LayoutKind.Sequential)]
    struct INPUT{
        public int type;
        public MOUSEINPUT wi;
    }
    [StructLayout(Layoutkind.Sequential)]
    struct MOUSEINPUT{
        public int dx;
        public int dy;
        public int mouseData;
        public int dwFlags;
        public int time;
        public IntPtr dwExtraInfo;
    }
    const int MOUSEEVENTF_MOVED = 0x0001;
    const int MOUSEEVENTF_LEFTDOWN = 0x0002;
    const int MOUSEEVENTF_LEFTUP = 0x0004;
    const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
    const int MOUSEEVENTF_RIGHTUP = 0x0010;
    const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
    const int MOUSEEVENTF_MIDDLEUP = 0x0040;
    const int MOUSEEVENTF_WHEEL = 0x0080;
    const int MOUSEEVENTF_XDOWN = 0x0100;
    const int MOUSEEVENTF_XUP = 0x0200;
    const int MOUSEEVENTF_ABSOLUTE = 0x800;
    const int screen_length = 0x10000;
    public static void leftClick(int x, int y){
        int resolution_x = 1920;
        int resolution_y = 1080;
        INPUT[] input = new INPUT[3];
        input[0].wi.dx = (x * 65535) / resolution_x;
        input[0].wi.dy = (y * 65535) / resolution_y;
        input[0].wi.dwFlags = MOUSEEVENTF_MOVED | MOUSEEVENTF_ABSOLUTE;
        input[1].wi.dwFlags = MOUSEEVENTF_LEFTDOWN;
        input[2].wi.dwFlags = MOUSEEVENTF_LEFTUP;
        SendInput(3, input, Marshal.SizeOf(input[0]));
    }
}
"@
$assemblies = @(
    "system.windows.forms"
)
add-type -language csharp -typedefinition $source -referencedassemblies $assemblies
[Win32MouseEvt]::leftClick(100, 100)

Pixel Determination

PowerShell retrieves the color on display.
#retrieve RGB info on cursor
$x = [system.windows.forms.cursor]::Position.x
$y = [system.windows.forms.cursor]::Position.y
$bitmap = new-object system.drawing.bitmap(1,1)
$graphics = [system.drawing.graphics]::fromImage($bitmap)
$graphics.copyfromscreen($x, $y, 0, 0, $bitmap.size)
$pixel = $bitmap.getpixel(0.0)
#RGB values(0-255,0-255,0-255) are in $pixel.R $pixel.G $pixel.B

Window Activation

PowerShell activates window console (by process id)
#activate window by process id
$procid = 100
add-type --assemblyname microsoft.visualbasic
[microsoft.visualbasic.interaction]::AppActivate($procid)

PowerShell activates window console (by process name)
#activate window by process name
$procname = "*Excel*"
add-type --assemblyname microsoft.visualbasic
$procid = ([system.diagnostics.process]::getprocesses() | ? {$_.ProcessName -like $name } | select-object -first 1 ).ID
if($procid -ne $null){
    [microsoft.visualbasic.interaction]::AppActivate($procid)
}

Window Activation(IE Window)

PowerShell activates IE window console (by URL)
#activate IE window by URL
$url = "*google.com*"
$shell = new-object -comobject shell.application
$source=@"
using System;
using System.Runtime.InterropServices;
using Sytem.Windows.Forms;
public class Win32ActivateWindow{
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool SetForegroundWindow(IntPtr hWnd);
}
"@
$assemblies = @(
    "system.windows.forms"
)
add-type -language csharp -typedefinition $source -referencedassemblies $assemblies
$hWnd = ($shell.windows() | ? {$_.Name -eq "Internet Explorer"} | ? {$_.LocationURL -like $url} | select-object -first 1 ).HWND
if($hWnd -ne $null){
    [void][Win32ActivateWindow]::SetForegroundWindow($hWnd)
}

PowerShell activates IE window console (by URL)
#activate IE window by LocationName
$name = "*Google*"
$shell = new-object -comobject shell.application
$source=@"
using System;
using System.Runtime.InterropServices;
using Sytem.Windows.Forms;
public class Win32ActivateWindow{
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool SetForegroundWindow(IntPtr hWnd);
}
"@
$assemblies = @(
    "system.windows.forms"
)
add-type -language csharp -typedefinition $source -referencedassemblies $assemblies
$hWnd = ($shell.windows() | ? {$_.Name -eq "Internet Explorer"} | ? {$_.LocationName -like $name} | select-object -first 1 ).HWND
if($hWnd -ne $null){
    [void][Win32ActivateWindow]::SetForegroundWindow($hWnd)
}

Simplified automation will be done by combining these methods.
Profile
I have technical job experience in enbedded software development and server side infrastructure/application engineering. I'm interested in programming and computer security.
Objective
To write down my technical knowledge in the place where I can access from anywhere. To share my program source code. To train my writing skill.
Link
  • LinkedIn (preparing)

  • Twitter

  • Facebook (preparing)

  • GitHub

  • StackOverFlow (preparing)

Archives