2011-07-07 164 views
4

目前,以下是啓動VMware vSphere PowerCLI命令提示符的路徑。我希望使用批處理文件自動運行我的sample.ps1腳本。我如何將sample.ps1納入此路徑並創建批處理文件?使用批處理文件(.bat)運行PS1腳本

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -c ". \"C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\"" 
+0

什麼你問?您可以隨時通過運行powershell.exe從cmd腳本調用powershell腳本,如上所述。你還想做什麼? –

+0

-psc = -PSConsoleFile - >加載控制檯文件以自動導入他的SNAPIN。 -noe = -NoExit - >爲了保持powershell會話。然後他想要發佈的腳本。 – JPBlanc

+0

這不是一個重複的... http://stackoverflow.com/questions/6037146/how-to-execute-powershell-commands-from-a-batch-file – SteveC

回答

3

如果您在使用PowerShell 2.0時,您可以使用PowerShell.exe

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -file "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1" 

-file參數如果您在使用PowerShell 1.0,則可以使用-command參數這樣

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -command "& 'C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1'" 
+0

感謝您的幫助。 – dwyane

1
echo off 

Title,Report Script &color 9e 
for /f "usebackq delims=$" %%a in (`cd`) do (
    set SCRIPTDIR=%%a 
) 

(Set ScriptFile=%SCRIPTDIR%\Report.ps1) 

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -c ". \"C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\";%ScriptFile%" 
0

您可以使用它通過調用bat來啓動任意.ps1腳本通過.bat文件像你的ps1文件。然後批量提取文件的名稱,並用它調用powershell。

對於準備使用的解決方案,請使用以下要點:https://gist.github.com/JonasGroeger/10417237

0

我在另一個頁面看到這個代碼,我測試它在W2012 R2,它運行。

我希望它的工作:

C:\>powershell "C:\>1\file.ps1" 
相關問題