PowerShell [uri] Types Accelerator 生成不同的 AbsoluteUri。

huangapple go评论58阅读模式
英文:

PowerShell [uri] Types Accelerator produces different AbsoluteUri

问题

我有一个本地文件($URLsFile),其中包含一组URL:

http://WIND451325/Dev/Application.aspx__%__dev.com
http://WIND451326/Dev/Application.aspx__%__dev.com

在使用[uri]类型加速器与Get-Content命令时:

[uri[]]$ArrayURLs = Get-Content -Path $URLsFile

我注意到AbsoluteUri属性的值发生了一些变化,__%__ 变成了 __%25__

foreach ($item in $ArrayURLs)
{
    $item.AbsoluteUri
}

http://WIND451325/Dev/Application.aspx__%25__dev.com
http://WIND451326/Dev/Application.aspx__%25__dev.com

在查阅官方文档之后:

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_type_accelerators?view=powershell-5.1

https://learn.microsoft.com/en-us/dotnet/api/system.uri?view=net-7.0

我没有找到与此主题相关的任何参考信息。

英文:

I have a local file ($URLsFile), which contains a list of URLs:

http://WIND451325/Dev/Application.aspx__%__dev.com
http://WIND451326/Dev/Application.aspx__%__dev.com

When using the [uri] Types Accelerator with the Get-Content command:

[uri[]]$ArrayURLs = Get-Content -Path $URLsFile

I've noticed some changes in the value of the AbsoluteUri property, as __%__ changed to __%25__ :

foreach ($item in $ArrayURLs)
{
    $item.AbsoluteUri
}

http://WIND451325/Dev/Application.aspx__%25__dev.com
http://WIND451326/Dev/Application.aspx__%25__dev.com

After going through the official documentation:

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_type_accelerators?view=powershell-5.1

https://learn.microsoft.com/en-us/dotnet/api/system.uri?view=net-7.0

I could not find any reference to the subject.

答案1

得分: 2

可以使用具有 `dontEscape` 参数设置为 `$true` 的[重载](https://learn.microsoft.com/en-us/dotnet/api/system.uri.-ctor?view=net-8.0#system-uri-ctor(system-string-system-boolean))。

$ArrayURLs = Get-Content -Path $URLsFile |
    ForEach-Object { [uri]::new($_, $true) }

在新版本的 PowerShell 7+ 中,此构造函数已被弃用,您应该使用UriCreationOptions 重载

$options = [System.UriCreationOptions]@{
    DangerousDisablePathAndQueryCanonicalization = $true
}

$ArrayURLs = Get-Content -Path $URLsFile |
    ForEach-Object { [uri]::new($_, [ref] $options) }
英文:

<!-- language-all: sh -->

You can use the overload with dontEscape set to $true.

$ArrayURLs = Get-Content -Path $URLsFile |
    ForEach-Object { [uri]::new($_, $true) }

This ctor is obsolete, in newer versions of PowerShell 7+, you should use the UriCreationOptions overload instead.

$options = [System.UriCreationOptions]@{
    DangerousDisablePathAndQueryCanonicalization = $true
}

$ArrayURLs = Get-Content -Path $URLsFile |
    ForEach-Object { [uri]::new($_, [ref] $options) }

huangapple
  • 本文由 发表于 2023年5月29日 18:19:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356488.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定