【问题标题】:VBScript not running from windows serviceVBScript 未从 Windows 服务运行
【发布时间】:2020-03-04 00:45:12
【问题描述】:

我有一个调用 bat 文件的 Windows 服务。该 bat 文件调用 PowerShell 脚本,在该 PowerShell 脚本中调用 VBScript。

Windows Service > bat file > powershell file > vbscript

当我手动运行bat文件时,VBscript成功执行,但如果我从Windows服务执行相同的bat文件,则调用所有脚本,但VBScript跳过运行。

手动执行 bat 文件会成功执行 VBScript,但不是通过 Windows 服务

我尝试以不同的方式在 PowerShell 中调用 VBScript:

  1. & c:\windows\system32\cscript.exe NameOfFile.vbs
  2. start-process
  3. invoke-expression
  4. C:\Windows\System32\cscript.exe NameOfFiles.vbs //B //Nologo $IP_SU $RemoteSessions_Output $user

我的 VBScript 是:

dim ip
dim sessions_dir
dim temp
dim username
dim password

set temp = Wscript.Arguments
ip = temp(0)
sessions_dir = temp(1)
username = temp(2)
password = temp(3)

Sub WaitEnter()
    WshShell.AppActivate("telnet " & ip )
    WScript.Sleep 2000
    WshShell.AppActivate("telnet " & ip)
    WshShell.SendKeys "{Enter}"
    WshShell.AppActivate("telnet " & ip)
    WScript.Sleep 2000
End Sub

set WshShell = WScript.CreateObject("WScript.Shell")
Wscript.Sleep 1000
WshShell.AppActivate("telnet " & ip )
WshShell.Run "telnet " & ip & " -f " & sessions_dir & "\" & ip & "_SU_Status_Output.txt",2
WshShell.AppActivate("telnet " & ip)
WScript.Sleep 1000
WshShell.AppActivate("telnet " & ip)
WshShell.SendKeys username
WaitEnter

WshShell.AppActivate("telnet " & ip)
WshShell.SendKeys password
WaitEnter

WshShell.AppActivate("telnet " & ip)
WshShell.SendKeys "SU_INrOmk=` pl | awk '{{}print {$}3{}}' | head -3 | cut -d '=' -f2`; SU_type=` pl | grep $SU_INrOmk | tail -1 | awk '{{}print {$}12{}}'`"
WaitEnter

WshShell.AppActivate("telnet " & ip)
WshShell.SendKeys "echo $SU_type"
WaitEnter

WshShell.AppActivate("telnet " & ip)
WshShell.SendKeys "exit"
WshShell.AppActivate("telnet " & ip)
WshShell.SendKeys "{Enter}"

调用它的 PowerShell 脚本如下:

if(Test-Path C:\Windows\System32\cscript.exe){
    echo "Cscript found"
    $command = "& C:\Windows\System32\cscript.exe NameOfFile.vbs $IP_SU $RemoteSessions_Output $user $DecPwd | out-null"
    Invoke-Expression -Command $Command
    start-Sleep 10
    if($?){
        start-sleep 10
        $SU_Output_File = $IP_SU + "_SU_Status_Output.txt"
        $SU_Remote_FilePath = $RemoteSessions_Output + "\" + $SU_Output_File
    }
}

我希望在 Windows 服务调用 bat 文件时调用 VBScript。

【问题讨论】:

  • Windows Service -> Batch File -> PowerShell -> VBScript。只有一个问题,为什么??
  • 是否需要指定 vbs 文件的完整路径?或者至少设置一个工作目录?指定完整路径,或者可能类似于: Start-Process -FilePath "c:\windows\system32\cscript.exe" -ArgumentList "NameOfFile.vbs" -WorkingDirectory "c:\path\to\vbs"...

标签: windows powershell vbscript


【解决方案1】:

我在这里看到了一些可能会给您带来问题的事情。

我不是 VBS 的大师,但我的猜测是您使用的是 Wscript,我认为这需要交互性,但您应该使用 Cscript 变体调用。我的猜测是您的脚本可能因此而被轰炸。自 Vista/Windows Server 2008 起,服务无法在交互式上下文中运行。

您使用Invoke-Expression 调用它,(几乎)总是返回成功,即使 cmdlet 失败。换句话说,Invoke-Expression 将(几乎)总是将$? 设置为 $True,即使命令失败。但是,您可以在表达式末尾添加对 ; $? 的评估,这将最终设置 $?,这打破了“始终将 $? 设置为 $True”的概念。

但是,您也错误地使用了$?$? 只评估cmdlets 的成功,而不是命令。 cscript.exe 是一个可执行文件,并且必须使用 $LASTEXITCODE 自动变量来评估其成功。这通常是 0 表示成功,任何其他值表示不成功。您必须自己检查此$LASTEXITCODE 是否成功,因为即使将ErrorActionPreference 设置为Stop,它也不会自动将非零退出代码视为终止错误。

超出了此答案的范围,但值得一提,I would recommend replacing Invoke-Expression with a straight call to the executable and splat your parameters instead

【讨论】:

  • 感谢 Bender 的回复,但我没有收到第一段。我只使用 cscript 而不是 wscript。我是 vbscript 和 telnet 的新手,请告诉我哪个代码行显示我使用的是 wscript 而不是 cscript。
  • 你的 vbs 的第 7 行是我看到的第一次出现
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-26
  • 2014-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多