Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 用于在目录中执行.ps1文件的Powershell批处理文件_Batch File_Powershell - Fatal编程技术网

Batch file 用于在目录中执行.ps1文件的Powershell批处理文件

Batch file 用于在目录中执行.ps1文件的Powershell批处理文件,batch-file,powershell,Batch File,Powershell,我有一个非常好的部署方法 我目前使用powershell执行.sql文件,并将结果写入日志文件。当前批处理文件、.ps1文件和.sql文件都在同一目录中 我希望有一个批处理文件执行Build 01.ps1,当Build 01.ps1完成时,它执行Build 02.ps1等 有时我有20个构建文件夹要部署,我试图避免双击20.bat文件 我是PowerShell的新手-我很抱歉,如果不清楚,请询问。谢谢。试试这个: Get-ChildItem -Recurse -include "*.ps1" |

我有一个非常好的部署方法

我目前使用powershell执行
.sql
文件,并将结果写入日志文件。当前批处理文件、
.ps1
文件和
.sql
文件都在同一目录中

我希望有一个批处理文件执行Build 01
.ps1
,当Build 01
.ps1
完成时,它执行Build 02
.ps1

有时我有20个构建文件夹要部署,我试图避免双击20
.bat
文件

我是PowerShell的新手-我很抱歉,如果不清楚,请询问。谢谢。

试试这个:

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
}

另请注意,当前目录是运行命令的目录。这不是脚本目录本身(如果您从脚本运行命令)

或者,如果您严格要求批处理文件而不是脚本,您可以执行以下操作:

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

cd/d%~dp0 powershell.exe-文件sp_script.ps1 pause每个批处理文件都包含以下内容:cd/d%~dp0 powershell.exe-文件sp_script.ps1 pause我刚刚将Get ChildItem-Recurse-在.bat文件中包含“*.ps1”|%{&$}?很抱歉,我写的东西使批处理文件变得无用。此powershell脚本将调用所有ps1文件。如果要指定目录,请使用Get-ChildItem$path-Recurse-include“*.ps1”