pixel0815
Goto Top

Erweiterung eines PS Skriptes - Copy von Dateien, ACL

Hallo zusammen,

ich würde untenstehendes Skript gerne so erweitern das die Quell und Zielordner

$SRC_DIR = 'QUELLPFAD'
$DST_DIR = 'Zielpfad'

und

$SFolder = 'QUELLPFAD'
$TFolder = 'ZIELPFAD'

aus einer CSV ausgelesen werden. So das ich jeweils QUELLPFAD;ZIELPFAD angebe und dann das ganze Skript einfach abarbeitet.
Kann man die Log Funktion simpel anpassen oder ist das so ein Akt? Ich find prinzipell das recht gut mit dem Start-Transcript.
Ist sowas einfach möglich?
Freu mich auf Antworten.

Gruß
Pixel face-wink


#FUNCTIONS
function Get-FileMD5 {
    Param([string]$file)
    $md5 = [System.Security.Cryptography.HashAlgorithm]::Create("MD5")  
    $IO = New-Object System.IO.FileStream($file, [System.IO.FileMode]::Open)
    $StringBuilder = New-Object System.Text.StringBuilder
    $md5.ComputeHash($IO) | % { [void] $StringBuilder.Append($_.ToString("x2")) }  
    $hash = $StringBuilder.ToString() 
    $IO.Dispose()
    return $hash
}
#VARIABLES

$date = get-date -format d
# replace \ by -
$time = get-date -format t
$month = get-date 
$month1 = $month.month
$year1 = $month.year

$date = $date.ToString().Replace(“/”, “-”)

$time = $time.ToString().Replace(":", "-")  
$time = $time.ToString().Replace(" ", "")  

$DebugPreference = "continue"  
#parameters
$SRC_DIR = 'QUELLPFAD'  
$DST_DIR = 'Zielpfad'  

$logs = ".\" + "Powershell-" + $date + "_" + $time + "_.txt"  
$zip = ".\" + "ZIPLOGFILE-" + $date + "_" + $time + "_.zip"  

start-transcript $logs
#SCRIPT MAIN
clear
$SourceFiles = GCI -Recurse $SRC_DIR | ? { $_.PSIsContainer -eq $false} #get the files in the source dir.
$SourceFiles | % { # loop through the source dir files
    $src = $_.FullName #current source dir file
    Write-Debug "---------------------------------------------------------------"  
    Write-Debug $src
    Write-Debug "---------------------------------------------------------------"  
    $dest = $src -replace $SRC_DIR.Replace('\','\\'),$DST_DIR #current destination dir file  
    if (test-path $dest) { #if file exists in destination folder check MD5 hash
        $srcMD5 = Get-FileMD5 -file $src
        Write-Debug "---------------------------------------------------------------"  
        Write-Debug "Source file hash: $srcMD5"  
        Write-Debug "---------------------------------------------------------------"  
        $destMD5 = Get-FileMD5 -file $dest
        Write-Debug "---------------------------------------------------------------"  
        Write-Debug "Destination file hash: $destMD5"  
        Write-Debug "---------------------------------------------------------------"  
        if ($srcMD5 -eq $destMD5) { #if the MD5 hashes match then the files are the same
            Write-Debug "---------------------------------------------------------------"  
            Write-Debug "File hashes match. File already exists in destination folder and will be skipped."  
            Write-Debug "---------------------------------------------------------------"  
            $cpy = $false
        }
        else { #if the MD5 hashes are different then copy the file and overwrite the older version in the destination dir
            $cpy = $true
            Write-Debug "---------------------------------------------------------------"  
            Write-Debug "File hashes don't match. File will be copied to destination folder."  
            Write-Debug "---------------------------------------------------------------"  
        }
    }
    else { #if the file doesn't in the destination dir it will be copied. 
        Write-Debug "File doesn't exist in destination folder and will be copied."  
        $cpy = $true
    }
    Write-Debug "Copy is $cpy"  
    if ($cpy -eq $true) { #copy the file if file version is newer or if it doesn't exist in the destination dir. 
        Write-Debug "---------------------------------------------------------------"  
        Write-Debug "Copying $src to $dest"  
        Write-Debug "---------------------------------------------------------------"  
        if (!(test-path $dest)) {
            New-Item -ItemType "File" -Path $dest -Force     
        }
        Copy-Item -Path $src -Destination $dest -Force
    }
}

#copy acl rechte auf neuen ordner

Write-Debug "---------------------------------------------------------------"  
Write-Debug "Kopiere ACL Berechtigungen von der Quelle auf die Source"  


$SFolder = 'QUELLPFAD'  
$TFolder = 'ZIELPFAD'  

$SFolderList = get-childitem -name $SFolder -recurse 
foreach ($Folder in $SFolderList) {
          $SFullPath = $SFolder + "\" + "$Folder"   
          $TFullPath = $TFolder + "\" + "$Folder"   
      $NewACL = Get-ACL "$SFullPath"  
Set-ACL "$TFullPath" $NewACL  
echo "ACL copied to $TFolder $Folder"   
}

#Erstelle ein ZIP und entferne die Logdatei

Stop-transcript


function create-7zip([String] $aDirectory, [String] $aZipfile){
    [string]$pathToZipExe = "$($Env:ProgramFiles)\7-Zip\7z.exe";  
    [Array]$arguments = "a", "-tzip", "$aZipfile", "$aDirectory", "-r";  
    & $pathToZipExe $arguments;
}

$date = get-date -format d
$date = $date.ToString().Replace(“/”, “-”)

create-7zip "$logs" "$zip"  

Remove-Item $Logs -Recurse -Force

Content-Key: 298465

Url: https://administrator.de/contentid/298465

Printed on: April 18, 2024 at 00:04 o'clock

Mitglied: 114757
Solution 114757 Mar 07, 2016, updated at Mar 08, 2016 at 09:24:18 (UTC)
Goto Top
Ist doch immer das gleiche, CSV als Objekt importieren und dann mit For-Each Schleife alle Zeilen abarbeiten...
$csv = Import-CSV 'c:\pfade.csv' -delimiter ';'  
$csv | %{
     # hier der Rest des Skriptes
     #...
     # In $_.Quellpfad steht immer der jeweilige Quellpfad der aktuellen Zeile und in $_.Zielpfad der Gegenpart, wenn die Überschriften deiner CSV so lauten.
}
Gruß jodel32