Powershell to manipulate host file

后端 未结 9 1939
我在风中等你
我在风中等你 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 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
    }
    

提交回复
热议问题