$password = ConvertTo-SecureString 'Paper123!@#' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential('INTRANET\sysadmin', $password)

Write-Host "================================================" -ForegroundColor Cyan
Write-Host "Creating Junction: COMMON -> _COMMON" -ForegroundColor Cyan
Write-Host "================================================" -ForegroundColor Cyan
Write-Host ""

Invoke-Command -ComputerName 192.168.0.6 -Credential $cred -ScriptBlock {
    Write-Host "[1/4] Backing up COMMON to COMMON.backup..." -ForegroundColor Yellow
    if (Test-Path "C:\Shares\test\COMMON.backup") {
        Remove-Item "C:\Shares\test\COMMON.backup" -Recurse -Force
    }
    Copy-Item "C:\Shares\test\COMMON" "C:\Shares\test\COMMON.backup" -Recurse -Force
    Write-Host "[OK] Backup created" -ForegroundColor Green
    Write-Host ""
    
    Write-Host "[2/4] Copying AUTOEXEC.BAT from COMMON to _COMMON..." -ForegroundColor Yellow
    Copy-Item "C:\Shares\test\COMMON\ProdSW\AUTOEXEC.BAT" "C:\Shares\test\_COMMON\ProdSW\" -Force
    Write-Host "[OK] AUTOEXEC.BAT copied to _COMMON" -ForegroundColor Green
    Write-Host ""
    
    Write-Host "[3/4] Removing COMMON directory..." -ForegroundColor Yellow
    Remove-Item "C:\Shares\test\COMMON" -Recurse -Force
    Write-Host "[OK] COMMON removed" -ForegroundColor Green
    Write-Host ""
    
    Write-Host "[4/4] Creating junction COMMON -> _COMMON..." -ForegroundColor Yellow
    cmd /c mklink /J "C:\Shares\test\COMMON" "C:\Shares\test\_COMMON"
    Write-Host "[OK] Junction created" -ForegroundColor Green
    Write-Host ""
    
    Write-Host "=== Verification ===" -ForegroundColor Yellow
    $junction = Get-Item "C:\Shares\test\COMMON" -Force
    Write-Host "COMMON LinkType: $($junction.LinkType)" -ForegroundColor Cyan
    Write-Host "COMMON Target: $($junction.Target)" -ForegroundColor Cyan
    Write-Host ""
    
    Write-Host "=== File count check ===" -ForegroundColor Yellow
    $underCount = (Get-ChildItem "C:\Shares\test\_COMMON\ProdSW" -File | Measure-Object).Count
    $commonCount = (Get-ChildItem "C:\Shares\test\COMMON\ProdSW" -File | Measure-Object).Count
    Write-Host "_COMMON\ProdSW: $underCount files" -ForegroundColor White
    Write-Host "COMMON\ProdSW: $commonCount files (via junction)" -ForegroundColor White
    
    if ($underCount -eq $commonCount) {
        Write-Host "[OK] File counts match!" -ForegroundColor Green
    } else {
        Write-Host "[ERROR] File counts differ!" -ForegroundColor Red
    }
}

Write-Host ""
Write-Host "================================================" -ForegroundColor Green
Write-Host "Junction Created Successfully!" -ForegroundColor Green
Write-Host "================================================" -ForegroundColor Green
Write-Host ""
Write-Host "Result:" -ForegroundColor Cyan
Write-Host "  COMMON is now a junction pointing to _COMMON" -ForegroundColor White
Write-Host "  Both paths access the same files" -ForegroundColor White
Write-Host "  Backup saved to: C:\Shares\test\COMMON.backup" -ForegroundColor White
