41 lines
990 B
PowerShell
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; |