2017-11-02 6 views
0

私は、ソースフォルダ内のすべてのファイルとフォルダを解凍し、それらをデスティネーションフォルダにコピーしてから削除するスクリプトを用意しています。 エラーロギングも追加しました。 私はPowershellの初心者ですが、スクリプトはほとんどの場合動作しますが、ファイルをアーカイブに追加しないで、後でそれを削除してレコードがなくなってしまいます。 エラーログははるかに高く評価されるだろうと予想通り、この作業を取得する方法Oこのアーカイブにファイルを追加する

$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

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 $_ 
} 
+0

乾杯ショーン、私は圧縮・アーカイブ・コマンドレットをインストールしていない、と私はそれが私たちのサーバー上で行われますことを疑う – Jacques

+0

それをテストします – Jacques

+0

バージョン4がインストールされているように見える – Jacques

関連する問題