0
votes

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?

1
You need to (re-)run the script itself with elevated privileges. - Ansgar Wiechers
Sounds dubious. Messing with hosts file is one way to gain access to systems. Why do you need admin privileges? Just give them the entries you need added to the hosts file and they'll push the changes to the PCs that need it. You're obviously trying to push changes to hosts files on machines on which you don't have admin rights and that's not cool. The network team has admin rights to all machines they supervise/support and they should do it. Just give them the entries you need. - MikeC

1 Answers

1
votes

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)