
#This script should only ever be used after direct consultation with MLtek Support (Support@MLtekSoftware.com)
#It is intended to be used when a live file system is being migrated to a new host. 
#It is used to update the symlinks in the live system to reflect changes in the archive structure resulting from such a move. 

# Specify the start location
Write-Host "It's running"

$startLocation = "\\LiveServer\Share\"

# Function to recursively process folders and files
function Process-Folder($folderPath) {
    $items = Get-ChildItem -Path "$folderPath" -File

    foreach ($item in $items) {
        if ($item.Attributes -band [System.IO.FileAttributes]::ReparsePoint) {            
            $linkTarget = (Get-Item $item.FullName).Target
		Write-Host "$linkTarget"
            if ($linkTarget) {
                Write-Host "Is Symlink = True"
                $newTarget = $linkTarget.replace("\ArchiveServer\ArchiveShare\OldLiveServer-OldLiveShare","\ArchiveServer\ArchiveShare\NewLiveServer-NewLiveShare")
                
				# Remove existing symlink
				cmd.exe /c del "$($item.FullName)"
				
                # Update the symlink target
                cmd.exe /c mklink "$($item.FullName)" "$newTarget"
                
                Write-Host "Updated symlink $($item.FullName) to target: $newTarget"
            }
        }
    }
}

# Function to get a list of all directories recursively under the start location and call Process-Folder on them.
function Iterate-Folders($folder) {

	$folderitems = Get-ChildItem -Path $folder -Directory -Recurse

	foreach ($folderitem in $folderitems) {Write-Host "$($folderitem.FullName)" | Process-Folder $($folderitem.FullName)}
}


# Start processing from the root folder
Iterate-Folders $startLocation




