Powershell to manipulate host file

后端 未结 9 1729
我在风中等你
我在风中等你 2021-01-30 02:32

I am looking at to see if I can create powershell script to update the contents in the host file.

Anybody know if there are any examples that manipulate the host file u

相关标签:
9条回答
  • 2021-01-30 02:55

    For me the biggest pain in dealing with the hosts file is remembering where it is. I set a variable that points to my hosts file in my PowerShell profile, which makes it easy to edit in a text editor.

    In PowerShell, type the following to open your profile:

    C:\> Notepad $profile
    

    Add this:

    $hosts = "$env:windir\System32\drivers\etc\hosts"
    

    Save the file, then close and re-open PowerShell, running as administrator. You can't edit the hosts file without elevated permissions.

    Now you can edit your hosts file the same way you'd edit your profile:

    C:\> Notepad $hosts
    
    0 讨论(0)
  • 2021-01-30 03:03

    I wrote a quick script that creates a simple GUI for adding new records to the HOSTS file. It will open a window, ask for hostname and IP, then append your input to the HOSTS file.

    I'm sure it could be simplified and look cleaner... but works fine for my use case.

    Enjoy!

    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
    
    $hostsfilelocation = "$env:windir\System32\drivers\etc\hosts"
    $readhostsfile = Get-Content $hostsfilelocation
    
    $form = New-Object System.Windows.Forms.Form
    $form.Text = 'Update HOSTS File'
    $form.Size = New-Object System.Drawing.Size(300,200)
    $form.StartPosition = 'CenterScreen'
    
    $AddHosts = New-Object System.Windows.Forms.Button
    $AddHosts.Location = New-Object System.Drawing.Point(55,120)
    $AddHosts.Size = New-Object System.Drawing.Size(90,25)
    $AddHosts.Text = 'Add Record'
    $AddHosts.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $form.AcceptButton = $AddHosts
    $form.Controls.Add($AddHosts)
    
    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Point(170,120)
    $CancelButton.Size = New-Object System.Drawing.Size(75,25)
    $CancelButton.Text = 'Cancel'
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $form.CancelButton = $CancelButton
    $form.Controls.Add($CancelButton)
    
    $Hostslabel = New-Object System.Windows.Forms.Label
    $Hostslabel.Location = New-Object System.Drawing.Point(10,20)
    $Hostslabel.Size = New-Object System.Drawing.Size(280,20)
    $Hostslabel.Text = 'Enter New HOSTNAME/FQDN:'
    $form.Controls.Add($Hostslabel)
    
    $HoststextBox = New-Object System.Windows.Forms.TextBox
    $HoststextBox.Location = New-Object System.Drawing.Point(10,40)
    $HoststextBox.Size = New-Object System.Drawing.Size(260,20)
    $form.Controls.Add($HoststextBox)
    
    $IPlabel = New-Object System.Windows.Forms.Label
    $IPlabel.Location = New-Object System.Drawing.Point(10,60)
    $IPlabel.Size = New-Object System.Drawing.Size(280,20)
    $IPlabel.Text = 'Enter IP:'
    $form.Controls.Add($IPlabel)
    
    $IPtextBox = New-Object System.Windows.Forms.TextBox
    $IPtextBox.Location = New-Object System.Drawing.Point(10,80)
    $IPtextBox.Size = New-Object System.Drawing.Size(260,20)
    $form.Controls.Add($IPtextBox)
    
    $form.Topmost = $true
    
    $form.Add_Shown({($HoststextBox,$IPtextbox).Select()})
    
    $result = $form.ShowDialog()
    
    if ($result -eq [System.Windows.Forms.DialogResult]::OK)
    {
        $inputhosts = $HoststextBox.Text
        $inputip = $IPtextBox.Text
        $newrecord = "$inputip $inputhosts"
        Add-Content -Path $hostsfilelocation -Value $newrecord
    }
    
    0 讨论(0)
  • 2021-01-30 03:05

    Starting with Kevin Remisoski's excellent answer above, I came up with this which lets me add/update multiple entries at once. I also changed the regex in the split to look for any white space, not just tab.

    function setHostEntries([hashtable] $entries) {
        $hostsFile = "$env:windir\System32\drivers\etc\hosts"
        $newLines = @()
    
        $c = Get-Content -Path $hostsFile
        foreach ($line in $c) {
            $bits = [regex]::Split($line, "\s+")
            if ($bits.count -eq 2) {
                $match = $NULL
                ForEach($entry in $entries.GetEnumerator()) {
                    if($bits[1] -eq $entry.Key) {
                        $newLines += ($entry.Value + '     ' + $entry.Key)
                        Write-Host Replacing HOSTS entry for $entry.Key
                        $match = $entry.Key
                        break
                    }
                }
                if($match -eq $NULL) {
                    $newLines += $line
                } else {
                    $entries.Remove($match)
                }
            } else {
                $newLines += $line
            }
        }
    
        foreach($entry in $entries.GetEnumerator()) {
            Write-Host Adding HOSTS entry for $entry.Key
            $newLines += $entry.Value + '     ' + $entry.Key
        }
    
        Write-Host Saving $hostsFile
        Clear-Content $hostsFile
        foreach ($line in $newLines) {
            $line | Out-File -encoding ASCII -append $hostsFile
        }
    }
    
    $entries = @{
        'aaa.foo.local' = "127.0.0.1"
        'bbb.foo.local' = "127.0.0.1"
        'ccc.foo.local' = "127.0.0.1"
    };
    setHostEntries($entries)
    
    0 讨论(0)
提交回复
热议问题