2014-02-12 274 views
1

我有一個非常好的部署方法。Powershell批處理文件在目錄中執行.ps1文件

我目前使用powershell執行.sql文件,並將結果寫入日誌文件。目前批處理文件.ps1文件和.sql文件都在同一個目錄中。

我想有執行建設01 .ps1和一個批處理文件時生成01 .ps1完成它執行建設02 .ps1

有時候,我有20個建立文件夾來部署和我試圖避免到雙擊20 .bat文件。

我是新來的PowerShell - 我很抱歉,如果這不明確請問。謝謝。

回答

1

試試這個:

Get-ChildItem -Recurse -include "*.ps1" | % { & $_ } 

要了解它是如何工作的,你可以分解這一行到:

$allScripts = Get-ChildItem -Recurse -include "*.ps1" #Get all ps1 file under the current directory 
foreach($script in $allScripts){ 
    & $script # run the script 
} 

一個側面說明,當前目錄則是運行命令的目錄。這是不是腳本目錄本身(如果你從腳本運行命令)

+0

每個批處理文件包含此: CD/d%〜DP0 powershell.exe -File sp_script.ps1 pause 我只是把Get-ChildItem -Recurse -include「* .ps1」| .bat文件中的%{&$ _} ?我真的很抱歉。 – user3302439

+0

我寫的使批處理文件無用。這個powershell腳本將調用所有ps1文件。 –

+0

如果要指定一個目錄,請使用Get-ChildItem $ path -Recurse -include「* .ps1」 – David

0

或者,如果你嚴格想要一個批處理文件,而不是一個腳本,你可以這樣做:

Start "" /wait "powershell.exe . .\build01.ps1" 
Start "" /wait "powershell.exe . .\build02.ps1" 
Start "" /wait "powershell.exe . .\build03.ps1" 
Start "" /wait "powershell.exe . .\build04.ps1" 

爲了澄清,我指的是將批處理文件作爲以.BAT.CMD擴展名結尾的文件。上面應該按順序運行.PS1腳本,在啓動下一個腳本之前等待每個腳本完成。如果您希望呼籲自己的每一個批處理文件從一個額外的批處理文件,你可以使用CALL命令,如:

CALL Batch01.bat 
CALL Batch02.bat 
CALL Batch03.bat 
+0

cd/d%〜dp0 powershell.exe -File sp_script.ps1 pause – user3302439