簡體   English   中英

使用命令作為參數從 vbs 運行 Powershell

[英]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")

也許我遺漏了一些東西,但我有一些看起來與其他人所說的非常相似的東西應該起作用:

Set objShell = CreateObject("Wscript.Shell")
objShell.Run "powershell.exe -noexit -command C:\Users\lpeder6\Desktop\PowerShell_Scripts\Arrays\Arrays - Clear Method.ps1",1,True

這是我運行 VBS 時得到的結果:

在此處輸入圖片說明

任何想法我做錯了什么?

在研究如何使用它來使用 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM