PowerShell |需要一些性能改进
我已经写了一个PowerShell脚本来存档旧的日志文件或者说一些输出文件的Web应用程序在TB中,但脚本需要很长时间。我做了一些改进,但无法从这里加速。PowerShell |需要一些性能改进
代码:
#region Archive Files using 7zip
[cmdletbinding()]
Param
(
[Parameter(Mandatory=$true, HelpMessage = "Path needs to be with trailing slash at the end of location.")]
[string]$SourceFilesPath
)
$7zip = "C:\Program Files\7-Zip\7z.exe"
$FilePath = ""
foreach ($filename in $(Get-ChildItem $SourceFilesPath -Force -Recurse | where {$_.LastWriteTime -lt (get-date).AddDays(-1).ToShortDateString()}))
{
$FilePath = Get-ItemProperty $filename.FullName
$ZipFilePath = $filename.Directory.ToString() + "\ZippedFiles" + "\Archive_" + $filename.LastWriteTime.ToString("MMddyyyy") + ".7z"
$tempPath = ("-w"+"C:\Temp")
$OutputData = &$7zip a $tempPath -t7z $ZipFilePath $FilePath
$OutputData
if ($OutputData -contains "Everything is OK")
{
Remove-Item $FilePath -Force
Write-Output "File removed $FilePath"
}
Get-Item $ZipFilePath | ForEach-Object {$_.LastWriteTime = $filename.LastWriteTime}
}
#endregion
#region Archive Files using 7zip
[cmdletbinding()]
Param
(
[Parameter(Mandatory = $true, HelpMessage = 'Path needs to be with trailing slash at the end of location.')]
[string]$SourceFilesPath
)
Import-Module ..\Invoke-Parallel.ps1 # Download from PSGallery
$7zip = 'C:\Program Files\7-Zip\7z.exe'
$fileToArchive = $(Get-ChildItem $SourceFilesPath -Force -Recurse | Where-Object -FilterScript {
$_.LastWriteTime -lt (Get-Date).AddDays(-1).ToShortDateString()
})
$counter = 0
$groupSize = 2000 # Will group items by 2,000 increments
$groups = $fileToArchive | Group-Object -Property {
[math]::Floor($counter++/$groupSize)
}
$groups
# This will spawn multiple instances of 7zip - depending on how many groups of 2,000 files exist
$groups.Group | Invoke-Parallel -ScriptBlock {
$FilePath = $null
$fileName = $_
$FilePath = Get-ItemProperty -Path $fileName.FullName
$ZipFilePath = $fileName.Directory.ToString() + '\ZippedFiles' + '\Archive_' + $fileName.LastWriteTime.ToString('MMddyyyy') + '.7z'
$tempPath = ('-w'+'C:\Temp')
$OutputData = &$Using:7zip a $tempPath -t7z $ZipFilePath $FilePath
$OutputData
if ($OutputData -contains 'Everything is OK')
{
Remove-Item $FilePath -Force
Write-Output -InputObject "File removed $FilePath"
}
Get-Item $ZipFilePath | ForEach-Object -Process {
$_.LastWriteTime = $fileName.LastWriteTime
}
}
在脚本中进行了一些更正之后。它在我的本地机器上工作,但不在服务器上。当我查看调试时,发现在调用并行脚本中有PSVersion的检查条件,并且我的服务器2008 R2上的PSVersion在2.0上。顺便说一句,错误是“” – Imran
Get-RunspaceData:管道元素中'&'后面的表达式产生一个无效的对象。它必须产生逗号,名称,脚本块或CommandInfo对象。 在E:\ DevOps \ Scripts \ Invoke-Parallel.ps1:569 char:37 + Get-RunspaceData Imran
Invoke-Parallel需要v3 +。 –
7zip的仅使用2在默认模式LZMA芯。切换到使用LZMA2压缩来启用所有CPU核心,或通过命令行开关指定更快的压缩模式。 – wOxxOm
我可以做到这一点,但不认为它会给文件大小不大的改进。文件大多数是kb,最大文件大小是5MB,但文件数量很大。所以可能需要使用过滤器或其他任何可能更快的过滤方式。 – Imran
大量文件*小尺寸=大整体大小=长压缩时间。 – wOxxOm