ps/Modules/Cole.PowerShell.Developer/Public/Disable-HostsFileEntry.ps1
2023-05-30 22:51:22 -07:00

40 lines
1.2 KiB
PowerShell

function Disable-HostsFileEntry {
param (
[Parameter()]
[string]$Hostname,
[Parameter()]
[string]$IPAddress,
[Parameter(Mandatory = $true)]
$Comment
)
$logLead = Get-LogLeadName
$hostnameProvided = ![string]::IsNullOrWhiteSpace($Hostname)
$ipaddressProvided = ![string]::IsNullOrWhiteSpace($IPAddress)
if (!$hostnameProvided -and !$ipaddressProvided) {
throw "$logLead : Must provide either the hostname or ip address to disable"
}
$hostsEntries = Get-HostsFileAllRecords
$disabledCounter = 0
foreach ($record in $records) {
# can't disable an already disabled record
if (!$record.IsDisabled) {
if (($hostnameProvided -and ($record.Hostname -eq $Hostname)) -or ($ipaddressProvided -and ($record.IpAddress -eq $IPAddress))) {
$record.IsDisabled = $true
$disabledCounter += 1
}
}
}
if ($disabledCounter -gt 0) {
Write-Host "$logLead : Updated $disabledCounter records. Saving."
Save-CompleteHostsFile -Records $records
} else {
Write-Host "$logLead : No records found to disable for provided values."
}
}