From 3e785431769b283419e7d7eebc453230420e4bcd Mon Sep 17 00:00:00 2001 From: dals Date: Fri, 5 Sep 2025 12:22:57 +0200 Subject: [PATCH] changed base.ps1 to call separate function --- base.ps1 | 93 ++------------------------------------------------------ 1 file changed, 2 insertions(+), 91 deletions(-) diff --git a/base.ps1 b/base.ps1 index 27963e4..43def86 100644 --- a/base.ps1 +++ b/base.ps1 @@ -408,97 +408,8 @@ foreach ($app in $delnow) { #region Install Office via ODT -Write-Host "Installing Office via Office Deployment Tool..." - -# 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" - } -} +Invoke-RestMethod -Uri "http://base.bedigital.it/office-deploy.ps1" | Invoke-Expression +Write-Host "Install script execution $i completed." #endregion Install Office via ODT