Created
October 26, 2019 21:20
-
-
Save smaglio81/a27cac3bc812898c87fb737313c924e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Parses a uri to replace environment information. This is specific for web urls. | |
Used to generate environment specific uris. | |
.PARAMETER Environment | |
The environment to retrieve variables for. Expected values are Dev, Test, | |
Prod, and Local | |
.PARAMETER Url | |
The url containing '.{env}' or '{ext-env}'. | |
'.{env}' will be replaced with the environment specific information. | |
'{ext-env}' will be replaced with the environment specific information. | |
.EXAMPLE | |
$url = Get-WebEnvironmentUri -Environment Dev -Url "http://unittest.{env}.yourcompany.com/" | |
# sets $url to 'http://unittest.dev.yourcompany.com/' | |
.EXAMPLE | |
$url = Get-WebEnvironmentUri -Environment Prod -Url "http://unittest.{env}.yourcompany.com/" | |
# sets $url to 'http://unittest.yourcompany.com/' | |
.EXAMPLE | |
$url = Get-WebEnvironmentUri -Environment Prod -Url "http://unittest.{ext-env}.yourcompany.com/" | |
# sets $url to 'http://unittest.ext-prod.yourcompany.com/' | |
.LINK | |
ConvertTo-WebUrl | |
.NOTES | |
This is distinct from ConvertTo-WebUrl, which converts a UriPaths objects into an url. | |
#> | |
Function Get-WebEnvironmentUri { | |
[CmdletBinding()] | |
Param ( | |
[ValidateSet("Prod","Test","Dev","Local","")] | |
[string] $Environment = "Local", | |
[Alias("Uri")] | |
[string] $Url = "" | |
) | |
# provides validation | |
if($Environment -eq "") { $Environment = "Local"; } | |
if($Url.Contains("{env}")) { | |
switch($Environment) { | |
"Prod" { | |
if($Url.Contains(".{env}")) { | |
$Url = $Url.Replace(".{env}", ""); # converts http://www.{env}.sa.ucsb.edu/ | |
} else { | |
if($Url.Contains("/{env}.")) { | |
$Url = $Url.Replace("{env}.", ""); # converts http://{env}.duels.lsaa.ucsb.edu/ | |
} else { | |
$Url = $Url.Replace("{env}", ""); # converts http://nonnorm.sa.ucsb.edu/{env}/ -- shouldn't be done | |
} | |
} | |
} | |
default { $Url = $Url.Replace("{env}", $Environment.ToLower()) } | |
} | |
} | |
# The following is for external addresses | |
elseif ($Url.Contains("{ext-env}")) { | |
$Url = $Url.Replace("{ext-env}", "ext-$($Environment.ToLower())") # the standard is ext-dev, ext-test, and ext-prod | |
} | |
return $Url | |
} | |
Set-Alias ConvertTo-WebEnvironmentUri Get-WebEnvironmentUri | |
Set-Alias ConvertTo-WebEnvironmentUrl Get-WebEnvironmentUri | |
Set-Alias Get-WebEnvironmentUrl Get-WebEnvironmentUri |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment