Compare commits

...

2 Commits

Author SHA1 Message Date
3e78543176 changed base.ps1 to call separate function 2025-09-05 12:22:57 +02:00
4ded4ec688 addedd separate office-deploy.ps1 2025-09-05 12:20:47 +02:00
2 changed files with 117 additions and 91 deletions

View File

@@ -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

115
office-deploy.ps1 Normal file
View File

@@ -0,0 +1,115 @@
<#
.SYNOPSIS
Script di livello enterprise per automatizzare il download e l'installazione di Microsoft Office tramite l'ODT.
.DESCRIPTION
Questo script autonomo gestisce l'intero processo: download dell'ultima versione dell'ODT, estrazione silenziosa,
e installazione di Microsoft 365 Apps for Enterprise basata su una configurazione predefinita.
.PARAMETER WorkingDirectory
Directory temporanea per il download e l'estrazione dei file.
.PARAMETER Mode
Specifica la modalità di esecuzione: 'Download', 'Configure' o 'All'.
.EXAMPLE
.\Deploy-Office.ps1 -Mode All
#>
param(
[Parameter(Mandatory = $false)]
[string]$WorkingDirectory = "C:\Temp\ODT_Install",
[Parameter(Mandatory = $false)]
[string]$Mode = 'All'
)
# --- Inizio Esecuzione ---
# Definizione della configurazione XML di Office
# Questo script installa Microsoft 365 Apps for Enterprise (64-bit) dal canale "Current".
# Esclude Groove, Lync e Teams. L'installazione è silenziosa.
$OfficeConfigurationXml = @"
<Configuration>
<Add OfficeClientEdition="64" Channel="Current">
<Product ID="O365ProPlusRetail">
<Language ID="MatchOS" />
<ExcludeApp ID="Groove" />
<ExcludeApp ID="Lync" />
<ExcludeApp ID="Teams" />
</Product>
</Add>
<RemoveMSI />
<Display Level="None" AcceptEULA="TRUE" />
<Property Name="FORCEAPPSHUTDOWN" Value="TRUE" />
</Configuration>
"@
# Funzione per ottenere l'URL di download più recente dell'ODT
function Get-LatestOdtUrl {
try {
$response = Invoke-WebRequest -Uri "https://www.microsoft.com/en-us/download/details.aspx?id=49117" -UseBasicParsing
$url = $response.Links | Where-Object { $_.href -like "*officedeploymenttool*.exe" } | Select-Object -ExpandProperty href -First 1
return $url
}
catch {
Write-Error "Impossibile recuperare l'URL di download dell'ODT. $_"
return $null
}
}
try {
# 1. Preparazione dell'ambiente
if (-not (Test-Path $WorkingDirectory)) {
New-Item -Path $WorkingDirectory -ItemType Directory -Force | Out-Null
Write-Host "Directory di lavoro creata in: $WorkingDirectory"
}
$OdtDownloaderPath = Join-Path $WorkingDirectory "officedeploymenttool.exe"
$SetupExePath = Join-Path $WorkingDirectory "setup.exe"
$ConfigurationXmlPath = Join-Path $WorkingDirectory "configuration.xml"
# Salvataggio del file di configurazione XML
$OfficeConfigurationXml | Out-File -FilePath $ConfigurationXmlPath -Encoding UTF8
Write-Host "File di configurazione di Office salvato in: $ConfigurationXmlPath"
# 2. Download ed Estrazione dell'ODT (se non già presente)
if (-not (Test-Path $SetupExePath)) {
Write-Host "setup.exe non trovato. Avvio del download dell'ODT..."
$latestOdtUrl = Get-LatestOdtUrl
if ($latestOdtUrl) {
Invoke-WebRequest -Uri $latestOdtUrl -OutFile $OdtDownloaderPath
if (-not (Test-Path $OdtDownloaderPath)) { throw "Download dell'ODT fallito." }
Write-Host "Estrazione silenziosa dell'ODT..."
$extractArgs = "/extract:$WorkingDirectory /quiet"
Start-Process -FilePath $OdtDownloaderPath -ArgumentList $extractArgs -Wait -NoNewWindow
if (-not (Test-Path $SetupExePath)) { throw "Estrazione dell'ODT fallita." }
Write-Host "Estrazione completata con successo."
}
} else {
Write-Host "setup.exe già presente. Saltato il download e l'estrazione."
}
# 3. Esecuzione delle modalità richieste
if ($Mode -in ('Download', 'All')) {
Write-Host "Avvio della modalità Download..."
$downloadArgs = "/download `"$ConfigurationXmlPath`""
$process = Start-Process -FilePath $SetupExePath -ArgumentList $downloadArgs -Wait -PassThru
if ($process.ExitCode -ne 0) { throw "La fase di Download è fallita con codice di uscita: $($process.ExitCode)" }
Write-Host "Download completato con successo."
}
if ($Mode -in ('Configure', 'All')) {
Write-Host "Avvio della modalità Configure..."
$configureArgs = "/configure `"$ConfigurationXmlPath`""
$process = Start-Process -FilePath $SetupExePath -ArgumentList $configureArgs -Wait -PassThru
if ($process.ExitCode -ne 0) { throw "La fase di Configure è fallita con codice di uscita: $($process.ExitCode)" }
Write-Host "Configurazione completata con successo."
}
}
catch {
Write-Error "Si è verificato un errore durante il processo di distribuzione: $_"
}
finally {
# Opzionale: Pulizia dei file di installazione
# Remove-Item -Path $WorkingDirectory -Recurse -Force
# Write-Host "Pulizia della directory di lavoro completata."
}