none
ZIP-Datei per Powershell produziert Fehler RRS feed

  • Frage

  • Moin moin,

    ich habe ein Backup-Skript geschrieben, was nun einen kleinen Schönheitsfehler hat... Und zwar werden die ZIP-Dateien anscheinend nicht ordentlich geschlossen...

    Fehlermeldung nach dem ZIP-Vorgang:
    Title: Compressed (zipped) Folders Error
    Body: Windows cannot open the folder.
    Access to the Compressed (zipped) Folder 'C:\abc\Backup.zip' is denied.

    Kann mir jemand dabei weiterhelfen?

    Skript:

    Param(
        [Parameter(Mandatory=$True)][ValidateScript({Test-Path $_})][String]$SourceDirectory,
        [Parameter(Mandatory=$True)][ValidateScript({Test-Path $_})][String]$DestinationDirectory,
        [Parameter(Mandatory=$True)][ValidateScript({0 -lt $_})][String]$MaxWeekBackup,
        [Array]$requiredProcess,
        [string]$Mail_Receiver
        
    )
    Clear-Host
    
    $Mail_Channel = "aa@bb.de"
    $Mail_smtpServer = "mailout.de"
    $Mail_subject = "Backup "
    $PC_Name = $(Get-WmiObject Win32_Computersystem).name
    $successful = $true
    $ZIP_create_Wait = 500
    $ZIP_finish_Wait = 30000
    
    $datum = Get-Date
    $DayOfWeek = $datum.DayOfWeek
    $PSConsoleLog = $DestinationDirectory + "\PSConsole_" + $DayOfWeek + ".log"
    
    #Quelle: http://www.admin-source.de/BlogDeu/450/powershell-zip-ohne-module-oder-externe-programme
    function Add-Zip() {
    
      param (
        [array]$PackFilesPath,
        [string]$DestinationZipPath
      )
    
      ### Create the ZIP File if it is not existing
      Write-Host ""
      Write-Host -backgroundcolor blue "" "// Start the ZIP-Process\\"
    
      if(-not (Test-Path ($DestinationZipPath))) {
    
          ### Write ZIP File-header to File
          Set-Content $DestinationZipPath (“PK”+ [char]5 + [char]6 + (“$([char]0)”* 18))
    
          Write-Host -NoNewline -ForegroundColor Yellow "Create ZIP-File: "
          Write-Host $DestinationZipPath
      }
     
      ### remove read only flag from File
      $zip=Get-Item $DestinationZipPath
      $zip.IsReadOnly = $false
    
      ### we need the Fullpath to the File
      $DestinationZipPath = $zip.FullName
    
      ### Create the Shell COM Object
      $ShellApplication = New-Object -ComObject Shell.Application
    
      ### Create the COM Folder Object from ZIP File
      $ZipFolder=$ShellApplication.NameSpace($DestinationZipPath)
    
      ### Process Files given with the as Parameter Argument
      ### Pack file(s) into ZIP Package
      
      If($PackFilesPath) {
    
            Foreach($File in $PackFilesPath) {
                ### Pack file into Package
                $ZipFolder.CopyHere($File)
    
                Write-Host -NoNewline -ForegroundColor Yellow "Copy "
                Write-Host $File
    
                ### Try to wait to finish the copy action
                Write-Host -ForegroundColor Yellow ("Wait: " + $ZIP_create_Wait)
                Start-sleep -milliseconds $ZIP_create_Wait
          }
      }
    
      ### Process Files comming from Pipeline $input.
      ### Pack file(s) into ZIP Package
      If($input) {
        Foreach($file in $input) {
    
          ### Pack file into Package
          $ZipFolder.CopyHere($file.FullName)
    
          Write-Host -NoNewline -ForegroundColor Yellow "Copy "
          Write-Host $File.FullName
            
          ### Try to wait to finish the copy action
          Write-Host -ForegroundColor Yellow ("Wait: " + $ZIP_create_Wait)
          Start-sleep -milliseconds $ZIP_create_Wait
        }
      }
      Write-Host -backgroundcolor darkblue "\\Finish the ZIP-Process//" ""
      Write-Host ""
      Write-Host -ForegroundColor Yellow ("Wait: " + $ZIP_finish_Wait)
    
      Start-sleep -milliseconds $ZIP_finish_Wait
    }
    
    function refreshDir($dir){
        if(Test-Path ($dir)) {
    
            ### Loesche Pfad
            Write-Host -NoNewline -ForegroundColor Yellow "Delete:     "
            Write-Host -NoNewline $dir 
    
            Remove-Item -Path $dir -Recurse -Include *
    
            ### Ausgabe des Status
            If (Test-Path $dir) {Write-Host -foregroundcolor red " -unsuccessfully"} else {Write-Host -foregroundcolor green " -successfully" ""}
        }
    }
    
    
    refreshDir $PSConsoleLog
    start-transcript $PSConsoleLog -append
    
    Write-Host ""
    Write-Host -backgroundcolor blue "" "//////works\\\\\\" ""
           
    Write-Host ""
    Write-Host -backgroundcolor darkgreen "" "---------------|Stop Services|---------------"
    $requiredProcessList = New-Object System.Collections.ArrayList
    
    if ($requiredProcess.Length -ne 0){
        for($i = 0; $requiredProcess.Length -gt $i ; $i++){
            $GetService = $true
            try {$y = Get-Service $requiredProcess[$i] -ErrorAction Stop} catch {$GetService = $false}
    
            if ($GetService -eq $true){
                $StopService = $true
    
                Write-Host -NoNewline -ForegroundColor Yellow "Stop Service:       " 
                Write-Host $requiredProcess[$i]
    
                try {Stop-Service $requiredProcess[$i] -ErrorAction Stop} catch {$StopService = $false}
               
                Write-Host ""
                Write-Host -backgroundcolor blue "" "/// Service state \\\"
                get-Service $requiredProcess[$i]
                Write-Host -backgroundcolor darkblue "" "\\\ Service state ///" ""
                Write-Host ""
    
                if ($StopService -eq $true){
                    $requiredProcessList.Add($requiredProcess[$i])
    
                } else {
                    $successful = $false
                    Write-Host -ForegroundColor red ("Error:      required Service " + $requiredProcess[$i] + " no exsist!")
                }
            } 
        }
    
        ### Ueberspeichere Array, damit alle gefundenen Services wieder gestartet werden
        $requiredProcess = $requiredProcessList
    }
    
    Write-Host -backgroundcolor darkgreen "" "------------------|Backup|-------------------"
    if ($successful -eq $true){
        If ($DayOfWeek -eq "Sunday"){
            ### Woechentlicher Pfad und kopiere Dateien
            $KW = (Get-Date -UFormat %W ) + 1
            $Floor = [Math]::Floor($KW / $MaxWeekBackup)
            $WeekNumber =  $KW - ($Floor * $MaxWeekBackup)
            $BackupDir = $DestinationDirectory + "\Week_" + $WeekNumber + ".zip"
    
        } else {       
            ### Taeglicher Pfad und kopiere Dateien
            $BackupDir = $DestinationDirectory + "\Day_" + $DayOfWeek + ".zip"
        }
    
        ### Direktory vorbereiten / alte löschen
        refreshDir $BackupDir
         
        ### Direktory move
        Write-Host ""
        Write-Host -backgroundcolor blue "" "//// Copyprocess \\\\"
    
        Write-Host -NoNewline -ForegroundColor Yellow "Form:       "
        Write-Host $SourceDirectory
        Write-Host -NoNewline -ForegroundColor Yellow "To:         " 
        Write-Host $BackupDir
    
        Add-Zip $SourceDirectory $BackupDir
        
        ### Prüfe ob Moveingprozess funktionierte
        If (Test-Path $BackupDir){
            Write-Host -NoNewline -ForegroundColor yellow "Copyprocess:        "
            Write-Host -ForegroundColor green  "Copyprocess was successful"
    
        } else {
            Write-Host -NoNewline -ForegroundColor yellow "Copyprocess:        "
            Write-Host -ForegroundColor red "Copyprocess was unsuccessful - Error"
            $successful = $false
        }
        Write-Host -backgroundcolor darkblue "\\\\ Copyprocess ////" ""
        Write-Host ""
    
    } else {
        Write-Host -ForegroundColor red "Error:      No copy option!"
    }
    
    Write-Host -backgroundcolor darkgreen "" "---------------|Start Services|---------------"
    
    if ($requiredProcess.Length -ne 0){
        for($i = 0; $requiredProcess.Length -gt $i ; $i++){
            Write-Host -NoNewline -ForegroundColor Yellow "Start Process:     " 
            Write-Host $requiredProcess[$i]
    
            Start-Service $requiredProcess[$i]
        }
    }
    
    Write-Host -backgroundcolor darkblue "" "\\\\\\finish//////" ""
    Write-Host ""
    
    stop-transcript
    $PSConsoleString = [IO.File]::ReadAllText($PSConsoleLog)
    
    if ($Mail_Receiver -ne ""){
        Write-Host ""
        Write-Host -backgroundcolor darkgreen "" "------------------|Send Mail|-------------------"
    
        if ($successful -eq $true) {
                
            ### Header erstellen "Erfolgreich"
            $subject = $Mail_subject + "Successfully - " + $PC_Name
        } else {
    
            ### Header erstellen "Erfolglos"
            $subject = $Mail_subject + "Error - " + $PC_Name
        }
    
        ### Mail wird versendet
        Send-MailMessage -To $Mail_Receiver -Subject $subject -Body $PSConsoleString -From $Mail_Channel -SmtpServer $Mail_smtpServer
        Write-Host -NoNewline -ForegroundColor Yellow "Send Mail to: "
        Write-Host $Mail_Receiver
    }
    
    Write-Host ""
    Write-Host -backgroundcolor darkgreen "" "------------------|EventLog|-------------------"
    $source = "PowerShell-BackupSkript"
     
    ### Lege Pfad an
    if ([System.Diagnostics.EventLog]::SourceExists($source) -eq $false) {
        [System.Diagnostics.EventLog]::CreateEventSource($source, "Application")
        Write-Host -NoNewline -ForegroundColor Yellow "eventlog-ID:        "
        Write-Host $source
    }
            
    ### Bestimme Fehlertyp
    if ($successful -eq $true) {
                
        ### Erfolgreich
        $EntryType = "Information"
        $eventID = "9465"
    } else {
    
        ### Erfolglos
        $EntryType = "Warning" #or Error
        $eventID = "58858"
    }
    
    write-eventlog -logname Application -Source $source -eventID $eventID -message $PSConsoleString -EntryType $EntryType
    Write-Host -ForegroundColor Yellow "Create eventlog:    "
    Write-Host -NoNewline -ForegroundColor Yellow "eventlog-ID:        "
    Write-Host $eventID
    Write-Host -NoNewline -ForegroundColor Yellow "eventlog_EntryType: "
    Write-Host $EntryType

    Dienstag, 19. August 2014 07:02

Antworten

Alle Antworten