37 lines
1.1 KiB
PowerShell
37 lines
1.1 KiB
PowerShell
function Save-CompleteHostsFile {
|
|
<#
|
|
.SYNOPSIS
|
|
Used to save a list of hosts records to the hosts file. Will overwrite all other information in the file. Use with discretion.
|
|
|
|
.PARAMETER Records
|
|
A list of records of the format @{ Keep = $false; IpAddress = $null; Hostname = $null; Comment = $null; BlankLine = $false; }
|
|
|
|
.PARAMETER RemoveDuplicates
|
|
Remove duplicates before saving
|
|
#>
|
|
[CmdletBinding()]
|
|
param (
|
|
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
|
|
[object[]]$Records,
|
|
[switch]$RemoveDuplicates
|
|
)
|
|
|
|
$logLead = Get-LogLeadName
|
|
|
|
if (Test-IsCollectionNullOrEmpty $Records) {
|
|
throw "$logLead : no records found to write to the hosts file"
|
|
}
|
|
|
|
if (($null -eq $Records.IpAddress) -or ($null -eq $Records.Hostname)) {
|
|
throw "$logLead : no records found with ipaddress or hostname, can not continue"
|
|
}
|
|
|
|
if ($RemoveDuplicates) {
|
|
# Reuse variable is frowned upon
|
|
$Records = Remove-DuplicateHostsFileRecords -Record $Records
|
|
}
|
|
|
|
$lines = Format-HostsFileRecord -Record $Records
|
|
|
|
$lines | Out-File (Get-HostsFilePath) -Encoding ASCII
|
|
} |