203 lines
7.0 KiB
PowerShell
203 lines
7.0 KiB
PowerShell
#Requires -Version 5.1
|
|
<#
|
|
.SYNOPSIS
|
|
Testet den SharePoint-Upload-Connector ohne E-Mail-Regeln.
|
|
.DESCRIPTION
|
|
Laedt eine Testdatei in die konfigurierte SharePoint-Dokumentbibliothek
|
|
und setzt die Metadaten-Felder. Dient zur Validierung der Konfiguration.
|
|
.PARAMETER TargetId
|
|
ID des SharePoint-Targets aus sharepoint-targets.json (Standard: "protokolle")
|
|
.PARAMETER FilePath
|
|
Pfad zu einer Testdatei die hochgeladen werden soll.
|
|
Wenn nicht angegeben, wird eine kleine Testdatei erzeugt.
|
|
.PARAMETER Thema
|
|
Wert fuer das Thema-Feld (Auswahlliste)
|
|
.PARAMETER DryRun
|
|
Wenn gesetzt, werden nur die Konfiguration und Site-Aufloesung getestet, kein Upload.
|
|
.EXAMPLE
|
|
.\Test-SharePointUpload.ps1 -DryRun
|
|
.\Test-SharePointUpload.ps1 -TargetId "protokolle" -Thema "Wartung MKini-Datacenter"
|
|
.\Test-SharePointUpload.ps1 -FilePath "C:\temp\bericht.pdf" -Thema "Wartung MKini-Datacenter"
|
|
#>
|
|
|
|
param(
|
|
[string]$TargetId = "protokolle",
|
|
[string]$FilePath = "",
|
|
[string]$Thema = "",
|
|
[string]$ConfigPath = (Join-Path $PSScriptRoot "..\config"),
|
|
[switch]$DryRun
|
|
)
|
|
|
|
$scriptRoot = $PSScriptRoot
|
|
Import-Module (Join-Path $scriptRoot "Logger.psm1") -Force
|
|
Import-Module (Join-Path $scriptRoot "GraphHelper.psm1") -Force
|
|
Import-Module (Join-Path $scriptRoot "SharePointHelper.psm1") -Force
|
|
|
|
# Logger starten
|
|
$logDir = Join-Path (Split-Path $ConfigPath -Parent) "logs"
|
|
Initialize-Logger -LogDirectory $logDir -Level "Debug"
|
|
|
|
# --- Konfiguration laden ---
|
|
$settingsFile = Join-Path $ConfigPath "settings.json"
|
|
$targetsFile = Join-Path $ConfigPath "sharepoint-targets.json"
|
|
|
|
if (-not (Test-Path $settingsFile)) {
|
|
Write-Host "FEHLER: $settingsFile nicht gefunden!" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
if (-not (Test-Path $targetsFile)) {
|
|
Write-Host "FEHLER: $targetsFile nicht gefunden!" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
$settings = Get-Content $settingsFile -Raw | ConvertFrom-Json
|
|
$settingsHash = @{
|
|
TenantId = $settings.TenantId
|
|
ClientId = $settings.ClientId
|
|
ClientSecret = $settings.ClientSecret
|
|
MailboxUser = $settings.MailboxUser
|
|
}
|
|
|
|
$targetsConfig = Get-Content $targetsFile -Raw | ConvertFrom-Json
|
|
$target = $targetsConfig.targets | Where-Object { $_.id -eq $TargetId }
|
|
|
|
if (-not $target) {
|
|
Write-Host "FEHLER: Target '$TargetId' nicht in sharepoint-targets.json gefunden!" -ForegroundColor Red
|
|
Write-Host "Verfuegbare Targets: $($targetsConfig.targets | ForEach-Object { $_.id } | Out-String)" -ForegroundColor Yellow
|
|
exit 1
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Host "=== SharePoint Upload Test ===" -ForegroundColor Cyan
|
|
Write-Host "Target: $($target.name) ($($target.id))"
|
|
Write-Host "Site: $($target.sharepoint.siteUrl)"
|
|
Write-Host "Bibliothek: $($target.sharepoint.libraryName)"
|
|
Write-Host ""
|
|
|
|
# --- Test 1: Site aufloesen ---
|
|
Write-Host "1. SharePoint Site aufloesen... " -NoNewline
|
|
try {
|
|
$site = Resolve-SharePointSite -Settings $settingsHash -SiteUrl $target.sharepoint.siteUrl
|
|
Write-Host "OK" -ForegroundColor Green
|
|
Write-Host " Site: $($site.displayName) (ID: $($site.id))"
|
|
}
|
|
catch {
|
|
Write-Host "FEHLER" -ForegroundColor Red
|
|
Write-Host " $_" -ForegroundColor Red
|
|
Write-Host ""
|
|
Write-Host "Pruefe die Site-URL und die Berechtigung Sites.Read.All" -ForegroundColor Yellow
|
|
exit 1
|
|
}
|
|
|
|
# --- Test 2: Dokumentbibliothek finden ---
|
|
Write-Host "2. Dokumentbibliothek suchen... " -NoNewline
|
|
try {
|
|
$drive = Get-DocumentLibrary -Settings $settingsHash -SiteId $site.id -LibraryName $target.sharepoint.libraryName
|
|
Write-Host "OK" -ForegroundColor Green
|
|
Write-Host " Bibliothek: $($drive.name) (ID: $($drive.id))"
|
|
Write-Host " URL: $($drive.webUrl)"
|
|
}
|
|
catch {
|
|
Write-Host "FEHLER" -ForegroundColor Red
|
|
Write-Host " $_" -ForegroundColor Red
|
|
|
|
# Alle verfuegbaren Bibliotheken anzeigen
|
|
Write-Host ""
|
|
Write-Host "Verfuegbare Bibliotheken auf dieser Site:" -ForegroundColor Yellow
|
|
try {
|
|
$allDrives = Invoke-GraphRequest -Endpoint "/sites/$($site.id)/drives" -Settings $settingsHash
|
|
foreach ($d in $allDrives.value) {
|
|
Write-Host " - $($d.name) : $($d.webUrl)" -ForegroundColor Yellow
|
|
}
|
|
} catch {}
|
|
exit 1
|
|
}
|
|
|
|
if ($DryRun) {
|
|
Write-Host ""
|
|
Write-Host "=== DRY RUN - kein Upload durchgefuehrt ===" -ForegroundColor Cyan
|
|
Write-Host "Site und Bibliothek erfolgreich aufgeloest!" -ForegroundColor Green
|
|
Write-Host "Starte ohne -DryRun und mit -FilePath fuer einen echten Upload-Test."
|
|
Write-Host ""
|
|
exit 0
|
|
}
|
|
|
|
# --- Test 3: Datei hochladen ---
|
|
# Testdatei erstellen falls keine angegeben
|
|
if (-not $FilePath) {
|
|
$FilePath = Join-Path $env:TEMP "mail-organizer-test.txt"
|
|
("Mail-Organizer SharePoint Upload Test`r`nDatum: " + (Get-Date -Format 'yyyy-MM-dd HH:mm:ss') + "`r`nTarget: " + $target.name) | Out-File $FilePath -Encoding UTF8
|
|
$isTestFile = $true
|
|
Write-Host " (Testdatei erstellt: $FilePath)"
|
|
} else {
|
|
$isTestFile = $false
|
|
if (-not (Test-Path $FilePath)) {
|
|
Write-Host "FEHLER: Datei nicht gefunden: $FilePath" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
Write-Host "3. Datei hochladen... " -NoNewline
|
|
try {
|
|
$driveItem = Upload-SharePointDocument `
|
|
-Settings $settingsHash `
|
|
-DriveId $drive.id `
|
|
-FilePath $FilePath
|
|
Write-Host "OK" -ForegroundColor Green
|
|
Write-Host " Datei: $($driveItem.name)"
|
|
Write-Host " URL: $($driveItem.webUrl)"
|
|
Write-Host " ID: $($driveItem.id)"
|
|
}
|
|
catch {
|
|
Write-Host "FEHLER" -ForegroundColor Red
|
|
Write-Host " $_" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
# --- Test 4: Metadaten setzen ---
|
|
$fields = @{}
|
|
|
|
# Datum setzen (Default: heute)
|
|
$datumDefault = $target.defaults.Datum -replace '\{today\}', (Get-Date -Format "yyyy-MM-dd")
|
|
$fields["Datum"] = $datumDefault
|
|
|
|
# Thema setzen
|
|
if ($Thema) {
|
|
$fields["Thema"] = $Thema
|
|
} elseif ($target.fields.Thema -and $target.fields.Thema.choices.Count -gt 0) {
|
|
$fields["Thema"] = $target.fields.Thema.choices[0]
|
|
Write-Host " (Thema nicht angegeben, verwende Standard: $($fields['Thema']))"
|
|
}
|
|
|
|
if ($fields.Count -gt 0) {
|
|
Write-Host "4. Metadaten setzen... " -NoNewline
|
|
try {
|
|
Set-SharePointItemFields `
|
|
-Settings $settingsHash `
|
|
-DriveId $drive.id `
|
|
-DriveItemId $driveItem.id `
|
|
-Fields $fields
|
|
Write-Host "OK" -ForegroundColor Green
|
|
foreach ($key in $fields.Keys) {
|
|
Write-Host " $key = $($fields[$key])"
|
|
}
|
|
}
|
|
catch {
|
|
Write-Host "WARNUNG" -ForegroundColor Yellow
|
|
Write-Host " Upload war erfolgreich, aber Metadaten konnten nicht gesetzt werden." -ForegroundColor Yellow
|
|
Write-Host " Fehler: $_" -ForegroundColor Yellow
|
|
Write-Host " Pruefe die Spaltennamen in der SharePoint-Bibliothek." -ForegroundColor Yellow
|
|
}
|
|
}
|
|
|
|
# Testdatei aufraeumen
|
|
if ($isTestFile -and (Test-Path $FilePath)) {
|
|
Remove-Item $FilePath -Force
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Host "=== SharePoint Upload Test erfolgreich! ===" -ForegroundColor Green
|
|
$msg = "Dokument wurde in " + $target.name + " hochgeladen."
|
|
Write-Host $msg
|
|
Write-Host ""
|