我有一个脚本可以压缩Source文件夹中的所有文件和文件夹,并将它们复制到Destination文件夹,然后删除它们。 我也添加了错误记录。 我是Powershell的新手,虽然脚本大部分时间都可以工作,但有时它不会将文件添加到存档中,当然之后会删除它,所以我没有记录。 错误记录不会捕获此
$Log = "c:\support\scripts\CommsLogArchiveDMLog.txt"
$DestZip="D:\SDM\Fltctrl\MsgBackup\"
$Source = "D:\SDM\Fltctrl\Msglog\ANZ\"
$folder = Get-Item -Path $Source
$ZipTimestamp = Get-Date -format yyyyMMdd-HHmmss;
$ZipFileName = $DestZip + $ZipTimestamp + "_Comms" + ".zip"
$Source
write-output "" >> $Log
write-output $ZipTimestamp >> $Log
Write-output "... Waiting for the zip file to be created" >> $Log
Try
{
set-content $ZipFileName ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
while (!(Test-Path -PathType leaf -Path $ZipFileName))
{
Start-Sleep -s 20
}
}
catch
{
Write-Output "Ran into an issue: $($PSItem.ToString())" >> $Log
}
Try
{
$ZipFile = (new-object -com shell.application).NameSpace($ZipFileName)
}
catch
{
Write-Output "Ran into an issue: $($PSItem.ToString())" >> $Log
}
Write-Output "... Zip file created" >> $Log
Start-Sleep -s 10
write-output "... Adding files to archive" >> $Log
Try
{
$ZipFile.CopyHere($Source)
}
catch
{
Write-Output "Ran into an issue: $($PSItem.ToString())" >> $Log
}
Try
{
$ZipFileName
}
catch
{
Write-Output "Ran into an issue: $($PSItem.ToString())" >> $Log
}
write-output "... Successfully added files to archive" >> $Log
Start-Sleep -s 10
write-output "... Deleting source files" >> $Log
Try
{
Get-ChildItem -Path $Source -Recurse -force |
Where-Object { -not ($_.psiscontainer) } |
Remove-Item –Force
}
catch
{
Write-Output "Ran into an issue: $($PSItem.ToString())" >> $Log
}
write-output "... Successfully deleted source files" >> $Log
如何使这项工作按预期工作的任何建议将非常感谢。 理想情况下,如果文件已成功添加到存档中,我希望仅删除这些文件。
答案 0 :(得分:0)
您应该尝试使用Compress-Archive
功能。我以前从未使用它,但它可以使你的代码更清洁。这是我想出来的,但它没有经过测试:
[CmdletBinding(SupportsShouldProcess = $true)]
param (
$Source = "D:\SDM\Fltctrl\Msglog\ANZ\",
$Destination = "D:\SDM\Fltctrl\MsgBackup\",
$Log = "c:\support\scripts\CommsLogArchiveDMLog.txt"
)
function Log ( [String] $Message ) {
Write-Verbose -Message $Message
if ( $Log )
{
"$(Get-Date -Format yyyyMMdd-HHmmss): $Message" | Add-Content $Log
}
}
# Generate .zip filename
$ArchiveFilePath = Join-Path -Path $Destination -ChildPath "$(Get-Date -Format yyyyMMdd-HHmmss)_Comms.zip"
Log -Message "Destination archive filepath: $ArchiveFilePath"
# Compress files
try
{
Compress-Archive -Path $Source -DestinationPath $ArchiveFilePath -CompressionLevel Optimal
}
catch
{
Log -Message "An error occurred creating archive: $($_.ToSTring()) Terminating script."
throw $_
}
# Remove source files
try
{
Get-ChildItem -Path $Source -Recurse -force `
| Where-Object -Property PSIsContainer -EQ -Value $false `
| ForEach-Object `
-Begin { Log -Message "Removing file(s) from $Source"; $Count = 1 } `
-Process {
Log -Message "Removing item: $($_.FullName)"
$_ | Remove-Item -Force
$Count ++
} `
-End { Log -Message "Removed $Count file(s) from $Source" }
}
catch
{
Log -Message "An error occurred removing files: $($_.ToSTring()) Terminating script"
throw $_
}