2012-07-21 174 views
1

我正試圖學習如何在提升模式下運行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,它會出現在新殼中,我永遠不會看到它,或者至少這是我認爲問題所在。

有沒有解決此問題的標準方法?

回答

3

問題1:

你沒有看到任何輸出,當你運行你的腳本升高,因爲RunElevated.vbs腳本中使用WScript.exe的重新啓動您的OSVersion.vbs腳本。 OSVersion.vbs使用標準輸入和輸出,只能從控制檯獲得。將RunElevated.vbs中的命令行更改爲使用cscript.exe而不是wscript.exe即可解決此問題。欲瞭解更多,請查看我的文章Error Trapping and Capturing Third-Party Output in WSH

問題#2

它看起來像你的問題在這裏,你混合不同種類的報價。一個命令應該是這樣的:

「C:用空格\路徑\ COMMAND.EXE」「參數用空格」「和另一個」

嘗試這些腳本。

RunElevated.vbs

' Run a script in Elevated Mode 
strName  = WScript.Arguments.Item(0) 
strArgs  = "" 
If WScript.Arguments.Count > 1 Then 
    For i = 1 To WScript.Arguments.Count - 1 
     strArgs = strArgs & " " & WScript.Arguments.Item(i) 
    Next 
End If 
WScript.echo "Running: ", strName, strArgs 
Set objShell = CreateObject("Shell.Application") 
Set objFso = CreateObject("Scripting.FileSystemObject") 
strPath  = objFso.GetParentFolderName(WScript.ScriptFullName) 
strParams = Chr(34) & strPath & "\" & strName & Chr(34) 
If strArgs <> "" Then 
    strParams = strParams & " " & Chr(34) & strArgs & Chr(34) 
End If 
If objFso.FileExists(strPath & "\" & strName) Then 
    objShell.ShellExecute "cscript.exe", strParams, "", "runas", 1 
Else 
    WScript.echo "Script file not found" 
End If 

OSVersion.vbs

'Test passing arguments to launched script 
If WScript.Arguments.Count > 0 Then 
    For i = 0 To WScript.Arguments.Count - 1 
     strArgs = Trim(strArgs & " " & WScript.Arguments.Item(i)) 
    Next 
End If 

' 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") 

For Each objOperatingSystem in colOperatingSystems 
    WScript.StdOut.Write objOperatingSystem.Caption 
    WScript.StdOut.WriteBlankLines(1) 
    WScript.StdOut.Write "Version " & objOperatingSystem.Version 
    WScript.StdOut.WriteBlankLines(2) 
Next 

' Print out any passed arguments 
WScript.StdOut.WriteBlankLines(2) 
WScript.StdOut.Write strArgs 
WScript.StdOut.WriteBlankLines(2) 

' Keep the window from closing before you get a chance to read it 
WScript.StdOut.Write "Press any key to continue " 
strInput = WScript.StdIn.Read(1) 

爲了捕獲輸出,我會使用的WshShell Execute方法可能會嘗試不同的方法。有可用的命令行工具,如ElevateHStart,可以啓動具有提升特權的命令行。以下是我將如何使用Elevate來做到這一點。

' Run a script in Elevated Mode 
strName  = WScript.Arguments.Item(0) 
strArgs  = "" 
If WScript.Arguments.Count > 1 Then 
    For i = 1 To WScript.Arguments.Count - 1 
     strArgs = strArgs & " " & WScript.Arguments.Item(i) 
    Next 
End If 
WScript.echo "Running: ", strName, strArgs 
Set WshShell = CreateObject("WScript.Shell") 
Set objFso = CreateObject("Scripting.FileSystemObject") 
strPath  = objFso.GetParentFolderName(WScript.ScriptFullName) 
strParams = Chr(34) & strPath & "\" & strName & Chr(34) 
If strArgs <> "" Then 
    strParams = strParams & " " & Chr(34) & strArgs & Chr(34) 
End If 
If objFso.FileExists(strPath & "\" & strName) Then 
    Set WshShellExec = WshShell.Exec("elevate cscript.exe " & strParams) 

    Select Case WshShellExec.Status 
     Case WshFinished 
      strOutput = WshShellExec.StdOut.ReadAll 
     Case WshFailed 
      strOutput = WshShellExec.StdErr.ReadAll 
    End Select 
Else 
    WScript.echo "Script file not found" 
End If 

WScript.echo strOutput 
+0

感謝Nilpo,我的所有Google搜索都沒有提供傳遞命令和參數的正確語法。我最終的目標是運行Java的vbscript。 p = Runtime.getRuntime()。exec(getVersion); InputStream s = p.getInputStream();這要求我從原始shell的stdout中獲得輸出。你的文章看起來非常好。我需要嘗試一下,看看是否可以從原始shell中的ShellExecute中捕獲stdout。如果可以的話,那將解決我最後的問題。 – 2012-07-22 20:33:45

+0

我看過你的文章。我是否正確,因爲我使用ShellExecute(因爲我需要在提升模式下運行)並且ShellExecute創建了一個新窗口,所以運行ShellExecute的腳本無法從ShellExecute執行的腳本中檢索stdout?我能做的最好的事情是創建一個文件並讓原始腳本讀取它的某種kludge? – 2012-07-22 21:01:24

+0

我在上面的答案中添加了更多內容。 – Nilpo 2012-07-23 00:45:10

1

我發現了兩件有趣的事情;首先,我必須得到適當數量的引用和空格,其次,我必須先實際調用臨時腳本,然後調用最終腳本提升。

INTIAL調用腳本片段:

If objFSO.FileExists("C:\Windows\System32\elevate.vbs") Then 
    objWSHShell.ShellExecute "wscript.exe", Chr(34) & Chr(34) & "C:\Windows\System32\elevate.vbs" & Chr(32) & strCampus & Chr(32) & strLocation & Chr(32) & strAssetTag & Chr(34) & Chr(34), "", "", 1 
Else 
    MsgBox "Regbrand script file not found",0,"Error Message" 
End If 

通知的CHR()語句和3個變量從原來的腳本來臨時腳本傳遞。

臨時腳本片段:

If Args.Count = 0 Then 
    MsgBox "Missing arguments to UAC permissions script.",0,"Missing Parameters" 
Else 
    strCampus = Args.Item(0) 
    strLocation = Args.Item(1) 
    strAssetTag = Args.Item(2) 

    strFilePath = objFSO.GetParentFolderName (WScript.ScriptFullName) 
    If objFSO.FileExists("C:\Windows\System32\regbrand.vbs") Then 
     objWSHShell.ShellExecute "wscript.exe", Chr(34) & Chr(34) & "C:\Windows\System32\regbrand.vbs" & Chr(32) & strCampus & Chr(32) & strLocation & Chr(32) & strAssetTag & Chr(34) & Chr(34), "", "runas", 1 
    Else 
     MsgBox "Script file not found",0,"Error Message" 
    End If 
End If 

在此期間腳本中,我們通過相同的參數最後腳本。再次請注意Chr()語句。

第一個腳本傳遞3個參數並調用臨時腳本,該腳本依次使用提升的權限調用第三個腳本「regbrand.vbs」。它的工作原理與它應該一樣。如果您刪除臨時腳本,它不起作用。我還沒有想到,但相信它與從網絡位置調用原始腳本有關。不過,我希望調用和傳遞參數的語法是有幫助的。

相關問題