janikvonrotz
10/17/2014 - 5:05 PM

PowerShell: Sync and mount HSLU ILIAS drives#PowerShell

PowerShell: Sync and mount HSLU ILIAS drives#PowerShell

.\HSLUDriveConfig.ps1

$Syncs | ForEach-Object{

    Write-Host "`nRun sync for: $($_.Name)"

    $SourcePath = $_.SourcePath
    $DestinationPath = $_.DestinationPath
    $Exclude = $_.Exclude

    # Get directories
    $SourceDirectories = Get-ChildItem $SourcePath -Directory -Recurse | ForEach-Object{
        $Directory = $_
        if($Exclude){
            $Exclude | ForEach-Object{
                $Match = $_
                $Directory | where{-not $_.FullName.contains($Match)}
            }
        }else{
            $Directory
        }
    } | select @{L="RelativePath";E={$_.FullName -replace [regex]::Escape($SourcePath),""}}

    $DestinationDirectories = Get-ChildItem $DestinationPath -Directory -Recurse | ForEach-Object{
        $Directory = $_
        if($Exclude){
            $Exclude | ForEach-Object{
                $Match = $_
                $Directory | where{-not $_.FullName.contains($Match)}
            }
        }else{
            $Directory
        }
    } | select @{L="RelativePath";E={$_.FullName -replace [regex]::Escape($DestinationPath),""}}

    # create new directories

    $NewDirectories = @()
    if($SourceDirectories -eq $null){


    }elseif($DestinationDirectories -eq $null){

        $NewDirectories += $SourceDirectories | ForEach-Object{
            Join-Path $DestinationPath $_.RelativePath
        }
    }else{

         Compare-Object $DestinationDirectories $SourceDirectories -Property "RelativePath" | where{$_.SideIndicator -eq "=>"} | ForEach-Object{
    
            $NewDirectories += Join-Path $DestinationPath $_.RelativePath
        }
    }

    $NewDirectories | where{$_ -ne $null} | ForEach-Object{

        Write-Host "Create new directory: $_"
        New-Item -Type Directory -Path $_ -Confirm | Out-Null
    }

    # sync files
    $SyncDirectories = @()
    $SyncDirectories += $SourceDirectories | ForEach-Object{$_.RelativePath}
    $SyncDirectories += "\"

    $NewFiles = @()
    $SyncDirectories | where{$_ -ne $null} | ForEach-Object{
        
        # get files

        $SourceDirectory = Join-Path $SourcePath $_
        $DestinationDirectory = Join-Path $DestinationPath $_

        $SourceFiles = Get-ChildItem $SourceDirectory -File | select @{L="RelativeFilePath";E={$_.FullName -replace [regex]::Escape($SourcePath),""}},LastWriteTime
        $DestinationFiles = Get-ChildItem $DestinationDirectory -File -ErrorAction Continue | select @{L="RelativeFilePath";E={$_.FullName -replace [regex]::Escape($DestinationPath),""}},LastWriteTime
        
        # sync files

        if($SourceFiles -eq $null){

        }elseif($DestinationFiles -eq $null){

            $NewFiles += $SourceFiles | ForEach-Object{
                @{
                    Source = Join-Path $SourcePath $_.RelativeFilePath
                    Destination = Join-Path $DestinationPath $_.RelativeFilePath
                }
            }
        }else{

            $NewFiles += Compare-Object $DestinationFiles $SourceFiles -Property "RelativeFilePath","LastWriteTime" | where{$_.SideIndicator -eq "=>"} | ForEach-Object{

                # compare time stamp if file already exists

                $SourceFile = Get-ChildItem (Join-Path -Path $SourcePath -ChildPath $_.RelativeFilePath)
                $DestinationFile = Join-Path -Path $DestinationPath -ChildPath $_.RelativeFilePath
                if(Test-Path($DestinationFile)){
                    $DestinationFile = Get-ChildItem $DestinationFile
                }

                if(Test-Path($DestinationFile)){

                    # if source file is newer create a time stamp copy in order not to overwrite the edited file

                    if($DestinationFile.LastWriteTime -lt $SourceFile.LastWriteTime){

                        # create new file path
                        $TempDestinationFilePath = Join-Path (Split-Path $DestinationFile -Parent) $($SourceFile.Name + "#" + $((Get-date $SourceFile.LastWriteTime -Format s) -replace ":","-") + $SourceFile.Extension)
                    
                        # output copy job if destination file not already exists
                        if(-not (Test-Path $TempDestinationFilePath)){

                            @{
                                Source = $SourceFile.FullName
                                Destination = $TempDestinationFilePath
                            }
                        }                    
                    }else{
                        
                        # do not copy as my files are newer
                    }

                }else{
                    @{
                        Source = $SourceFile.FullName
                        Destination = $(if($DestinationFile.GetType().Name -eq "String"){$DestinationFile}else{$DestinationFile.FullName})
                    }
                }
            }
        }
    }
    # copy files

    $NewFiles | ForEach-Object{

        Write-Host "Copy file from: $($_.Source) to: $($_.Destination)"
        Copy-Item -Path $_.Source -Destination $_.Destination -Confirm | Out-Null
    }
}
.\HSLUDriveConfig.ps1

$Mounts | ForEach-Object{

    Write-Host "Mount WebDav folder for: $($_.Name) to: $($_.DriveLetter)"
    
    $Net = $(New-Object -ComObject WScript.Network);
    $Net.MapNetworkDrive($($_.DriveLetter + ":"), $($IliasUrl + $_.WebDavID + "/"), $true, $User, $Password);
    #New-PSDrive -Name $_.DriveLetter -PSProvider FileSystem -Root $($IliasUrl + $_.WebDavID + "/") -Persist
    #& net use $($_.DriveLetter + ":") $($IliasUrl + $_.WebDavID + "/") /User:$User $Password
}
$global:User = ""
$global:Password = ""
$global:IliasUrl = "https://elearning.hslu.ch/ilias/webdav.php/hslu/ref_"
$global:Credential = $(New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $(ConvertTo-SecureString -String $Password -AsPlainText -Force))

$global:Mounts = @{
    Name = "English"
    WebDavID = "2394466"
	DriveLetter = "E"
},
@{
    Name = "Informationssysteme"
    WebDavID = "2394472"
	DriveLetter = "I"
},
@{
    Name = "Programmierung"
    WebDavID = "2394470"
	DriveLetter = "P"
},
@{
    Name = "Kommunikation"
    WebDavID = "2394468"
	DriveLetter = "K"
},
@{
    Name = "Rechnungswesen, Betriebswirtschaftslehre, Volkswirtschaftslehere"
    WebDavID = "2394462"
	DriveLetter = "V"
},
@{
    Name = "Web"
    WebDavID = "2394474"
	DriveLetter = "W"
},
@{
    Name = "Mathematik"
    WebDavID = "2394464"
	DriveLetter = "M"		
} 

$global:Syncs = @{
    Name = "English"
    SourcePath = "E:\W.WIWSP01_A.H1471\Foren\"
    DestinationPath = "C:\OneDrive\Education\HSLU\English\"
},
@{
    Name = "Web"
    SourcePath = "W:\"
    DestinationPath = "C:\OneDrive\Education\HSLU\Web\"
    Exclude = "W:\W.WITEM12.H1471"
},
@{
    Name = "Rechnungswesen"
    SourcePath = "V:\FRW\"
    DestinationPath = "C:\OneDrive\Education\HSLU\Rechnungswesen\"
},
@{
    Name = "Mathematik"
    SourcePath = "M:\"
    DestinationPath = "C:\OneDrive\Education\HSLU\Mathematik\"
    Exclude = "M:\W.WIMAT01.H1471"
},
@{
    Name = "Volkswirtschaft"
    SourcePath = "V:\VWL\"
    DestinationPath = "C:\OneDrive\Education\HSLU\Volkswirtschaftslehre\"
},
@{
    Name = "Betriebswirtschaftslehre"
    SourcePath = "V:\BWL\"
    DestinationPath = "C:\OneDrive\Education\HSLU\Betriebswirtschaftslehre\"
},
@{
    Name = "Programmieren"
    SourcePath = "P:\"
    DestinationPath = "C:\OneDrive\Education\HSLU\Programmierung\"
    Exclude = "P:\W.WIINM11.H1471"
},
@{
    Name = "Kommunikation"
    SourcePath = "K:\W.WIDEU01.H1471_1\Dokumente\"
    DestinationPath = "C:\OneDrive\Education\HSLU\Kommunikation\"
},
@{
    Name = "Informationssysteme"
    SourcePath = "I:\"
    DestinationPath = "C:\OneDrive\Education\HSLU\Informationssysteme\"
    Exclude = "I:\W.WITEM11.H1471"
}
.\HSLUDriveConfig.ps1

$Mounts | ForEach{

    Write-Host "Dismount WebDav folder for: $($_.Name) on: $($_.DriveLetter)"

    net use $($_.DriveLetter + ":") /Delete
}