Powershell to manipulate host file

后端 未结 9 1724
我在风中等你
我在风中等你 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 02:43

    99% of the time admin rights are needed to modify a host record. Try adding this code at the top of your Powershell script.

    If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
    
    {   
    $arguments = "& '" + $myinvocation.mycommand.definition + "'"
    Start-Process powershell -Verb runAs -ArgumentList $arguments
    Break
    }
    

提交回复
热议问题