Skip to content

Latest commit

 

History

History
145 lines (115 loc) · 3.65 KB

powershell.md

File metadata and controls

145 lines (115 loc) · 3.65 KB

Powershell Examples

GetForegroundWindow

Get the foreground window at the time of the call. The value returned is a IntPtr or a window handle, (32bit integer really).

  $signature = @"
    [DllImport("user32.dll")]
    public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,int Y, int cx, int cy, uint uFlags);

    static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);

    const UInt32 SWP_NOSIZE = 0x0001;
    const UInt32 SWP_NOMOVE = 0x0002;
    const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;

    public static void MakeTopMost (IntPtr fHandle)
    {
            SetWindowPos(fHandle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
    }
  "@

  $app = Add-Type -MemberDefinition $signature -Name Win32Window -Namespace ScriptFanatic.WinAPI -ReferencedAssemblies System.Windows.Forms -Using System.Windows.Forms -PassThru

  $hwnd = $am.GetForegroundWindow()
  if($hWnd -ne 0)
  {
    $null = $app::MakeTopMost($hWnd)
  }
  else
  {
    Write-Host "$hWnd is 0"
  }

Log messages

  $am.Log( 1, "Hello from powershell" )

or, (to prevent output).

    [void]($am.Log( 1, "Hello from powershell" ))

The log messages are:

  • Success = 1
  • Error = 2
  • Warning = 3
  • Message = 4
  • System = 5

Say

The first parameter is a string, then the amount of time in ml the message will appear for, and the last parameter is the fade speed, (if zero it will not fade).

  # Say message
  [void]($am.Say( "Hello world", 3000, 5 ))

Currently selected string

  Try
  {
    $string = $am.GetString( $false ) # throw if nothing selected.
    $stringQuoted = $am.GetString( $true )
  }
  Catch [ArgumentOutOfRangeException]
  {
    # Caught exception!
    $string = "";
    $stringQuoted = "";
  }
  Catch
  {
    # Caught another exception!
    $string = "";
    $stringQuoted = "";
  }

Get Folder/File/Url

  Try
  {
    $file = $am.GetFile( 0, $false ) # throw if nothing selected.
    # $am.GetFolder( 0, $false ) # throw if nothing selected.
    # $am.GetUrl( 0, $false ) # throw if nothing selected.
  }
  Catch [ArgumentOutOfRangeException]
  {
    # No file at this index.
    $file = ""
  }
  Catch
  {
    Write-Host "No files/Folder/Url selected"
  }

Exceptions

  • Say, None.
    • Exception if the Elapse parameter is invalid, (cannot be zero).
    • Exception if the Fade parameter is invalid.
  • Version, None.
  • GetCommand, ArgumentOutOfRangeException if the command index does not exist.
  • GetAction, Exception if the current action name cannot be found.
  • GetCommandCount, None.
  • Execute, None.
  • Getstring, Exception if we have no selected string.
  • Getfile, ArgumentOutOfRangeException if there is no file at the index.
  • Getfolder, ArgumentOutOfRangeException if there is no folder at the index.
  • Geturl, ArgumentOutOfRangeException if there is no url at the index.
  • GetVersion, None
  • Log, None.
  • AddAction, None.
  • RemoveAction, None.
  • FindAction, ArgumentOutOfRangeException if the action at the index does not exist.

Note: In some cases, an Exception will be thrown if there are other problem, but the main reason would be because ActionMonitor.exe was closed while the script itself is still running.

You can check the log file for error/warning messages.

How to Debug

  • Comandline arguments: -noexit -command "&{ import-module .\ActionMonitor.dll -Verbose"}"
  • Start External program: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Then in your powershell script:

# Create a object
$am = New-Object Am.Core 'UniqueValue`

But of course this will not really help as you cann Say( ... ) anything for example as you are not connected.