小编Dav*_*ett的帖子

从 PowerShell 中编辑主机文件

我需要能够使用 PowerShell 脚本管理我的环境。我需要定期更改我的主机文件。但是,每当我尝试从脚本编辑主机时,都会出现以下错误:

Access to the path C:\windows\system32\drivers\etc\hosts is denied
Run Code Online (Sandbox Code Playgroud)

目前我的脚本如下所示:

Function ManageHosts([string] $environment)
{
cd C:\windows\system32\drivers\etc
if($environment.StartsWith("p"))
{
get-content .\hosts.prod | set-content .\hosts -force
}
else
{
get-content .\hosts.dev | set-content .\hosts -force
}
}
Run Code Online (Sandbox Code Playgroud)

windows powershell

4
推荐指数
1
解决办法
3248
查看次数

标签 统计

powershell ×1

windows ×1