WorkJ
WorkJ

Reputation: 190

Modify Windows hosts file from vbscript with admin privileges

Our network team uses a .VBS script file that runs every time a user logs into the network. They asked me to edit their script so that it will modify the windows hosts file.

The problem is the script needs admin privileges on the user's computer. From the script, how do I open the hosts file with elevated privileges, make some changes, and then save the file?

Upvotes: 1

Views: 1204

Answers (1)

Hackoo
Hackoo

Reputation: 18827

Something like that :

If Not WScript.Arguments.Named.Exists("elevate") Then
  CreateObject("Shell.Application").ShellExecute WScript.FullName _
    , WScript.ScriptFullName & " /elevate", "", "runas", 1
  WScript.Quit
End If
Hosts = "%windir%\system32\drivers\etc\hosts"
Command = "cmd /c attrib "& Hosts &" -r"
Set Ws = WScript.CreateObject("WScript.Shell")

Result = Ws.run(Command,0,True)
EditHostsFile = Ws.run("cmd /c Notepad "& Hosts,0,True)
HostsReadOnly = Ws.run("cmd /c attrib "& Hosts &" +r",0,True)

Upvotes: 2

Related Questions