function InstallOffice { param ( [string] $mode ) if ($mode -eq "offline") { DownloadFiles -Type "Office" $imagePath = Join-Path $tmpPath "O365ProPlusRetail.img" Write-Host "Starting Microsoft Office Installation..." if (Test-Path -Path $imagePath -PathType Leaf) { $mountResult = Mount-DiskImage -ImagePath $imagePath -PassThru $driveLetter = ($mountResult | Get-Volume).DriveLetter $setupPath = "${driveLetter}:\Office\Setup64.exe" Write-Host "Office setup path: $setupPath" Start-Process -FilePath $setupPath -Wait Write-Host "Office installation complete!" } else { Write-Host "Office setup files not found." } } elseif ($mode -eq "online") { DownloadFiles -Type "Office-online" $setupPath = Join-Path $tmpPath "OfficeSetup.exe" Write-Host "Starting Microsoft Office Installation..." if (Test-Path -Path $setupPath -PathType Leaf) { Start-Process -FilePath $setupPath -Wait Write-Host "Office installation complete!" } else { Write-Host "Office setup files not found." } } else { Write-Host "Invalid mode. Please choose 'online' or 'offline'." } } function CheckIfOfficeIsInstalled { $officeRegistryPath = "HKLM:\SOFTWARE\Microsoft\Office\ClickToRun" if (Test-Path -Path $officeRegistryPath) { $installedPath = Get-ItemProperty -Path $officeRegistryPath | Select-Object -ExpandProperty "InstallPath" if ($installedPath) { Write-Host "Microsoft Office is installed at: $installedPath" if ($installedPath -like "C:\Program Files*") { Write-Host "Office is installed as 64-bit." } elseif ($installedPath -like "C:\Program Files (x86)*") { Write-Host "Office is installed as 32-bit." } else { Write-Host "Office architecture is unknown." } } else { Write-Host "Microsoft Office is installed, but the path could not be determined." } return $true } else { return $false } } function SetupOffice { Write-Host "Setting up Microsoft Office..." if (CheckIfOfficeIsInstalled) { Write-Host "Office is already installed! Skipping..." } else { Write-Host "Office is not installed." InstallOffice -mode "online" Start-Sleep -s 20 if (-not (CheckIfOfficeIsInstalled)) { Write-Host "Office installation failed. Trying again using offline setup..." InstallOffice -mode "offline" } } }