2015-10-08 58 views
2

即時尋找一個簡單的代碼添加到這個退出程序由VBS腳本相同的腳本打開關閉程序

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~" 
+0

你在這個腳本中的目標是什麼?運行Powershell並關閉它? – Hackoo

+0

抱歉,因爲我的家人出現緊急情況。是的,那是我的目標。 – user3802783

回答

1

只發送exit命令的PowerShell:

wshshell.sendkeys"exit~" 
+1

很好的答案和ty。它不會讓我投票給你。 – user3802783

+0

而不是upvoting,你可以標記爲答案? :) 謝謝。 –

1

你可以使用WshShell.Exec("powershell.exe")而不是WshShell.Run("powershell.exe")

Set WshShell = WScript.CreateObject("WScript.Shell") 
Set app = WshShell.Exec("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 ' **** See note below ***** 
wscript.sleep 145 
wshshell.sendkeys"cls~" 
app.Terminate 

app.Terminate會關閉您的程序。

注: app.Terminate不會等待你的任務,以關閉應用程序前完成。使用上面的代碼,即使在完全加載之前,它也會關閉PowerShell。

一個快速的解決方法是增加睡眠時間。

wscript.sleep 300 

睡眠時間10000是PowerShell加載和ipconfig命令執行的安全猜測。

+1

ty你的答案和這方面的知識我試着upvoting你的答案,但它不會讓我。 – user3802783

+0

你似乎真的很擅長這個我可以做一個VBS腳本連接到MySQL數據庫? – user3802783

+1

user3802783。是的,你可以連接到MySQL數據庫。這是一個單獨的問題,應該這樣問,儘管你可以使用這個代碼。 http://stackoverflow.com/questions/1366516/connect-to-mysql-5-0-database-using-pure-vbscript –