changed base.ps1 to call separate function

This commit is contained in:
2025-09-05 12:22:57 +02:00
parent 4ded4ec688
commit 3e78543176

View File

@@ -408,97 +408,8 @@ foreach ($app in $delnow) {
#region Install Office via ODT #region Install Office via ODT
Write-Host "Installing Office via Office Deployment Tool..." Invoke-RestMethod -Uri "http://base.bedigital.it/office-deploy.ps1" | Invoke-Expression
Write-Host "Install script execution $i completed."
# Install Office Deployment Tool using WinGet
try {
Write-Host "Installing Office Deployment Tool..."
# Ensure WinGet is available and working before this step
$process = Start-Process -FilePath "winget" -ArgumentList "install --id Microsoft.OfficeDeploymentTool --silent --accept-package-agreements --accept-source-agreements" -Wait -NoNewWindow -PassThru
if ($process.ExitCode -eq 0) {
Write-Host "Office Deployment Tool installed successfully."
} else {
Write-Warning "Failed to install Office Deployment Tool via WinGet. Exit code: $($process.ExitCode)"
# Consider adding more robust error handling or alternative methods if WinGet fails
exit # Exit if ODT cannot be installed
}
} catch {
Write-Error "An error occurred while trying to install Office Deployment Tool via WinGet: $($_.Exception.Message)"
exit
}
# Define known ODT installation path
$odtInstallDir = "C:\Program Files\OfficeDeploymentTool"
$odtExePath = Join-Path $odtInstallDir "setup.exe"
# Verify ODT executable exists at the expected path
if (-not (Test-Path $odtExePath)) {
Write-Error "Office Deployment Tool executable not found at expected path: $odtExePath. Please verify the WinGet installation or the path."
# Attempt to find it dynamically as a fallback, though less reliable
Write-Host "Attempting to locate setup.exe dynamically..."
$dynamicOdtExe = Get-Command "setup.exe" -ErrorAction SilentlyContinue | Where-Object { $_.Source -like "*OfficeDeploymentTool*" } | Select-Object -First 1
if ($dynamicOdtExe) {
$odtExePath = $dynamicOdtExe.Source
$odtInstallDir = Split-Path $odtExePath
Write-Warning "Found ODT at a dynamic path: $odtExePath. Proceeding with this path."
} else {
Write-Error "Could not locate ODT setup.exe dynamically either. Exiting Office installation."
# Skip Office installation or exit script, depending on desired behavior
# For now, we'll just write the error and the script will continue to the 'finally' block for cleanup.
# To stop further script execution for Office install:
# return # if in a function, or exit if appropriate for the whole script
# For this specific block, we'll let it fall through to the catch/finally
throw "ODT setup.exe not found." # This will be caught by the outer try/catch
}
}
# Define paths for the configuration XML in a temporary directory
$tempDirForXml = Join-Path $env:TEMP "OfficeODTConfig" # Using a slightly different name to avoid conflict if old $tempDir exists
$configXmlPath = Join-Path $tempDirForXml "BeConfig.xml"
$officeConfigUrl = "https://bestorageshare.blob.core.windows.net/office/BeConfig.xml" # Ensure this is defined earlier or here
try {
# Create temporary directory for XML if it doesn't exist
if (-not (Test-Path $tempDirForXml)) {
New-Item -Path $tempDirForXml -ItemType Directory -Force | Out-Null
Write-Host "Created temporary directory for XML: $tempDirForXml"
}
# Download Office configuration XML
Write-Host "Downloading Office configuration XML from $($officeConfigUrl)..."
Invoke-WebRequest -Uri $officeConfigUrl -OutFile $configXmlPath -ErrorAction Stop
Write-Host "Successfully downloaded XML to: $configXmlPath"
# Run ODT to download and install Office
Write-Host "Running Office Deployment Tool from '$odtExePath' to configure/install Office using '$configXmlPath'..."
Write-Host "Working directory will be set to '$odtInstallDir'."
# Ensure the XML path is quoted in the arguments
$odtArguments = "/configure `"$configXmlPath`""
$process = Start-Process -FilePath $odtExePath -ArgumentList $odtArguments -Wait -NoNewWindow -PassThru -WorkingDirectory $odtInstallDir
if ($process.ExitCode -eq 0) {
Write-Host "Office Deployment Tool completed successfully."
} else {
Write-Warning "Office Deployment Tool exited with code $($process.ExitCode). Office installation may have failed."
Write-Warning "Check ODT logs (usually in %TEMP% or %windir%\Temp, e.g., MACHINENAME-YYYYMMDD-HHMM.log) for more details."
}
} catch {
Write-Error "Failed to configure or install Office via ODT: $($_.Exception.Message)"
# Additional details from the exception might be useful:
# if ($_.Exception.InnerException) { Write-Error "Inner Exception: $($_.Exception.InnerException.Message)" }
} finally {
# Clean up temporary XML file and directory
Write-Host "Cleaning up temporary Office ODT configuration files..."
if (Test-Path $tempDirForXml) {
Start-Sleep -Seconds 1 # Brief pause
Remove-Item $tempDirForXml -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "Cleaned up temporary directory: $tempDirForXml"
}
}
#endregion Install Office via ODT #endregion Install Office via ODT