ps/Modules/Alkami.PowerShell.Common/Public/Format-Url.ps1
2023-05-30 22:51:22 -07:00

41 lines
990 B
PowerShell

function Format-Url {
<#
.SYNOPSIS
Removes preceeding http(s) prefixes and trailing slashes from URLs
.DESCRIPTION
Removes preceeding http(s) prefixes and trailing slashes from URLs. Does not break down to the host name alone
.PARAMETER url
[string] The URL to Clean
.EXAMPLE
Format-Url "https://foo.bar.com/"
foo.bar.com
#>
[CmdletBinding()]
[OutputType([System.String])]
Param(
[Parameter(Mandatory)]
[String]$url
)
$logLead = (Get-LogLeadName)
[Regex]$urlCleanerRegex = "^http(s)?:\/\/|\/$"
Write-Verbose "$logLead : Cleaning URL $url with regex $urlCleanerRegex"
if (!($urlCleanerRegex.IsMatch($url.Trim()))) {
Write-Host "$logLead : URL $url Doesn't Need Cleaning."
return $url
}
$cleansedUrl = $urlCleanerRegex.Replace($url.Trim(), "") | Select-Object -First 1
Write-Verbose "$logLead : Cleansed URL is: $cleansedUrl"
return $cleansedUrl
}
Set-Alias -name Clean-Url -value Format-Url;