############################################################### # # Scriptname: ReplicatePVSStores.ps1 # # Autor: Urs Heeb # Date: 30.11.2022 # # Version: 2022.11.01 /30.11.22 / Urs Heeb # Create script # # # Description: Script does following: # Check on which PVS it runs # Let the admin choose which Store should be replicated # Let the admin choose if all or just a selected vDisk should be replicated # # # Requirements: # Read/Write permissions on source/destination # Maximum of 5 dedicated vDisks to choose # ############################################################### # Script variables $PVS01 = "pvs01.domain.pit" $PVS02 = "pvs02.domain.pit" # Enumerate the local and the remote PVS $PVSLocal = $env:COMPUTERNAME + ".domain.pit" If ($PVSLocal -eq $PVS01) { $PVSRemote = $PVS02 } Else { $PVSRemote = $PVS01 } # Define PVS vDisk stores # Create an array for each store and let the user chose which one should be checked # If a store is added to the array, the option has to be added for the choice as well $StoreArray = @() $StoreArray += [pscustomobject]@{Store="VDI";StoreDisk="V";StorePath="\vDisks\VDI"} $StoreArray += [pscustomobject]@{Store="RDSH";StoreDisk="R";StorePath="\vDisks\RDSH"} $StoreArray += [pscustomobject]@{Store="Test";StoreDisk="T";StorePath="\vDisks\Test"} # Prompt for Store choice $StoreTitle = "PVS Store" $StoreMessage = "Which PVS store will you replicate" $Store1 = New-Object System.Management.Automation.Host.ChoiceDescription "&VDI Store", "VDI" $Store2 = New-Object System.Management.Automation.Host.ChoiceDescription "&RDSH Store", "RDSH" $Store3 = New-Object System.Management.Automation.Host.ChoiceDescription "&Test Store", "TEST" $StoreOptions = [System.Management.Automation.Host.ChoiceDescription[]]($Store1, $Store2, $Store3) $Store2Replicate=$host.ui.PromptForChoice($StoreTitle, $StoreMessage, $StoreOptions, 2) # Prepare variables after the user entry $StoreID = $Store2Replicate $StoreDisk = $StoreArray[$StoreID].StoreDisk $StorePath = $StoreArray[$StoreID].StorePath $LocalStore = $StoreDisk + ":" + $StorePath + "\" $RemoteStore = "\\" + $PVSRemote + "\" + $StoreDisk + "$" + $StorePath + "\" $LocalStorePath = $LocalStore + "*" $RemoteStorePath = $RemoteStore + "*" # Compare servers $LocalStoreContent = Get-ChildItem -Path $LocalStorePath -Exclude WriteCache,*.lok,*.i.vhdx $RemoteStoreContent = Get-ChildItem -Path $RemoteStorePath -Exclude WriteCache,*.lok,*.i.vhdx $StoreDiff = Compare-Object -ReferenceObject $LocalStoreContent -DifferenceObject $RemoteStoreContent -Property Name, LastWriteTime # Enumerate a list of vDisks # Create an array with all disk names $DiskArray = $null $DiskArray = @() $DiskID = 1 ForEach ($File in $StoreDiff) { If ($File.SideIndicator -eq "<="){ $Filename = $File.Name If ($Filename -like "*.vhd*") { $DiskID = $DiskID+1 $SplitChar = $Filename.IndexOf(".") $DiskName = $Filename.Substring(0, $SplitChar) $PVPName = $DiskName + ".pvp" $DiskArray += [pscustomobject]@{DiskID=$DiskID;vDisk=$Filename;vDiskName=$DiskName;PVPName=$PVPName} } } } # Prompt for replication choice $ReplTitle = "vDisk replication" $ReplMessage = "Choose what to replicate" $vDiskAll = New-Object System.Management.Automation.Host.ChoiceDescription "&All vDisks","Continue with all vDisks." $CancelAll = New-Object System.Management.Automation.Host.ChoiceDescription "&Cancel", "Skip this operation and all subsequent operations." # Generate choice output of each vDisk to replicate $ReplOptionCount = 0 ForEach ($vDisk in $DiskArray) { $ReplOptionCount = $ReplOptionCount +1 $ChoiceID = $vDisk.DiskID $ChoiceDisk = $vDisk.vDiskName $ChoiceCmd = New-Object System.Management.Automation.Host.ChoiceDescription "&$ChoiceID $ChoiceDisk", "Replicate selected vDisk." New-Variable "ReplOption$ChoiceID" $ChoiceCmd } # Generate $ReplOptions variable for final choice output depending on the amount of options Switch ($ReplOptionCount) { 0 {$ReplOptions = [System.Management.Automation.Host.ChoiceDescription[]]($vDiskAll, $CancelAll)} 1 {$ReplOptions = [System.Management.Automation.Host.ChoiceDescription[]]($vDiskAll, $CancelAll, $ReplOption2)} 2 {$ReplOptions = [System.Management.Automation.Host.ChoiceDescription[]]($vDiskAll, $CancelAll, $ReplOption2, $ReplOption3)} 3 {$ReplOptions = [System.Management.Automation.Host.ChoiceDescription[]]($vDiskAll, $CancelAll, $ReplOption2, $ReplOption3, $ReplOption4)} 4 {$ReplOptions = [System.Management.Automation.Host.ChoiceDescription[]]($vDiskAll, $CancelAll, $ReplOption2, $ReplOption3, $ReplOption4, $ReplOption5)} 5 {$ReplOptions = [System.Management.Automation.Host.ChoiceDescription[]]($vDiskAll, $CancelAll, $ReplOption2, $ReplOption3, $ReplOption4, $ReplOption5, $ReplOption6)} } $vDisk2Replicate=$host.ui.PromptForChoice($ReplTitle, $ReplMessage, $ReplOptions, 0) If ($vDisk2Replicate -eq 0) { # Copy all found vDisks and PVP files ForEach ($Disk in $DiskArray) { $SourcevDisk = $null $SourcePVP = $null $DestvDisk = $null $DestPVP = $null $vDiskFile = $Disk.vDisk $PVPFile = $Disk.PVPName $SourcevDisk = $LocalStore + $vDiskFile $SourcePVP = $LocalStore + $PVPFile $DestvDisk = $RemoteStore + $vDiskFile $DestPVP = $RemoteStore + $PVPFile Copy-Item $SourcevDisk -Destination $DestvDisk Copy-Item $SourcePVP -Destination $DestPVP } } ElseIf ($vDisk2Replicate -eq 1) { # Cancel all return; break } Else { # Copy the selected vDisk # ID has to be reduced by 2 to match the array IDs $SourceID = $vDisk2Replicate - 2 $SourcevDisk = $null $SourcePVP = $null $DestvDisk = $null $DestPVP = $null $vDiskFile = $DiskArray[$SourceID].vDisk $PVPFile = $DiskArray[$SourceID].PVPName $SourcevDisk = $LocalStore + $vDiskFile $SourcePVP = $LocalStore + $PVPFile $DestvDisk = $RemoteStore + $vDiskFile $DestPVP = $RemoteStore + $PVPFile Copy-Item $SourcevDisk -Destination $DestvDisk Copy-Item $SourcePVP -Destination $DestPVP }