Vbscript 用相同的脚本关闭由vbs脚本打开的程序

Vbscript 用相同的脚本关闭由vbs脚本打开的程序,vbscript,Vbscript,我正在寻找一个简单的代码添加到这个退出程序 Set WshShell = WScript.CreateObject("WScript.Shell") WshShell.Run("powershell.exe") wscript.sleep 2 wshshell.AppActivate "powershell" wscript.sleep 145 wshshell.sendkeys"ipconfig /all > C:\Users\Public\file.txt" wshshell.se

我正在寻找一个简单的代码添加到这个退出程序

Set WshShell = WScript.CreateObject("WScript.Shell") 
WshShell.Run("powershell.exe") 
wscript.sleep 2
wshshell.AppActivate "powershell"
wscript.sleep 145
wshshell.sendkeys"ipconfig /all > C:\Users\Public\file.txt"
wshshell.sendkeys"~"
wscript.sleep 300
wscript.sleep 145
wshshell.sendkeys"cls~"

只需将
exit
命令发送到powershell:

wshshell.sendkeys"exit~"

您可以使用
WshShell.Exec(“powershell.exe”)
而不是
WshShell.Run(“powershell.exe”)

app.Terminate
将关闭您的程序

注意:
app.Terminate
在关闭应用程序之前不会等待任务完成。使用上面的代码,它将在PowerShell完全加载之前关闭PowerShell

一个快速的解决方法是增加睡眠时间

wscript.sleep 300

对于powershell加载和ipconfig命令执行来说,10000的睡眠时间是一个安全的猜测。

您在这个脚本中的目标是什么?运行Powershell并关闭它?很抱歉我耽搁了我有家庭紧急情况。是的,这是我的目标。根据我对你的答案的了解,我试着投票给你的答案,但我不同意。你看起来真的很擅长这个。我可以让vbs脚本连接到MySQL数据库吗?用户3802783。是的,您可以连接到MySQL数据库。这是一个单独的问题,虽然您可以使用此代码,但也应该这样问。我想把雅尔的两个答案都标记为正确的lol。它们都完美无瑕。你能把它标记为答案而不是向上投票吗?:)谢谢
wscript.sleep 300