Vscode-powershell: Debugger cannot find [System.Drawing.Color] when in script

Created on 7 Jul 2016  路  7Comments  路  Source: PowerShell/vscode-powershell

When debugging a script that contains a Color type it throws an error.

Example is using New-SlackMessageAttachment -Color $([System.Drawing.Color]::red)

Unable to find type [System.Drawing.Color].
+ New-SlackMessageAttachment -Color $([System.Drawing.Color]::red) `
+                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Drawing.Color:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

Unable to find type [System.Drawing.Color].
+ ... SlackMessageAttachment -Color $([System.Drawing.Color]::LightGreen) `
+                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Drawing.Color:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

Most helpful comment

You need to load that assembly Add-Type -Assembly System.Drawing

All 7 comments

You need to load that assembly Add-Type -Assembly System.Drawing

Why does the debugger need to load it? Running in any other context does
not require it to be loaded.

Well, if I fire up a PowerShell v5 console with -NoProfile it doesn't have the assembly loaded either:

PS C:\Users\Keith> [System.Drawing.Color]::LightGreen
Unable to find type [System.Drawing.Color].
At line:1 char:1
+ [System.Drawing.Color]::LightGreen
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Drawing.Color:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

However, once I execute Out-GridView that assembly is loaded. It would also be loaded by default for ISE since ISE is a WPF app.

I wouldn't rely on PowerShell loading an assembly meant for GUI apps. For instance, you could get the debugger to work by simply calling this command before referencing the color:

Microsoft.PowerShell.Management\Get-Clipboard | Out-Null

That command pulls in WinForms, which pulls in System.Drawing. IMO it is better to explicitly add the assembly.

Yep, that assembly is not loaded by default. Keith is correct, it gets loaded when anything UI-related gets loaded into the PowerShell session. Out-GridView is one way, running your code in the ISE is another way. If the Slack module depends on that type it needs to call Add-Type -Assembly System.Drawing when the module gets loaded.

If you start powershell.exe with -NoProfile and try to run that command, does it work?

This isn't actually a GUI app, but is sending a slack message. The color was for the color of the notification. I will just have to add the type before hand.

It doesn't have to be a GUI app. I have PSReadline handler function loaded in my profile that loads WinForms to copy stuff to the clipboard. When testing, I try to always fire up PowerShell with -NoProfile to make sure that I'm testing in more of a "clean state".

Closed issue as "by design".

Was this page helpful?
0 / 5 - 0 ratings