当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何使用 PowerShell 对 URL 进行编码和解码?

作者:精品下载站 日期:2024-12-14 21:16:56 浏览:16 分类:玩电脑

如何使用 PowerShell 对 URL 进行编码和解码?


要求:使用 PowerShell 对 SharePoint Online 中的 URL 进行编码或解码。

PowerShell 解码 URL:

URL 编码是将 URL 中的字符转换为可以通过 Internet 安全传输的格式的过程。解码是将编码字符转换回其原始形式的逆过程。在 PowerShell 中,可以使用 [System.Uri] 类及其方法来对 URL 进行编码和解码。

我们在 SharePoint 的各种场景中都需要解码的 URL。假设我们想要将文件 URL 作为参数传递给某个函数。


#Parameter
$URL = "https%3A%2F%2Fcrescent.sharepoint.com%2Fsites%2Fmarketing%2F2018%2FDocuments%2FInfo%20Mgmt%20v2%2Epdf"

#Decode URL
[System.Web.HttpUtility]::UrlDecode($URL)

#Output: 
https://crescent.sharepoint.com/sites/marketing/2018/Documents/Info Mgmt v2.pdf

我们还可以使用 [System.URI] 类中的 UnescapeDataString 方法。


$EncodedURL = "https%3A%2F%2Fcrescent.sharepoint.com%2Fpersonal%2Fsalaudeen_crescent_com%Documents%2FAssets%20%26%20Inventory.xlsx"

[system.uri]::UnescapeDataString($EncodedURL)

使用 PowerShell 解码 SharePoint Online URL:

我们还可以使用 SharePoint Online 方法来解码 URL:


#Import PoweShell Module for SharePoint Online
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking

#Function to Decode URL
Function Decode-URL([string]$URL)
{
    Try {
        #Decode the URL
        Return [Microsoft.SharePoint.Client.Utilities.HttpUtility]::UrlKeyValueDecode($URL)
    }
    catch {
        Return "Error Getting De-codedURL: $($_.Exception.Message)"
    }
}

#Parameter
$URL = "https%3A%2F%2Fcrescent.sharepoint.com%2Fsites%2Fmarketing%2F2018%2FShared%20Documents%2FInformation%20Management%20v2%2Epdf"

#Call the function to decode URL
Decode-URL $URL 

在另一种情况下,我必须计算 SharePoint 中编码 URL 的长度并通过以下方式实现:


$URL = "https://crescent.sharepoint.com/personal/salaudeen_crescent_com/Shared Documents/Assets & Inventory.xlsx"
Write-host([URI]::EscapeUriString($URL)) 

要解码 SharePoint URL,您还可以使用在线工具,例如:

  • https://www.urldecoder.org
  • https://meyerweb.com/eric/tools/dencoder/

[玩转系统] 如何使用 PowerShell 对 URL 进行编码和解码?

总之,使用 PowerShell 对 URL 进行编码和解码是一个简单的过程,可以使用 [System.Uri] 类及其方法来执行。 EscapeDataString() 方法可用于对 URL 进行编码,而 UnescapeDataString() 方法可用于对 URL 进行解码。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯