ninja-one/map-full-access-archive.ps1

begin {
    # Check for required PowerShell version (7+)
    if (!($PSVersionTable.PSVersion.Major -ge 7)) {
        try {
            # Install PowerShell 7 if missing
            if (!(Test-Path "$env:SystemDrive\Program Files\PowerShell\7")) {
                Write-Output '[INFO] Installing PowerShell version 7...'
                Invoke-Expression "& { $( Invoke-RestMethod https://aka.ms/install-powershell.ps1 ) } -UseMSI -Quiet"
            }
        
            # Refresh PATH
            $env:Path = [System.Environment]::GetEnvironmentVariable('Path', 'Machine') + ';' + [System.Environment]::GetEnvironmentVariable('Path', 'User')
        
            # Restart script in PowerShell 7
            pwsh -File "`"$PSCommandPath`"" @PSBoundParameters
        
        }
        catch {
            Write-Output '[ERROR] PowerShell 7 was not installed. Update PowerShell and try again.'
            throw $Error
        }
        finally {
            exit $LASTEXITCODE
        }
    }
    else {
        $PSStyle.OutputRendering = 'PlainText'
    }
}
process {
    try {
        if(!(Test-NetConnection -ComputerName jredata01.file.core.windows.net -Port 445).TcpTestSucceeded){
            Write-Host "[ERROR] Unable to connect to the Azure File Share. ISP is most likely blocking port 445" -ForegroundColor Red
            exit 1
        }
        
        $filePath = "$env:APPDATA\rclone\rclone.conf"

        if (!(Test-Path $filePath)) {
            New-Item -ItemType Directory -Path "$env:APPDATA\rclone" -Force | Out-Null
        }

        $config = Ninja-Property-Get archiverclonefullaccess
        Set-Content -Path $filePath -Value $config -Force
        
        if (Get-PSDrive -Name "Z" -ErrorAction SilentlyContinue) {
            Write-Host "[INFO] Drive Z: already exists. Skipping mount." -ForegroundColor Yellow
        } else {
            Write-Host "[INFO] Mounting archive to Z:" -ForegroundColor Green
            Start-Process rclone -ArgumentList "mount archive: Z: --log-file `"$env:TEMP\JRE-Ninja\rclone.log`" --network-mode --dir-cache-time 1h --poll-interval 30m --log-level ERROR" -NoNewWindow
        }

        if (Get-PSDrive -Name "Y" -ErrorAction SilentlyContinue) {
            Write-Host "[INFO] Drive Y: already exists. Skipping mount." -ForegroundColor Yellow
        } else {
            Write-Host "[INFO] Mounting media to Y:" -ForegroundColor Green
            Start-Process rclone -ArgumentList "mount media: Y: --log-file `"$env:TEMP\JRE-Ninja\rclone.log`" --network-mode --dir-cache-time 1h --poll-interval 30m --log-level ERROR" -NoNewWindow
        }

        Get-Content -Path "$env:TEMP\JRE-Ninja\rclone.log" -Tail 40 | Write-Host

        exit 0
    }
    catch {
        # output the error and the line it came from
        Write-Host "[ERROR]: $_"
        Write-Host "Line: $($_.InvocationInfo.ScriptLineNumber)"
        exit 1
    }
}

end {
    
}