我正試圖學習如何在提升模式下運行vbscript。我不能完全得到這個工作,如果有vbscript的參數,我想運行提升。在vbscript中將可變參數傳遞給ShellExecute
下面是一個簡單的例子:
OSVersion.vbs
' Return a string indicating the operating system
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
'Wscript.echo Wscript.Arguments(0)
For Each objOperatingSystem in colOperatingSystems
Wscript.StdOut.Write objOperatingSystem.Caption
Wscript.StdOut.WriteBlankLines(1)
Wscript.StdOut.Write "Version " & objOperatingSystem.Version
Next
RunElevated.vbs
' Run the script in elevated mode
'
' This will be needed to install programs into Program Files
prgName = Wscript.Arguments.Item(0)
prgArgs = ""
If Wscript.Arguments.Count > 1 Then
For i = 1 To Wscript.Arguments.Count - 1
prgArgs = prgArgs & " " & Wscript.Arguments.Item(i)
Next
End If
Wscript.echo "Running: " & prgName & prgArgs
Set objShell = CreateObject("Shell.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
strPath = fso.GetParentFolderName (WScript.ScriptFullName)
If fso.FileExists(strPath & "\" & prgName) Then
objShell.ShellExecute "wscript.exe", _
Chr(34) & strPath & "\" & prgName & Chr(34), _
"", "runas", 1
Else
Wscript.echo "Script file not found"
End If
的OSVersion.vbs腳本運行的罰款:
CSCRIPT OSVERSION .vbs Microsoft(R)Windows腳本宿主版本5.8 版權所有(C)Microsoft Corporation。版權所有。
微軟Windows 7家庭高級版
版本6.1.7601
問題1當我嘗試運行此升高,沒有出現在stdout
C:\用戶\ ChemModeling \ Documents \ FreeThink \ Java \ install> cscript RunElevated.vbs OSVersion.vbs Microsoft(R)Windows腳本宿主版本5.8 版權所有(C)Microsoft Corporation。版權所有。
運行:OSVersion.vbs
問題2我無法弄清楚如何將參數傳遞給我傳遞給RunElevated腳本。我讀了你應該「‘我在這裏的參數’」使用像語法,所以我嘗試這樣做:如果我運行
' Run the script in elevated mode
'
' This will be needed to install programs into Program Files
prgName = Wscript.Arguments.Item(0)
prgArgs = ""
If Wscript.Arguments.Count > 1 Then
For i = 1 To Wscript.Arguments.Count - 1
prgArgs = prgArgs & " " & Wscript.Arguments.Item(i)
Next
End If
Wscript.echo "Running: " & prgName & prgArgs
Set objShell = CreateObject("Shell.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
strPath = fso.GetParentFolderName (WScript.ScriptFullName)
If fso.FileExists(strPath & "\" & prgName) Then
objShell.ShellExecute "wscript.exe", _
Chr(39) & Chr(34) & strPath & "\" & prgName & prgArgs & Chr(34) & Chr(39), _
"", "runas", 1
Else
Wscript.echo "Script file not found"
End If
:
CSCRIPT RunElevated.vbs OSVersion.vbs測試 微軟(R )Windows腳本宿主版本5.8 版權所有(C)Microsoft Corporation。版權所有。
運行:OSVersion.vbs測試
然後我得到一個錯誤「有一個文件擴展名不引擎」 .vbs的測試」
任何人都可以提出什麼,我需要改變
?我已經取得了一些進展,在解決這一problem-
我改變了最初的腳本:
OSVersion.vbs
' Return a string indicating the operating system
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
'Wscript.echo Wscript.Arguments(0)
For Each objOperatingSystem in colOperatingSystems
Wscript.Echo objOperatingSystem.Caption
Wscript.Echo "Version " & objOperatingSystem.Version
Next
所以我真的可以看到是否發生了什麼事情。
我改變了第二腳本:
' Run the script in elevated mode
'
' This will be needed to install programs into Program Files
prgName = Wscript.Arguments.Item(0)
prgArgs = ""
If Wscript.Arguments.Count > 1 Then
For i = 1 To Wscript.Arguments.Count - 1
prgArgs = prgArgs & " " & Wscript.Arguments.Item(i)
Next
End If
Wscript.echo "Running: " & prgName & prgArgs
Set objShell = CreateObject("Shell.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
strPath = fso.GetParentFolderName (WScript.ScriptFullName)
If fso.FileExists(strPath & "\" & prgName) Then
objShell.ShellExecute "wscript.exe", _
Chr(34) & strPath & "\" & prgName & Chr(34) & prgArgs, _
"", "runas", 1
Else
Wscript.echo "Script file not found"
End If
和使用:WScript的RunElevated.vbs OSVersion.vbs測試
現在我看到我附和信息彈出窗口時運行該腳本。
所以我回到問題#1,我開始一個新的shell以管理員模式運行,所以如果我切換到cscript並嘗試將信息寫入標準輸出或使用Wscript.StdOut.Write,它會出現在新殼中,我永遠不會看到它,或者至少這是我認爲問題所在。
有沒有解決此問題的標準方法?
感謝Nilpo,我的所有Google搜索都沒有提供傳遞命令和參數的正確語法。我最終的目標是運行Java的vbscript。 p = Runtime.getRuntime()。exec(getVersion); InputStream s = p.getInputStream();這要求我從原始shell的stdout中獲得輸出。你的文章看起來非常好。我需要嘗試一下,看看是否可以從原始shell中的ShellExecute中捕獲stdout。如果可以的話,那將解決我最後的問題。 – 2012-07-22 20:33:45
我看過你的文章。我是否正確,因爲我使用ShellExecute(因爲我需要在提升模式下運行)並且ShellExecute創建了一個新窗口,所以運行ShellExecute的腳本無法從ShellExecute執行的腳本中檢索stdout?我能做的最好的事情是創建一個文件並讓原始腳本讀取它的某種kludge? – 2012-07-22 21:01:24
我在上面的答案中添加了更多內容。 – Nilpo 2012-07-23 00:45:10