Environment: Win 10 64 bit, PS version 7.1
I had a module and I decided to move everything into class based. To use the assembly I need to load the .dll etc via the workaround psd1 file.
After look at the thread: Powershell: Unable to find type when using PS 5 classes
**Per comment from @mklement0 the .dll can't be loaded as the same way as the assmebly 'System.Windows.Forms'
updated per suggestion from @mklement0 but it still didn't work This is what it looks like after the update:
H:\PowerShell\Modules\TestUtils
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2/26/2021 6:25 AM 4168 TestUtils.psd1
-a--- 2/26/2021 6:22 AM 449 TestUtils.psm1
and TestUtils.psd1 was created by command:
New-ModuleManifest TestUtils.psd1 -RootModule TestUtils.psm1 The content in .psm1 is as below:
Add-Type -name NativeMethods -namespace Win32 -MemberDefinition @'
[DllImport("user32.dll")]
public static extern void mouse_event(int flags, int dx, int dy, int cButtons, int info);
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
'@
Class MyWinUtils
{
Static [Void] ClickLeftMouse([int]$x,[int]$y)
{
[Win32.NativeMethods]::mouse_event(6,0,0,0,0)
}
}
When I load the module the error popped up:
using module TestUtils
InvalidOperation: At TestUtils.psm1:15 char:5
+ [Win32.NativeMethods]::mouse_event(6,0,0,0,0)
+ ~~~~~~~~~~~~~~~~~~~
Unable to find type [Win32.NativeMethods].
Thanks