ps/Modules/Alkami.PowerShell.SDK/Public/Set-HostFileTarget.ps1
2023-05-30 22:51:22 -07:00

57 lines
2.2 KiB
PowerShell

function Set-HostFileTarget {
<#
.SYNOPSIS
Allows a user to quickly switch their host file to target either localhost or the remote for dev environment URLs
.DESCRIPTION
This function lets users swap between either having their host file point at the local environment (aka localhost)
or not when resolving dev environment URLs. This allows developers to do local development of services and quickly
switch to testing on the dev environments. This is accomplished by commenting and uncommenting any of the lines in
the host file that point at 127.0.0.1 and have a url in the form of "*.dev.alkamitech.com"
.PARAMETER Remote
Switch to say to point to the remote when accessing these URLs. Either this switch or the -Local switch must be
specified, but not both
.PARAMETER Local
Switch to say to point to the localhost when accessing these URLs. Either this switch or the -Local switch must be
specified, but not both
.EXAMPLE
Set-HostFileTarget -Local
.EXAMPLE
Set-HostFileTarget -Remote
#>
[CmdletBinding()]
[OutputType([void])]
param (
# CmdletBinding gives you these neat attributes to use
[Parameter(ParameterSetName = 'Remote')]
[switch]$Remote,
[Parameter(ParameterSetName = 'Local')]
[switch]$Local
)
$HostFilePath = "$env:windir\System32\drivers\etc\hosts"
$HostFile = Get-Content $HostFilePath
if ($Local) {
$HostFile | Foreach {
# Things that are commented out should become uncommented
if ($_.StartsWith('#127.0.0.1') -and $_.EndsWith('dev.alkamitech.com') -and -not $_.Contains('orion.dev.alkamitech.com') -and -not $_.Contains('local.dev.alkamitech.com')) {
$_.Replace('#', '')
} else {$_}
} | Out-File $HostFilePath -enc ascii
} else {
$HostFile | Foreach {
# Things that are not commented out should become commented
if ($_.StartsWith('127.0.0.1') -and $_.EndsWith('dev.alkamitech.com') -and -not $_.Contains('orion.dev.alkamitech.com') -and -not $_.Contains('local.dev.alkamitech.com')){
"#" + $_
} else {$_}
} | Out-File $HostFilePath -enc ascii
}
}