我需要能够使用 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)