Created
January 18, 2019 21:04
-
-
Save gavi/de180fdcfcd6b4cc4b1479974688d0f1 to your computer and use it in GitHub Desktop.
Revisions
-
gavi created this gist
Jan 18, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ function AddHostEntry($fqdn){ $Pattern = '^(?<IP>\d{1,3}(\.\d{1,3}){3})\s+(?<Host>.+)$' $File = "$env:SystemDrive\Windows\System32\Drivers\etc\hosts" $Entries = @() $found = $false (Get-Content -Path $File) | ForEach-Object { If ($_ -match $Pattern) { #Write-Host "$($Matches.IP),$($Matches.Host)" -ForegroundColor Green if($Matches.Host -eq $fqdn){ $found = $true } } } if($found){ Write-Host "Entry Exists" -ForegroundColor Green } else{ Write-Host "Adding Entry $($fqdn)" -ForegroundColor Green Add-Content $File "`r`127.0.0.1 $($fqdn) " } }