Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.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
powershell脚本-创建zip文件,不包括一些文件和文件夹_Powershell_7zip - Fatal编程技术网

powershell脚本-创建zip文件,不包括一些文件和文件夹

powershell脚本-创建zip文件,不包括一些文件和文件夹,powershell,7zip,Powershell,7zip,我正在尝试创建powershell脚本函数来压缩一个文件夹,不包括一些文件夹和文件这里是我的代码,它创建了包含所有文件和文件夹的压缩文件 # Zip creator method function create-zip([String] $aDirectory, [String] $aZipfile){ [string]$pathToZipExe = "C:\Program Files\7-zip\7z.exe"; [Array]$arguments = "a", "-tzip

我正在尝试创建powershell脚本函数来压缩一个文件夹,不包括一些文件夹和文件这里是我的代码,它创建了包含所有文件和文件夹的压缩文件

# Zip creator method

function create-zip([String] $aDirectory, [String] $aZipfile){
    [string]$pathToZipExe = "C:\Program Files\7-zip\7z.exe";
    [Array]$arguments = "a", "-tzip", "$aZipfile", "$aDirectory";
    & $pathToZipExe $arguments;
}
但我想排除*.tmp文件以及bin和obj文件夹

我发现
Get ChildItem“C:\path\to\folder to zip”-Recurse-Exclude*.txt,*.pdf |%{7za.exe a-mx3”C:\path\to\newZip.zip“$\ FullName}
与powershell命令一样工作正常,但如何在脚本文件的函数中使用它呢


请建议…

如果一个班轮有效,那么将其放入函数中非常简单:

function Create-Zip([string]$directory, [string]$zipFile, [string[]]$exclude=@())
{
    $pathToZipExe = "C:\Program Files\7-zip\7z.exe"
    Get-ChildItem $directory -Recurse -Exclude $exclude | 
        Foreach {& $pathToZipExe a -mx3 $zipFile $_.FullName}
}
如果需要排除目录,请将Get ChildItem行更改为:

    Get-ChildItem $directory -Recurse -Exclude $exclude | Where {!$_.PSIsContainer} |

快速入门-您将这些函数实际放置在何处,以便powershell可以访问它们您可以将函数放在上面的profile.ps1脚本中(位于$home\Documents\WIndowsPowerShell下)。PowerShell将在加载该脚本时处理该脚本,并且该函数将可用。您可以在以下位置找到可能的解决方案: