1

I have a .psm1 module in my default powershell module path. I create a class in it but for some reason it doesn't respect my "Add-Type"

#file: MyTest.psm1


Class MyWinUtils
{
    
    
    Static [Void] MoveMouse([Int]$x, [Int]$y)
    { 
    
        Add-Type -AssemblyName System.Windows.Forms
        Add-type -AssemblyName System.Drawing
        [System.Windows.Forms.Cursor]::Position = `
        New-Object System.Drawing.Point( $x, $y)
        
        #Write-Host "Move Mouse to x=$x y=$y"
    }
    
    Static [Void] GetMousePosition()
    {
        $X = [System.Windows.Forms.Cursor]::Position.X
        $Y = [System.Windows.Forms.Cursor]::Position.Y
        Write-Host "X: $X | Y: $Y"
    }
} 

Then I did below in command line: using module MyTest

The error message is

+         [System.Windows.Forms.Cursor]::Position = `
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.Cursor].

I also tried to move the Add type to the beginning of the psm1 file the same error Can you please shed some light on it?

Ginger_Chacha
  • 317
  • 1
  • 11
  • 1
    Answered already here - https://stackoverflow.com/a/42839957/11954025 – Daniel Feb 24 '21 at 01:16
  • Thanks @Daniel. The thread helped me! I still have a question for the .dll import part which I will ask in the original question. – Ginger_Chacha Feb 24 '21 at 20:20
  • The short of it is that a reference to a type via a _type literal_ (e.g., `[System.Windows.Forms.Form]`) inside a `class` definition or in a script file's (`*.ps1`) parameter-definition block (`param(...)`) fails, if the referenced type isn't already loaded _before_ execution begins. That is why attempts to load assemblies _from inside the same file_ do not work, which up to at least PowerShell 7.3.6 also applies to the `using assembly` and `using module` statements, unfortunately. See the linked duplicate for details and workarounds. – mklement0 Aug 01 '23 at 14:22

1 Answers1

0

you left the back tick character after"::position ` "

I don't know what it does, but I did rewrite your class... remove the static part...

Class MyWinUtils
{
    
    
    [Void] MoveMouse([Int]$x, [Int]$y)
    { 
    
        Add-Type -AssemblyName System.Windows.Forms
        Add-type -AssemblyName System.Drawing

        $Pos = [System.Windows.Forms.Cursor]::Position

        $point =New-Object System.Drawing.Point( $x, $y)
        [System.Windows.Forms.Cursor]::Position = $point
        
        
        #Write-Host "Move Mouse to x=$x y=$y"
    }
    
    [Void] GetMousePosition()
    {
        $X = [System.Windows.Forms.Cursor]::Position.X
        $Y = [System.Windows.Forms.Cursor]::Position.Y
        Write-Host "X: $X | Y: $Y"
    }
} 

and then over here in a .ps1 file call it out.

using assembly System.Drawing

using assembly System.Windows.Forms
     


Using module '.\test.psm1'
$newTest = [MyWinUtils]::new()
$newTest.MoveMouse(500,500)
$newTest.GetMousePosition()
Dana M
  • 1
  • 4
  • The backtick (`\``) in the OP's code is a _line (statement) continuation_. While it isn't _necessary_ - because a line ending in `=` _implicitly_ causes PowerShell to look for the end of the statement on the next line - it works, so there's nothing to fix. There's also no good reason to switch the static methods to instance methods. – mklement0 Jul 31 '23 at 23:03