使用管理员权限从 vbscript 修改 Windows 主机文件 [英] Modify Windows hosts file from vbscript with admin privileges

查看:24
本文介绍了使用管理员权限从 vbscript 修改 Windows 主机文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的网络团队使用 .VBS 脚本文件,每次用户登录网络时都会运行该脚本文件.他们让我编辑他们的脚本,以便修改 Windows 主机文件.

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?

推荐答案

类似的事情:

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)

这篇关于使用管理员权限从 vbscript 修改 Windows 主机文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆