【发布时间】: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:
& c:\windows\system32\cscript.exe NameOfFile.vbs-
start-process invoke-expressionC:\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