Skip to content

N.GodMode [Offline mode]

Trần Trung Trực edited this page Jun 16, 2026 · 1 revision

Mặc định, tác giả David Segura (Module powershell OSDCloud) quy định cài đặt tải file trực tiếp từ Microsoft cloud. Việc này đáp ứng được vấn đề cài đặt luôn mới nhất từ mạng

Trên thực tế, không phải ở đâu có có kết nối Internet tốc độ cao để có được một tiến trình cài đặt suôn sẻ và nhanh chóng, lúc này cơ chế làm việc off-line sẽ là lựa chọn tối ưu

GOD-MODE [HYBRID OFFLINE MODE]

Cơ chế tải file cài đặt từ cloud của OSDCloud (v.26.5.24.1) được quy định tại đường dẫn /osdcloud.26.5.24.1/private/steps/4-install/ cụ thể với file step-install-downloadwindowsimage.ps1

Để kích hoạt hệ thống hybrid offline mode, chúng ta cần can thiệp file này để điều hướng hoạt động của hệ thống ưu tiên tìm file cài đặt đang có sẵn tại máy trước khi tải từ Internet. Source code của file đã can thiệp điều hướng bên dưới

function step-install-downloadwindowsimage {
    [CmdletBinding()]
    param (
        $OperatingSystemObject = $global:OSDCloudWorkflowInvoke.OperatingSystemObject
    )
    #=================================================
    $Message = "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Start"
    Write-Debug -Message $Message; Write-Verbose -Message $Message
    $Step = $global:OSDCloudCurrentStep
    #=================================================
    # Is there an OperatingSystem Object?
    if (-not ($OperatingSystemObject)) {
        Write-Warning "[$(Get-Date -format s)] OperatingSystemObject is not set."
        Write-Warning 'Press Ctrl+C to exit OSDCloud'
        Start-Sleep -Seconds 86400
        exit
    }
    #=================================================
    # Is there a FilePath?
    if (-not ($OperatingSystemObject.FilePath)) {
        Write-Warning "[$(Get-Date -format s)] OperatingSystemObject does not have a FilePath to validate."
        Write-Warning 'Press Ctrl+C to exit OSDCloud'
        Start-Sleep -Seconds 86400
        exit
    }
    #=================================================
    # [CoreSystem-Godmod] - Quét hệ thống ổ đĩa local để tìm file cài đặt
    # Does the file exist on a Drive?
    $IsOffline = $false
    $FileName = $OperatingSystemObject.FileName
    $MatchingFiles = @()
    $MatchingFiles = Get-PSDrive -PSProvider FileSystem | ForEach-Object {
        Get-ChildItem "$($_.Name):\OSDCloud\OS\" -Include "$FileName" -File -Recurse -Force -ErrorAction Ignore
    }
    
    if ($MatchingFiles) {
        Write-Host -ForegroundColor Green "[$(Get-Date -format s)] [OK] Phát hiện bộ cài Offline!"
        $IsOffline = $true
        # Check hash SHA256
        $FileInfo = $MatchingFiles[0]
    }
    else {
        Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] Không tìm thấy bộ cài Offline"
    }

    #=================================================
    # [CoreSystem-GodMod] - Kích hoạt phương thức cài đặt trực tuyến khi không tìm thấy file cài offline
    if ($IsOffline -eq $false) {

        Write-Host -ForegroundColor Cyan "[$(Get-Date -format s)] Chuyển sang kiểm tra mạng để tải từ Microsoft Cloud..."

        # Is it reachable online?
        $IsOnline = $false
        try {
            $WebRequest = Invoke-WebRequest -Uri $OperatingSystemObject.FilePath -UseBasicParsing -Method Head
            if ($WebRequest.StatusCode -eq 200) {
                Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] OperatingSystemObject FilePath returned a 200 status code. OK."
                $IsOnline = $true
            }
        }
        catch {
            Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] OperatingSystemObject FilePath is not reachable."
        }

        # Nothing to do if it is unavailable online and offline
        if ($IsOnline -eq $false) {
            Write-Warning "[$(Get-Date -format s)] Tiến trình cài đặt thất bại do không thể tìm thấy bộ cài!"
            Write-Warning 'Press Ctrl+C to exit OSDCloud'
            Start-Sleep -Seconds 86400
            exit
        }

        # Create Download Directory
        $DownloadPath = "C:\OSDCloud\OS"
        $ItemParams = @{
            ErrorAction = 'SilentlyContinue'
            Force       = $true
            ItemType    = 'Directory'
            Path        = $DownloadPath
        }
        if (!(Test-Path $ItemParams.Path -ErrorAction SilentlyContinue)) {
            New-Item @ItemParams | Out-Null
        }

        # Is there a USB drive available?
        $USBDrive = Get-DeviceUSBVolume | Where-Object { ($_.FileSystemLabel -match "OSDCloud|USB-DATA") } | `
                    Where-Object { $_.SizeGB -ge 16 } | Where-Object { $_.SizeRemainingGB -ge 10 } | Select-Object -First 1
        
        Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] $($OperatingSystemObject.FilePath)"
        Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] FileName: $FileName"

        if ($USBDrive) {
            $USBDownloadPath = "$($USBDrive.DriveLetter):\OSDCloud\OS\$($OperatingSystemObject.OperatingSystem)"
            Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] DownloadPath: $USBDownloadPath"

            if (-not (Test-Path $USBDownloadPath)) {
                $null = New-Item -Path $USBDownloadPath -ItemType Directory -Force
            }
            $SaveWebFile = Invoke-OSDCloudDownloadFile -SourceUrl $OperatingSystemObject.FilePath -DestinationDirectory "$USBDownloadPath" -DestinationName $FileName

            if ($SaveWebFile) {
                Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] Copy Offline OS to $DownloadPath"
                $null = Copy-Item -Path $SaveWebFile.FullName -Destination $DownloadPath -Force
                $FileInfo = Get-Item "$DownloadPath\$($SaveWebFile.Name)"
            }
        }
        else {
            # $SaveWebFile is a FileInfo Object, not a path
            Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] DownloadPath: $DownloadPath"
            $SaveWebFile = Invoke-OSDCloudDownloadFile -SourceUrl $OperatingSystemObject.FilePath -DestinationDirectory $DownloadPath -ErrorAction Stop
            $FileInfo = $SaveWebFile
        }
    }

    #=================================================
    # Do we have FileInfo for the downloaded/offline file?
    if (-not ($FileInfo)) {
        Write-Warning "[$(Get-Date -format s)] Unable to locate or download the WindowsImage."
        Write-Warning 'Press Ctrl+C to exit OSDCloud'
        Start-Sleep -Seconds 86400
        exit
    }
    #=================================================
    # Store this as a FileInfo Object
    $global:OSDCloudWorkflowInvoke.FileInfoWindowsImage = $FileInfo
    $global:OSDCloudWorkflowInvoke.WindowsImagePath = $global:OSDCloudWorkflowInvoke.FileInfoWindowsImage.FullName
    Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] WindowsImagePath:  $($global:OSDCloudWorkflowInvoke.WindowsImagePath)"
    #=================================================
    # Check the File Hash
    if ($OperatingSystemObject.Sha1) {
        $FileHash = (Get-FileHash -Path $FileInfo.FullName -Algorithm SHA1).Hash
        Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] Microsoft Verified ESD SHA1: $($OperatingSystemObject.Sha1)"
        Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] Downloaded ESD SHA1: $FileHash"

        if ($OperatingSystemObject.Sha1 -notmatch $FileHash) {
            Write-Warning "[$(Get-Date -format s)] Unable to deploy this Operating System."
            Write-Warning "[$(Get-Date -format s)] Downloaded ESD SHA1 does not match the verified Microsoft ESD SHA1."
            Write-Warning 'Press Ctrl+C to exit OSDCloud'
            Start-Sleep -Seconds 86400
        }
        else {
            Write-Host -ForegroundColor Green "[$(Get-Date -format s)] Downloaded ESD SHA1 matches the verified Microsoft ESD SHA1. OK."
        }
    }
    if ($OperatingSystemObject.Sha256) {
        $FileHash = (Get-FileHash -Path $FileInfo.FullName -Algorithm SHA256).Hash
        Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] Microsoft Verified ESD SHA256: $($OperatingSystemObject.Sha256)"
        Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] Downloaded ESD SHA256: $FileHash"

        if ($OperatingSystemObject.Sha256 -notmatch $FileHash) {
            Write-Warning "[$(Get-Date -format s)] Unable to deploy this Operating System."
            Write-Warning "[$(Get-Date -format s)] Downloaded ESD SHA256 does not match the verified Microsoft ESD SHA256."
            Write-Warning 'Press Ctrl+C to exit OSDCloud'
            Start-Sleep -Seconds 86400
        }
        else {
            Write-Host -ForegroundColor Green "[$(Get-Date -format s)] Downloaded ESD SHA256 matches the verified Microsoft ESD SHA256. OK."
        }
    }
    #=================================================
    $Message = "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] End"
    Write-Verbose -Message $Message; Write-Debug -Message $Message
    #=================================================
}

Giải thích dễ hiểu: Khi hàm tải file này được gọi, nó sẽ tìm trong các thư mục tại máy thỏa các tiêu chí

  • Tên thư mục [Ổ đĩa:]OSDCloud\OS
  • Tên file cài đặt .esd nguyên bản từ Microsoft. Tên của các file này có sẵn trong \catalogs\operatingsystem của OSDCloud
  • Có thể sử dụng một công cụ nào đó để tải sẵn bộ cài .esd phù hợp nhu cầu

KỊCH BẢN VẬN HÀNH

  • Khi máy tính không kết nối mạng (offline), script trên sẽ kích hoạt quá trình tìm kiếm bộ cài đặt có sẵn trong máy tính (DVD/USB,SSD...), kiểm tra hash SHA256 đối chiếu với danh mục hệ điều hành tại OS-catalog. Nếu kiểm tra thành công, tiến hành các bước cài đặt tiếp theo đó
  • Khi máy tính có kết nối mạng (online), script cũng vận hành tương tự trên tức ưu tiên bộ cài có sẵn
  • Khi máy tính có kết nối mạng (online), nhưng bộ cài không có sẵn. Script lúc này tiến hành tải file từ cloud Microsoft như hệ thống nguyên bản

Clone this wiki locally