i'm trying to write a powershell script file that has Parameter
s whose types require loading. A simple code example is below: [System.Windows.Forms.MessageBoxButtons]
requires loading of system.windows.forms
.
The problem is, the Param(...)
block must be the very first in the script file. So:
- i cannot place
Add-Type
as the first line in the file. - i tried with
using assembly system.windows.forms
but it errors out saying:Cannot load assembly 'System.Windows.Forms'
. I think it could be possible by explicitly writing the dll file path, but it's ugly and not device-agnostic
So what can i do? Here's the code sample.
messagebox.ps1
# Add-Type -AssemblyName system.windows.forms # DOESN'T WORK, can't be placed before Param()
# using assembly System.Windows.Forms # DOESN'T WORK, can't find the assembly to load
Param(
[string] $Text = '',
[string] $Caption = '',
[System.Windows.Forms.MessageBoxButtons] $Buttons = [System.Windows.Forms.MessageBoxButtons]::OK # REPORTS: Unable to find type [System.Windows.Forms.MessageBoxButtons].
)
[System.Windows.Forms.MessageBox]::Show($Text, $Caption, $Buttons)
Thanks
A similar question (about a user defined type, instead of a system type): Powershell script Param block validation requires a type defined in another script