[英]Running Powershell from vbs with command as parameter
嘿,我想從一個 vbs 腳本運行一個 powershell 突擊隊。 諸如啟動 powershell.exe 並輸入特定命令(例如 Restart-Service)。 我認為類似的東西可以工作:
strCommand = "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -command Restart-Service [service name]"
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objExec = WshShell.Exec(strCommand)
有人知道我該如何管理嗎?
1)將您的powershell命令存儲為powershell腳本。
2)使用vbs運行powershell
Set objShell = CreateObject("Wscript.Shell")
objShell.Run("powershell.exe -noexit c:\scripts\test.ps1")
嘗試這個:
powershell -command '& {command to run}'
/G
您可以使用最短的版本。
CreateObject("Wscript.Shell").Run ("powershell.exe -noexit $c= 'lola'; write-host $c -ForegroundColor green")
嘗試這個:
Set objShell = CreateObject("Wscript.Shell")
objShell.Run("powershell.exe -noexit .\c:\scripts\test.ps1")
或者將文件保存在 PS exe 所在的同一文件夾中,然后
Objshell.Run("powershell.exe -noexit .\test.ps1")
在研究如何使用它來使用 VBScript 運行 PowerShell 腳本時,我遇到了如下所示的問題:
從那以后我就知道為什么它不起作用了。 PowerShell 無法讀取腳本位置中的空格。 如上圖所示,當完整路徑為C:\Users\lpeder6\Desktop\PowerShell_Scripts\Arrays\Arrays - Clear Method.ps1
時,錯誤在C:\Users\lpeder6\Desktop\PowerShell_Scripts\Arrays\Arrays
處停止。 C:\Users\lpeder6\Desktop\PowerShell_Scripts\Arrays\Arrays - Clear Method.ps1
。
從我的路徑名中刪除所有空格和多余字符后,使用 VBScript 運行 PowerShell 腳本可以使用以下代碼:
Set objShell = CreateObject("Wscript.Shell")
objShell.Run("powershell.exe -noexit -command C:\Users\lpeder6\Desktop\PowerShellScripts\GUIFiles\BasicGUIScript-ComboBox.ps1"),1,True
當我運行它時,我得到的是:
我希望這對某人有所幫助,因為這是我真的可以使用的信息。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.