Powershell操纵主机文件

Ton*_*ony 43 powershell

我正在查看是否可以创建powershell脚本来更新主机文件中的内容.

有人知道是否有任何使用PowerShell或任何其他脚本语言操作主机文件的示例?

谢谢.

Kei*_*ill 33

首先,如果您使用的是Vista或Windows 7,请确保从提升的提示符运行这些命令:

# Uncomment lines with localhost on them:
$hostsPath = "$env:windir\System32\drivers\etc\hosts"
$hosts = get-content $hostsPath
$hosts = $hosts | Foreach {if ($_ -match '^\s*#\s*(.*?\d{1,3}.*?localhost.*)')
                           {$matches[1]} else {$_}}
$hosts | Out-File $hostsPath -enc ascii

# Comment lines with localhost on them:
$hosts = get-content $hostsPath
$hosts | Foreach {if ($_ -match '^\s*([^#].*?\d{1,3}.*?localhost.*)') 
                  {"# " + $matches[1]} else {$_}} |
         Out-File $hostsPath -enc ascii
Run Code Online (Sandbox Code Playgroud)

鉴于此,我认为你可以看到如何使用正则表达式来操作条目.


Aar*_*sen 25

碳模块有一个设置,HostsEntry用于设置主机入口函数:

Set-HostsEntry -IPAddress 10.2.3.4 -HostName 'myserver' -Description "myserver's IP address"
Run Code Online (Sandbox Code Playgroud)


Kev*_*ski 24

如果有人正在寻找更高级的例子,我一直特别喜欢这个要点:https: //gist.github.com/markembling/173887

#
# Powershell script for adding/removing/showing entries to the hosts file.
#
# Known limitations:
# - does not handle entries with comments afterwards ("<ip>    <host>    # comment")
#

$file = "C:\Windows\System32\drivers\etc\hosts"

function add-host([string]$filename, [string]$ip, [string]$hostname) {
    remove-host $filename $hostname
    $ip + "`t`t" + $hostname | Out-File -encoding ASCII -append $filename
}

function remove-host([string]$filename, [string]$hostname) {
    $c = Get-Content $filename
    $newLines = @()

    foreach ($line in $c) {
        $bits = [regex]::Split($line, "\t+")
        if ($bits.count -eq 2) {
            if ($bits[1] -ne $hostname) {
                $newLines += $line
            }
        } else {
            $newLines += $line
        }
    }

    # Write file
    Clear-Content $filename
    foreach ($line in $newLines) {
        $line | Out-File -encoding ASCII -append $filename
    }
}

function print-hosts([string]$filename) {
    $c = Get-Content $filename

    foreach ($line in $c) {
        $bits = [regex]::Split($line, "\t+")
        if ($bits.count -eq 2) {
            Write-Host $bits[0] `t`t $bits[1]
        }
    }
}

try {
    if ($args[0] -eq "add") {

        if ($args.count -lt 3) {
            throw "Not enough arguments for add."
        } else {
            add-host $file $args[1] $args[2]
        }

    } elseif ($args[0] -eq "remove") {

        if ($args.count -lt 2) {
            throw "Not enough arguments for remove."
        } else {
            remove-host $file $args[1]
        }

    } elseif ($args[0] -eq "show") {
        print-hosts $file
    } else {
        throw "Invalid operation '" + $args[0] + "' - must be one of 'add', 'remove', 'show'."
    }
} catch  {
    Write-Host $error[0]
    Write-Host "`nUsage: hosts add <ip> <hostname>`n       hosts remove <hostname>`n       hosts show"
}
Run Code Online (Sandbox Code Playgroud)

  • 遇到暂时无法访问文件(锁定)的问题。请注意,如果我运行它两次,它通常会被释放。在重试中包装保存输出文件可能会很好。 (2认同)

Jas*_*son 8

从Kevin Remisoski上面的优秀答案开始,我想出了这个让我一次添加/更新多个条目.我还改变了分割中的正则表达式以寻找任何空白区域,而不仅仅是制表符.

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)
Run Code Online (Sandbox Code Playgroud)


小智 5

我写了一个代码来从主机中删除条目。您可以轻松更改代码以从代码中添加条目。

$domainName = "www.abc.com"
$rplaceStr = ""
$rHost = "C:\Windows\System32\drivers\etc\hosts"
$items = Get-Content $rHost | Select-String $domainName
Write-host $items
foreach( $item in $items)
{
(Get-Content $rHost) -replace $item, $rplaceStr| Set-Content $rHost
}
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅 http://nisanthkv.blog.com/2012/06/13/remove-host-entries-using-powershell/


小智 5

所有这些答案都非常详尽。这就是添加主机文件条目所需的全部:

Add-Content -Path C:\Windows\System32\drivers\etc\hosts -Value "127.0.0.1`tlocalhost" -Force
Run Code Online (Sandbox Code Playgroud)

IP地址和主机名用`t分隔,t是制表符的Powershell表示法。

  • 最简单的一个。在极端情况下,您可能想使用 `$env:windir` 而不是硬编码的 `C:\Windows`... (6认同)
  • 注意:您需要在之前添加换行符,否则它只会附加到最后一个主机条目 (2认同)