Powershell 中的 JavaScript 编码 URI 等效项?

lee*_*d00 3 javascript powershell encodeuricomponent

encodeURI()Powershell 中的 Javascripts /相当于什么encodURIComponent()

我正在对 URL 进行编码(%20其中需要一些 s),但我讨厌手动执行此操作。

Adm*_*ngs 10

对于这些情况,您可以使用System.Uri类。

等效encodeURI()方法是使用EscapeUriString类中的静态方法或将 URI 字符串转换为System.URI类型并访问该AbsoluteUri属性。

$uri = 'https://example.com/string with spaces'
# Method 1
[uri]::EscapeUriString($uri)
# Method 2
([uri]$uri).AbsoluteUri

# Output
https://example.com/string%20with%20spaces
Run Code Online (Sandbox Code Playgroud)

encodeURIComponent()可以使用类的方法完成等效操作EscapeDataString

$uri = 'https://example.com/string with space&OtherThings=?'
[uri]::EscapeDataString($uri)

#Output

https%3A%2F%2Fexample.com%2Fstring%20with%20space%26OtherThings%3D%3F
Run Code Online (Sandbox Code Playgroud)

注意:您不必定义变量($uri在本例中)。您只需将其替换为带引号的字符串即可。我仅使用该变量来提高可读性。