40 lines
1.2 KiB
PowerShell
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."
|
|
}
|
|
} |