When using powershell 5.1.19041.1682
, I cannot use [System.Web.HttpUtility]
inside a class method:
Add-Type -AssemblyName System.Web
# This works fine
[System.Web.HttpUtility]::UrlEncode("foo")
class Test {
[void] Doit() {
# This creates the error below
[System.Web.HttpUtility]::UrlEncode("bar")
}
}
$d = [Test]::new()
$d.Doit()
Here is the output:
At C:\doit.ps1:7 char:6
+ [System.Web.HttpUtility]::UrlEncode("bar")
+ ~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Web.HttpUtility].
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TypeNotFound
On the latest powershell (7.3) this problem does not happen.
If I comment the call inside Doit()
it correctly prints "foo".
Can someone explain what is going on with 5.1? Is there a way around it?