当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何使用 PowerShell 添加 HOST 文件条目?

作者:精品下载站 日期:2024-12-14 20:59:33 浏览:15 分类:玩电脑

如何使用 PowerShell 添加 HOST 文件条目?


以下是使用 PowerShell 添加 HOST 文件条目的漂亮 PowerShell 脚本:


$HostFile = 'C:\Windows\System32\drivers\etc\hosts'

# Create a backup copy of the Hosts file
$dateFormat = (Get-Date).ToString('dd-MM-yyyy hh-mm-ss')
$FileCopy = $HostFile + '.' + $dateFormat  + '.copy'
Copy-Item $HostFile -Destination $FileCopy

#Hosts to Add
$Hosts = @("intranet.Crescent.com", "Intranet", "mysite.crescent.com")

# Get the contents of the Hosts file
$File = Get-Content $HostFile

# write the Entries to hosts file, if it doesn't exist.
foreach ($HostFileEntry in $Hosts) 
{
    Write-Host "Checking existing HOST file entries for $HostFileEntry..."
    
    #Set a Flag
    $EntryExists = $false
    
    if ($File -contains "127.0.0.1 `t $HostFileEntry") 
    {
        Write-Host "Host File Entry for $HostFileEntry is already exists."
        $EntryExists = $true
    }
    #Add Entry to Host File
    if (!$EntryExists) 
    {
        Write-host "Adding Host File Entry for $HostFileEntry"
        Add-content -path $HostFile -value "127.0.0.1 `t $HostFileEntry"
    }
}

SharePoint 主机文件 PowerShell

[玩转系统] 如何使用 PowerShell 添加 HOST 文件条目?

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯