VBScript未从windows服务运行
我有一个调用bat文件的Windows服务。该bat文件调用PowerShell脚本,在该PowerShell脚本中调用VBScriptVBScript未从windows服务运行,windows,powershell,vbscript,Windows,Powershell,Vbscript,我有一个调用bat文件的Windows服务。该bat文件调用PowerShell脚本,在该PowerShell脚本中调用VBScript Windows Service > bat file > powershell file > vbscript 手动运行bat文件时,会成功执行VBscript,但如果从Windows服务执行相同的bat文件,则会调用所有脚本,但VBscript会跳过运行 手动执行bat文件可成功执行VBScript,但不能通过Windows服务执行 我尝
Windows Service > bat file > powershell file > vbscript
手动运行bat文件时,会成功执行VBscript,但如果从Windows服务执行相同的bat文件,则会调用所有脚本,但VBscript会跳过运行
手动执行bat文件可成功执行VBScript,但不能通过Windows服务执行
我尝试以不同的方式调用PowerShell中的VBScript:
&c:\windows\system32\cscript.exe NameOfFile.vbs
启动流程
调用表达式
C:\Windows\System32\cscript.exe NameOfFiles.vbs//B//Nologo$IP\u SU$RemoteSessions\u Output$user
dim ip
迪姆迪尔酒店
暗温
暗淡用户名
暗密码
set temp=Wscript.Arguments
ip=温度(0)
会话\u dir=temp(1)
用户名=临时工(2)
密码=临时(3)
子WaitEnter()
WshShell.AppActivate(“telnet”和ip)
WScript.Sleep 2000
WshShell.AppActivate(“telnet”和ip)
WshShell.SendKeys“{Enter}”
WshShell.AppActivate(“telnet”和ip)
WScript.Sleep 2000
端接头
设置WshShell=WScript.CreateObject(“WScript.Shell”)
Wscript.Sleep 1000
WshShell.AppActivate(“telnet”和ip)
WshShell.Run“telnet”&ip&“-f”&sessions\u dir&“\”&ip&“u SU\u Status\u Output.txt”,2
WshShell.AppActivate(“telnet”和ip)
WScript.Sleep 1000
WshShell.AppActivate(“telnet”和ip)
WshShell.SendKeys用户名
等待中心
WshShell.AppActivate(“telnet”和ip)
WshShell.SendKeys密码
等待中心
WshShell.AppActivate(“telnet”和ip)
WshShell.SendKeys“SU|INrOmk=`pl|awk'{{}print{3}}}head-3{124; cut-d'='-f2`;SU|u type=`pl|grep$SU|INrOmk{tail-1{awk'{}print{12}`”
等待中心
WshShell.AppActivate(“telnet”和ip)
WshShell.SendKeys“echo$SU_类型”
等待中心
WshShell.AppActivate(“telnet”和ip)
WshShell.SendKeys“退出”
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。我在这里看到一些可能会给您带来问题的因素 我不是VBS的高手,但我猜您使用的是
Wscript
,我认为这需要交互性,但您应该使用Cscript
变量调用。我猜你的剧本可能就是因为这个。自Vista/Windows Server 2008以来,服务无法在交互式上下文中运行
您使用调用表达式调用它,即使cmdlet失败,(几乎)总是返回成功。换句话说,Invoke Expression
将(几乎)始终将$?
设置为$True,即使命令失败。但是,您可以插入对;$的求值代码>在表达式的末尾,它将按照预期设置$?
,这打破了“始终将$?
设置为$True
”的概念
但是,您也不正确地使用了$?
<代码>$?
仅评估cmdlet的成功,而不是命令cscript.exe
是一个可执行文件,必须使用$LASTEXITCODE
自动变量对其成功进行评估。对于成功,该值通常为0
,对于不成功,该值通常为任何其他值。您必须亲自检查此$LASTEXITCODE
是否成功,因为即使ErrorActionPreference
设置为Stop
,它也不会自动将非零退出代码视为终止错误
超出此答案的范围,但值得一提,.请。Windows服务->批处理文件->PowerShell->VBScript
。只有一个问题,为什么需要指定vbs文件的完整路径?或者至少设置一个工作目录?或者指定完整路径,或者类似于:启动进程-文件路径“c:\windows\system32\cscript.exe”-参数列表“NameOfFile.vbs”-工作目录“c:\path\to\vbs”…感谢Bender的回复,但我没有得到第一段。我只使用cscript而不是wscript。我是vbscript和telnet的新手,请告诉我哪一行代码显示我使用的是wscript而不是cscript。vbs的第7行是我第一次看到的