57 lines
1.7 KiB
PowerShell
57 lines
1.7 KiB
PowerShell
|
function Add-HostsFileEntry {
|
||
|
<#
|
||
|
.SYNOPSIS
|
||
|
Updates or adds a specific hosts file entry to the hosts file
|
||
|
|
||
|
.LINK
|
||
|
Get-KnownDeveloperHostsEntries
|
||
|
#>
|
||
|
[CmdletBinding()]
|
||
|
param (
|
||
|
[Parameter(Mandatory = $true)]
|
||
|
[ValidateNotNullOrEmpty()]
|
||
|
[string]$IpAddress,
|
||
|
[Parameter(Mandatory = $true)]
|
||
|
[ValidateNotNullOrEmpty()]
|
||
|
[string]$Hostname,
|
||
|
[Parameter(Mandatory = $false)]
|
||
|
[string]$Comment
|
||
|
)
|
||
|
|
||
|
$logLead = Get-LogLeadName
|
||
|
|
||
|
$records = @()
|
||
|
$existingRecords = Get-HostsFileAllRecords
|
||
|
|
||
|
$foundPriorRecord = $false
|
||
|
foreach ($record in $existingRecords) {
|
||
|
if ($record.Hostname -eq $Hostname) {
|
||
|
$foundPriorRecord = $true
|
||
|
|
||
|
$record.IpAddress = $IpAddress
|
||
|
# Only update the comment if we passed one in. Ignore the previous comment and overwrite (frequently this is for adding jira tickets on changes)
|
||
|
# We could alternately prepend this comment to the existing comment, with a comma+space separator
|
||
|
if (![string]::IsNullOrWhiteSpace($Comment)) {
|
||
|
$record.Comment = $Comment
|
||
|
}
|
||
|
|
||
|
$formattedRecord = Format-HostsFileRecord -Record $record
|
||
|
|
||
|
Write-Host "$logLead : Updating record for $formattedRecord"
|
||
|
}
|
||
|
$records += $record
|
||
|
}
|
||
|
|
||
|
if (!$foundPriorRecord) {
|
||
|
$newRecord = New-HostsFileEntry -IpAddress $IpAddress -Hostname $Hostname -Comment $Comment
|
||
|
$formattedRecord = Format-HostsFileRecord -Record $record
|
||
|
|
||
|
Write-Host "$logLead : Adding record for $formattedRecord"
|
||
|
|
||
|
$records += $newRecord
|
||
|
}
|
||
|
|
||
|
Save-CompleteHostsFile -Record $records
|
||
|
}
|
||
|
|
||
|
Set-Alias -Name Update-HostsFileEntry -Value Add-HostsFileEntry
|