Add-Type isn't working

Hi,

I’m using PowerShell Studio and getting this anoying error. I started the code in ISE and it was working fine but ported to PSS started getting this error. It would make sense to post on PSS forums but they are not working. Hoping someone can help.

This is one of the errors:
ERROR: At Line: 166 char: 29
ERROR: + [System.Uri]$configUrl = [System.Web.HttpUtility]::UrlDecode( …
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~
ERROR: Unable to find type [System.Web.HttpUtility].
ERROR:

This code goes something like this:

class MyClass: IDisposable
{
# props
MyClass()
{
	[System.Uri]$configUri = Get-ItemPropertyValue -Path 'HKLM:\SOFTWARE\Company' -Name 'ConfigUri'
	$this.init($configUri)
}
# another constructor
# dispose

hidden [void]init([System.Uri]$Uri)
{
	Add-Type -AssemblyName System.Web
	Add-Type @"
#<inline C# code that also is not working>
"@
	[System.Uri]$configUrl = [System.Web.HttpUtility]::UrlDecode($Uri)
	# some more code goes here
}
# a few other methods
}
function Main
{
$wp = [MyClass]::New()
}

I tried to use the Add-Type in the class method as is showing, the class constructors, the Main function, the top of the file and added teh system.web to the default editor assemblies.

This is a Windows 64bit Sapien PS v5 host silent project in STA mode and doesn’t require elevation.

Let me know if you need more details.

Any help is appreciated.

Cheers, C

C,
Welcome to the forum. :wave:t3:

Without having experience with the particular issue …

Have you tried to put the Add-Type command directly before you use the type? … like this:

Add-Type -AssemblyName System.Web
[System.Uri]$configUrl = [System.Web.HttpUtility]::UrlDecode($Uri)

A StackOverflow answer recommends the same:

Thanks Olaf.
The code is actually how I have it on the sample code and it’s just before the command. Only the inline Add-Type is in the middle but I tried to remove it and still getting the error.

The editor recognizes, intellisense works fine and running in console works too.

How about removing everything else what is not working and just have the two pieces you need for this particular part of the functionality to figure out what the issue is? :man_shrugging:t3:

For example … if I do this:

$URI = 'https://forums.powershell.org/t/add-type-isnt-working/22442'
Add-Type -AssemblyName System.Web
[System.Uri]$configUrl = [System.Web.HttpUtility]::UrlDecode($Uri)
$configUrl

I get this:

AbsolutePath   : /t/add-type-isnt-working/22442
AbsoluteUri    : https://forums.powershell.org/t/add-type-isnt-working/22442
LocalPath      : /t/add-type-isnt-working/22442
Authority      : forums.powershell.org
HostNameType   : Dns
IsDefaultPort  : True
IsFile         : False
IsLoopback     : False
PathAndQuery   : /t/add-type-isnt-working/22442
Segments       : {/, t/, add-type-isnt-working/, 22442}
IsUnc          : False
Host           : forums.powershell.org
Port           : 443
Query          : 
Fragment       : 
Scheme         : https
OriginalString : https://forums.powershell.org/t/add-type-isnt-working/22442
DnsSafeHost    : forums.powershell.org
IdnHost        : forums.powershell.org
IsAbsoluteUri  : True
UserEscaped    : False
UserInfo       :

Now I know this part is actually working. Now I’d add some more code and test each step.

Thanks Olaf.

I’ve been doing more troubleshooting and googling and found this:

This was 6 years ago and still not fixed so not very hopeful!

I need to take some time and test this properly and will let everyone know how it goes.

Cheers, C

1 Like

You may test with PS 7.x and switch if it works there. :man_shrugging:t3: :smirk:

Yeah, they say it’s fixed on PS7 but I’ve been procrastinating using PS7. there’a lot of interesting features, particularly parallelism but I can’t take time learning new PS or adjusting my scripts and for my line of work, it doesn’t make much sense as it’s not even generally deployed on devices.

I found that using [System.Uri]$configUrl = [System.Net.WebUtility]::UrlDecode($Uri) works fine so I’m using this instead. I don’t know why but won’t spend any more time around this!
As per my inline C# class, I changed it so I can instantiate and call the method instead and it works.

Instead of:

[myCsClass]::func1($param1, $param2)

I’m using:

$myVar = New-Object myCsClass
$myVar.func1($param1, $param2)

I appreciate you help Olaf :slight_smile:

Cheers,

C