vbs ShellExecute運(yùn)行外部程序時(shí)如何判斷外部程序執(zhí)行成功與否
需要vbs執(zhí)行一些命令,比如 ""uwfmgr filter enable"" (統(tǒng)一寫入篩選器), 但是這個(gè)uwfmgr.exe需要管理員權(quán)限才能運(yùn)行相關(guān)命令
目前我是這么寫的
Set sst = CreateObject("Shell.Application")
Call sst.ShellExecute("uwfmgr","filter enable", ,"runas",0)這樣可以以管理員身份運(yùn)行命令, 但是我怎么才能判斷uwfmgr執(zhí)行是成功還是失敗的? (管理員cmd運(yùn)行命令會(huì)有返回值)
原來我執(zhí)行一些cmd命令都是用
intReturn = WScript.CreateObject("wscript.shell").Run("xxxxxx",1,true)
通過判斷intReturn是否為0來判斷命令執(zhí)行成功失敗, 但是這個(gè)不能執(zhí)行需要管理員權(quán)限的命令, 有沒有辦法讓Run能以管理員運(yùn)行命令?
如何隱藏運(yùn)行Setup.bat文件
'以管理員身份運(yùn)行程序的命令admin.vbs
Set objWMIServices = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set objWbemObjectSet = objWMIServices.ExecQuery(_
"SELECT * FROM Win32_Process WHERE " &_
"ExecutablePath='" & Replace(WScript.FullName,"\","\\") & "' and " & _
"CommandLine LIKE '%" & WScript.ScriptName & "%'")
for each objWbemObject in objWbemObjectSet
cmdline = objWbemObject.CommandLine
next
if WScript.Arguments.Count then
file = WScript.Arguments(0)
if file="/?" then
call ShowHelp()
WScript.Quit
end if
Set RegEx = new RegExp
RegEx.IgnoreCase = true
RegEx.Global = true
RegEx.Pattern = "\\|\/|\||\(|\)|\[|\]|\{|\}|\^|\$|\.|\*|\?|\+"
temp1 = RegEx.Replace(WScript.ScriptName, "\$&")
temp2 = RegEx.Replace(file, "\$&")
RegEx.Global = false
RegEx.Pattern = "^.*?" & temp1 & "[""\s]*" & temp2 & """?\s*"
args = RegEx.Replace(cmdline, "")
'WScript.Echo file, args
else
file = "Setup.bat"
'args = "/k cd /d """ & CreateObject("WScript.Shell").CurrentDirectory & Chr(34)
end if
'核心代碼
Set sh = CreateObject("Shell.Application")
call sh.ShellExecute( file, args, , "runas" )
function ShowHelp()
dim HelpStr
HelpStr = "以管理員身份運(yùn)行程序。" & vbCrLf _
& vbCrLf _
& WScript.ScriptName & " [program] [parameters]..." & vbCrLf _
& vbCrLf _
& "program 要運(yùn)行的程序" & vbCrLf _
& "parameters 傳遞給 program 的參數(shù)" & vbCrLf _
& vbCrLf
WScript.Echo HelpStr
end function 到此這篇關(guān)于vbs ShellExecute運(yùn)行外部程序時(shí)如何判斷外部程序執(zhí)行成功與否的文章就介紹到這了,更多相關(guān)vbs ShellExecute運(yùn)行外部程序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
教你用vbs實(shí)現(xiàn)微信自動(dòng)發(fā)送消息功能
無意中接觸了vbs這個(gè)腳本感覺挺好玩的,這篇文章主要給大家介紹了關(guān)于如何用vbs實(shí)現(xiàn)微信自動(dòng)發(fā)送消息功能的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
利用VBS實(shí)現(xiàn)顯示系統(tǒng)服務(wù)列表
顯示服務(wù)列表的方法有很多,在接下來的文章中為大家介紹下利用VBS是如何實(shí)現(xiàn)的,感興趣的朋友不要錯(cuò)過2013-11-11
用vbs更改 Internet Explorer 的標(biāo)題欄
用vbs更改 Internet Explorer 的標(biāo)題欄...2007-03-03

