none
PowerShell file copy with optional GUI

    Question

  • Hey All,

    I've just started a new job and although I'm a SQL DBA and have been for a while I am a complete newb with ps. Infact I have less then a weeks worth of experience!

    Below is a ps script that we use here to copy recent backups around; I understand it enough to read it but would like more control over the destination, age of file and maybe dare I say it, even an app-like front-end.

    Is this possible and can anyone help me out or point me in the right-direction?

    TIA.

    #
    #
    #   Script to archive files 
    #   
    
    [CmdletBinding(SupportsShouldProcess=$true)]
    Param (
        [Parameter(Mandatory=$true)]        
            [string]    $SourcePath,
        [Parameter(Mandatory=$true)]        
            [string]    $ArchivePath,
        [Parameter(Mandatory=$false)]
            [string]    $Match                  = '',
        [Parameter(Mandatory=$false)] 
        [ValidateSet('All','None','Newer','Larger')]    
            [string]    $OverwriteExisting      = 'Never',
        [Parameter(Mandatory=$false)]
            [switch]      $Remove                 = $false
    )
    
    Try {
    
        If (!(Test-Path $SourcePath -PathType Container)) {
            Write-Error ($SourcePath + ' is not a directory');
            Exit 1;
        }
    
        if (!(Test-Path $ArchivePath -PathType Container)) {
            Write-Error ($ArchivePath + ' is not a directory');
            Exit 1;
        }
    
        Get-ChildItem -File $SourcePath | 
    		Where-Object { $_.Name -match $Match } | 
    		ForEach-Object {
    
            $sizes = @{}; $times = @{};
            
            $sourceFile = $_; 
            $archiveFile = Get-ChildItem ($ArchivePath + '\' + $_.Name) -ErrorAction SilentlyContinue;
    
            $overwrite = $false;
    
            if (('All','None').Contains($OverwriteExisting)) {
                $overwrite = ($OverwriteExisting -eq 'All');
            }
            elseif ($archiveFile) {
                $sizes['source'] = $sourceFile.Length;
                $sizes['archive'] = $archiveFile.Length;
                $times['source'] = $sourceFile.LastWriteTime;
                $times['archive'] = $archiveFile.LastWriteTime;
                $larger = ($sizes['source'] -gt $sizes['archive']);
                $newer = ($times['source'] -gt $times['archive']);
    
                if ($larger -and $OverwriteExisting -eq 'Larger') {
                    $overwrite = $true;
                    Write-Verbose ($_.Name + ': source is larger than archive');
                }
                if ($newer -and $OverwriteExisting -eq 'Newer') {
                    $overwrite = $true;
                    Write-Verbose ($_.Name + ': source is newer than archive');
                }
    
            }
    
            if ((!$archiveFile) -or $overwrite) {
                Write-Verbose ('Archiving ' + $sourceFile + ' to ' + $ArchivePath);
                Copy-Item -Force -Path $sourceFile.FullName -Destination $ArchivePath;
                if ($Remove) {
                    Write-Verbose ('Removing ' + $sourceFile);
                    Remove-Item -Force $sourceFile.FullName;
                }
            }
        }
    }
    
    Catch {
        Write-Error $_;
        Exit 1;
    }
    

    • Moved by Bill_Stewart Friday, March 15, 2019 5:29 PM This is not "fix/debug/rewrite my script for me" forum
    Tuesday, January 8, 2019 10:48 AM

All replies