人生学徒 發表於 2023-6-11 16:27:22

vbs ShellExecute运行外部程序时如何判断外部程序执行成功与否

<p>需要vbs执行一些命令,比如 &quot;&quot;uwfmgr filter enable&quot;&quot; (统一写入筛选器), 但是这个uwfmgr.exe需要管理员权限才能运行相关命令<br />目前我是这么写的</p>
<div class="jb51code"><pre class="brush:vb;">Set sst = CreateObject("Shell.Application")
Call sst.ShellExecute("uwfmgr","filter enable", ,"runas",0)</pre></div>
<p>这样可以以管理员身份运行命令, 但是我怎么才能判断uwfmgr执行是成功还是失败的? (管理员cmd运行命令会有返回值)</p>
<p>原来我执行一些cmd命令都是用</p>
<p>intReturn = WScript.CreateObject(&quot;wscript.shell&quot;).Run(&quot;xxxxxx&quot;,1,true)</p>
<p>通过判断intReturn是否为0来判断命令执行成功失败, 但是这个不能执行需要管理员权限的命令, 有没有办法让Run能以管理员运行命令?</p>
<h3>如何隐藏运行Setup.bat文件</h3>
<div class="jb51code"><pre class="brush:vb;">'以管理员身份运行程序的命令admin.vbs
Set objWMIServices = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set objWbemObjectSet = objWMIServices.ExecQuery(_
"SELECT * FROM Win32_Process WHERE " &amp;_
"ExecutablePath='" &amp; Replace(WScript.FullName,"\","\\") &amp; "' and " &amp; _
"CommandLine LIKE '%" &amp; WScript.ScriptName &amp; "%'")
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, "\$&amp;")
temp2 = RegEx.Replace(file, "\$&amp;")
RegEx.Global = false
RegEx.Pattern = "^.*?" &amp; temp1 &amp; "[""\s]*" &amp; temp2 &amp; """?\s*"
args = RegEx.Replace(cmdline, "")
'WScript.Echo file, args
else
file = "Setup.bat"
'args = "/k cd /d """ &amp; CreateObject("WScript.Shell").CurrentDirectory &amp; Chr(34)
end if
'核心代码
Set sh = CreateObject("Shell.Application")
call sh.ShellExecute( file, args, , "runas" )
function ShowHelp()
dim HelpStr
HelpStr = "以管理员身份运行程序。" &amp; vbCrLf _
&amp; vbCrLf _
&amp; WScript.ScriptName &amp; " ..." &amp; vbCrLf _
&amp; vbCrLf _
&amp; "program 要运行的程序" &amp; vbCrLf _
&amp; "parameters 传递给 program 的参数" &amp; vbCrLf _
&amp; vbCrLf
WScript.Echo HelpStr
end function </pre></div>
頁: [1]
查看完整版本: vbs ShellExecute运行外部程序时如何判断外部程序执行成功与否