无法在PowerShell中找到类型[System.Web.HttpUtility]

Dav*_*ley 16 powershell azure microsoft-translator

我正在尝试使用PowerShell获取Microsoft Translator应用程序的访问令牌,但由于该错误,该过程中的某些命令失败:

Unable to find type [System.Web.HttpUtility]
Run Code Online (Sandbox Code Playgroud)

首先我输入了代码,但是如果我将代码直接从MSDN页面复制粘贴到PowerShell ISE中(并替换缺少的值),则显示相同的错误:

# ...
$ClientID = '<Your Value Here From Registered Application>'
$client_Secret = ‘<Your Registered Application client_secret>'

# If ClientId or Client_Secret has special characters, UrlEncode before sending request
$clientIDEncoded = [System.Web.HttpUtility]::UrlEncode($ClientID)
$client_SecretEncoded = [System.Web.HttpUtility]::UrlEncode($client_Secret)
# ...
Run Code Online (Sandbox Code Playgroud)

我是PowerShell的新手(通常使用Linux进行开发)但我的猜测是,这应该是开箱即用的,而不必安装其他工具; 如果没有,我在哪里可以找到它们?

von*_*ryz 45

您需要加载System.Web程序集.Add-Type像这样使用cmdlet,

PS C:\> [System.Web.HttpUtility]::UrlEncode("www.google.com")
Unable to find type [System.Web.HttpUtility].

PS C:\> Add-Type -AssemblyName System.Web
PS C:\> [System.Web.HttpUtility]::UrlEncode("www.google.com")
www.google.com
Run Code Online (Sandbox Code Playgroud)