简体   繁体   English

合并Powershell脚本

[英]Merging powershell scripts

I am configuring Jenkins, and when the check out is performed, I need 2 folders to be deleted. 我正在配置Jenkins,执行检出操作时,需要删除2个文件夹。 I have created the script, which deletes all the bin folder. 我创建了脚本,该脚本删除了所有bin文件夹。 However, I want to delete all obj folder too. 但是,我也想删除所有obj文件夹。

Get-ChildItem -path C:\testFolderCheck 'obj' -Recurse -force |
Remove-Item -force -Recurse 
Get-ChildItem -path C:\testFolderCheck 'bin' -Recurse -force | 
Remove-Item -force -Recurse

When I am executing this scripts, only the bin folder is getting deleted. 当我执行此脚本时,仅bin文件夹将被删除。 Is there any way of executing both scripts in one run? 有没有一种方法可以一次执行两个脚本?

You can pass an array of paths to the Get-ChildItem cmdlet : 您可以将路径数组传递给Get-ChildItem cmdlet:

Get-ChildItem [[-Path] <String[]> ] [[-Filter] <String> ] [-Exclude <String[]> ] [-Force] [-Include <String[]> ] [-Name] [-Recurse] [-UseTransaction] [ <CommonParameters>]

So try this: 所以试试这个:

Get-ChildItem -path 'C:\testFolderCheck\obj', 'C:\testFolderCheck\bin' -Recurse -force |
    Remove-Item -force -Recurse
function clean
{
    Get-ChildItem -path C:\testFolderCheck 'obj' -Recurse -force | Remove-Item -force -Recurse 
    Get-ChildItem -path C:\testFolderCheck 'bin' -Recurse -force | Remove-Item -force -Recurse
}

clean 

One possibility to excute the script in one run is to define a function. 一次执行脚本的一种可能性是定义一个函数。 Another possibility is to use the invoke-command with computername and your script as scriptblock (maybe the better way, if you do remoting). 另一种可能性是使用带有计算机名调用命令,并将脚本作为脚本块 (如果是远程处理,则可能是更好的方法)。

Don´t be to hard -it´s my first post on stackoverflow :-) 不要辛苦-这是我关于stackoverflow的第一篇文章:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM