Set wshShell = WScript.CreateObject ("WSCript.shell")
wshshell.run "runas ..."
結果を取得してMsgBoxに表示するにはどうすればよいですか
Set wshShell = WScript.CreateObject ("WSCript.shell")
wshshell.run "runas ..."
結果を取得してMsgBoxに表示するにはどうすればよいですか
Runの代わりにWshShellオブジェクトのExecメソッドを使用することをお勧めします。次に、標準ストリームからコマンドラインの出力を読み取るだけです。これを試してください:
Const WshFinished = 1
Const WshFailed = 2
strCommand = "ping.exe 127.0.0.1"
Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)
Select Case WshShellExec.Status
Case WshFinished
strOutput = WshShellExec.StdOut.ReadAll
Case WshFailed
strOutput = WshShellExec.StdErr.ReadAll
End Select
WScript.StdOut.Write strOutput 'write results to the command line
WScript.Echo strOutput 'write results to default output
MsgBox strOutput 'write results in a message box
WshShell.Exec
これは、非同期であるという問題を修正するNilpoの回答の修正バージョンです。シェルのステータスが実行されなくなるまでビジーループを実行してから、出力を確認します。コマンドライン引数-n 1
をより高い値に変更して時間がping
かかるようにし、スクリプトが完了するまでの待機時間が長くなることを確認します。
(誰かが問題に対する真の非同期のイベントベースの解決策を持っているなら、私に知らせてください!)
Option Explicit
Const WshRunning = 0
Const WshFinished = 1
Const WshFailed = 2
Dim shell : Set shell = CreateObject("WScript.Shell")
Dim exec : Set exec = shell.Exec("ping.exe 127.0.0.1 -n 1 -w 500")
While exec.Status = WshRunning
WScript.Sleep 50
Wend
Dim output
If exec.Status = WshFailed Then
output = exec.StdErr.ReadAll
Else
output = exec.StdOut.ReadAll
End If
WScript.Echo output
exec.Statusはエラーレベルを返さないため、BoffinBrainのソリューションはまだ機能しません(実行中は0を返し、終了時には1を返します)。そのためには、exec.ExitCodeを使用する必要があります(Exec()メソッドを使用して実行されるスクリプトまたはプログラムによって設定された終了コードを返します)。したがって、ソリューションは次のように変更されます
Option Explicit
Const WshRunning = 0
' Const WshPassed = 0 ' this line is useless now
Const WshFailed = 1
Dim shell : Set shell = CreateObject("WScript.Shell")
Dim exec : Set exec = shell.Exec("ping.exe 127.0.0.1 -n 1 -w 500")
While exec.Status = WshRunning
WScript.Sleep 50
Wend
Dim output
If exec.ExitCode = WshFailed Then
output = exec.StdErr.ReadAll
Else
output = exec.StdOut.ReadAll
End If
WScript.Echo output
var errorlevel = new ActiveXObject('WScript.Shell').Run(command, 0, true)
3番目のパラメーターはtrueである必要があり、エラーレベルは戻り値になります。0に等しいかどうかを確認してください。