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

95 lines
3.7 KiB
PowerShell

function ConvertTo-HostsFileEntry {
<#
.SYNOPSIS
Convert the given record to a defined Hosts File Entry
.OUTPUTS
Returns a [object[]] of hosts entries.
#>
[CmdletBinding(DefaultParameterSetName = 'FromPipeline')]
[OutputType([string[]])]
param (
[Parameter(Mandatory = $false, ValueFromPipeline = $true, Position = 0, ParameterSetName = 'FromPipeline')]
[string]$RawRecord,
[Parameter(Mandatory = $true, ParameterSetName = 'Entries')]
[ValidateNotNullOrEmpty()]
[string]$IpAddress,
[Parameter(Mandatory = $true, ParameterSetName = 'Entries')]
[ValidateNotNullOrEmpty()]
[string]$Hostname,
[Parameter(Mandatory = $false, ParameterSetName = 'Entries')]
[string]$Comment
)
begin {
# Define the defaults for the HostsFileEntry response to add some custom display information
$defaultTypeName = 'HostsFileEntry'
$defaultKeys = @('IPAddress')
$defaultDisplaySet = @('IPAddress', 'Hostname', 'IsDisabled', 'Comment')
$defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet',[string[]]$defaultDisplaySet)
$defaultKeyPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultKeyPropertySet',[string[]]$defaultKeys)
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($defaultDisplayPropertySet,$defaultKeyPropertySet)
$disabledKey = "#DISABLED#"
}
process {
$workingIpAddress = $IpAddress
$workingHostname = $Hostname
$workingComment = $Comment
$isDisabled = $false
$commentSeparator = -1
if ($PSCmdlet.ParameterSetName -eq 'FromPipeline') {
$RawRecord = $RawRecord.Trim()
if ($RawRecord.StartsWith($disabledKey)) {
$isDisabled = $true
$RawRecord = $RawRecord.Substring($disabledKey.Length).Trim()
}
$commentSeparator = $RawRecord.IndexOf("#")
$workingComment = ""
$keep = $false
if ($commentSeparator -gt -1) {
$splits = $RawRecord -split '#',2
$workingComment = $splits[1].Trim()
$RawRecord = $splits[0].Trim()
}
if ($RawRecord.length -gt 0) {
$bits = [regex]::Split($RawRecord, "\s+")
if ($bits.count -gt 1) {
$workingIpAddress = $bits[0].Trim()
$workingHostname = $bits[1].Trim()
}
}
}
$keep = (($workingComment -imatch 'keep') -or ($workingIpAddress -eq $null))
$blankLine = ([string]::IsNullOrWhiteSpace($workingComment) -and [string]::IsNullOrWhiteSpace($workingIpAddress) -and ($commentSeparator -eq -1))
try {
$hostEntryObject = New-Object PSCustomObject -Property @{
IpAddress = $workingIpAddress
Hostname = $workingHostname
Comment = $workingComment
Keep = $keep
BlankLine = $blankLine
IsDisabled = $isDisabled
}
#Give this object a unique typename
$hostEntryObject.PSObject.TypeNames.Insert(0,$defaultTypeName)
$hostEntryObject | Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $PSStandardMembers
return $hostEntryObject
} catch {
Write-Warning "$logLead : Could not convert item to HostEntry object. Check error below. Returning `$null"
Write-ErrorObject -ErrorItem $PSItem
return $null
}
}
}
Set-Alias -Name New-HostsFileEntry -Value ConvertTo-HostsFileEntry