feat: Add Get-YesterdayCitrixRegistration.ps1 - SCCM deployments vom Vortag, Citrix Registration Check, CSV-Log

This commit is contained in:
buiaca 2026-07-29 23:39:44 +02:00
parent 5666a8df0d
commit 786ab05ae3

View File

@ -0,0 +1,160 @@
<#
.SYNOPSIS
Liest SCCM-App-Deployments vom Vortag aus, extrahiert Hostnames aus den Namen
und prüft in Citrix, ob die jeweiligen VDAs registriert (eingeschaltet + registriert) sind.
Ergebnisse werden in ein Log geschrieben.
.DESCRIPTION
1. Holt alle App-Deployments vom Vortag (oder seit dem letzten Lauf).
2. Extrahiert den Hostnamen aus dem Deployment-Namen (erste Bestandteile vor Bindestrich/Trennzeichen).
3. Queryt Citrix Broker, ob die Maschine registered ist.
4. Schreibt Ergebnisse in ein CSV-Log unter \\server\share\Logs\ oder lokal.
.NOTES
Datei: Get-YesterdayCitrixRegistration.ps1
Autor: alin
Umgebung: SCCM + Citrix VDA; Citrix Broker PowerShell SDK vorausgesetzt.
Ort: Hermes/ (gemäss Gitea-Konvention)
#>
[CmdletBinding()]
param(
[string]$LogPath = "C:\Logs\CitrixRegistration",
[string]$SCCMServer = "sccm-server",
[string]$CitrixSiteServer = "citrix-site-server",
[int]$DaysBack = 1
)
# --- Logging ---
$LogFile = Join-Path $LogPath "CitrixRegistration_$(Get-Date -Format 'yyyyMMdd_HHmmss').log"
$CsvFile = Join-Path $LogPath "CitrixRegistration_$(Get-Date -Format 'yyyyMMdd').csv"
function Write-Log {
param([string]$Message, [string]$Level = "INFO")
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$entry = "[$timestamp] [$Level] $Message"
Write-Host $entry
Add-Content -Path $LogFile -Value $entry -Encoding UTF8
}
# --- Pfade + SCCM-Modul laden ---
try {
Import-Module ConfigurationManager -ErrorAction Stop
Write-Log "SCCM-Modul geladen."
} catch {
Write-Log "SCCM-Modul konnte nicht geladen werden: $_" "ERROR"
exit 1
}
try {
Import-Module Citrix.Broker.Admin.V2 -ErrorAction Stop
Write-Log "Citrix Broker-Modul geladen."
} catch {
Write-Log "Citrix Broker-Modul konnte nicht geladen werden: $_" "ERROR"
exit 1
}
# SCCM Drive verbinden
$SiteCode = $(Get-PSDrive -PSProvider CMSite -ErrorAction SilentlyContinue | Select-Object -First 1).Name
if (-not $SiteCode) {
Write-Log "Kein SCCM-Site-Code gefunden. Bitte Site-Setup pruefen." "ERROR"
exit 1
}
Set-Location "$($SiteCode):\"
Write-Log "SCCM-Site: $SiteCode"
# --- Vortag berechnen ---
$Yesterday = (Get-Date).AddDays(-$DaysBack).Date
$Tomorrow = $Yesterday.AddDays(1)
Write-Log "Pruefe Deployments vom $Yesterday"
# --- SCCM App-Deployments vom Vortag holen ---
$Deployments = Get-CMApplicationDeployment |
Where-Object { $_.CollectionName -and $_.LastModificationTime -ge $Yesterday -and $_.LastModificationTime -lt $Tomorrow } |
Select-Object -Unique -Property Name, CollectionName, LastModificationTime
if (-not $Deployments -or @($Deployments).Count -eq 0) {
Write-Log "Keine neuen Deployments vom $Yesterday gefunden." "WARNING"
# Auch ohne Deployments beenden und Log schreiben
exit 0
}
Write-Log "$($Deployments.Count) Deployment(s) vom Vortag gefunden."
# --- Hostnames aus Deployment-Namen extrahieren ---
$Results = @()
foreach ($Deployment in $Deployments) {
$deploymentName = $Deployment.Name
# Hostname-Extraktion: typischer Format "HOSTNAME - Beschreibung" oder "HOSTNAME_Beschreibung"
# Verschiedene Trennzeichen durchprobieren
$hostname = $null
if ($deploymentName -match '^([A-Za-z0-9_-]+)[\s_\-]\s*') {
$hostname = $Matches[1]
}
elseif ($deploymentName -match '^([A-Za-z0-9_-]+)$') {
$hostname = $deploymentName
}
if (-not $hostname) {
Write-Log "Konnte keinen Hostnamen aus Deployment-Namen '$deploymentName' extrahieren." "WARN"
continue
}
Write-Log "Pruefe Maschine: $hostname (Deployment: $deploymentName)"
# --- Citrix Registrierungsstatus pruefen ---
$citrixStatus = @{
MachineName = $hostname
DeploymentName = $deploymentName
CheckTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
IsRegistered = $false
IsPoweredOn = $false
PowerState = "Unknown"
RegistrationState = "Unknown"
}
try {
$brokerMachine = Get-BrokerMachine -MachineName $hostname -ErrorAction Stop
if ($brokerMachine) {
$citrixStatus.IsRegistered = $true
Write-Log " -> REGISTRIERT: Ja"
} else {
Write-Log " -> REGISTRIERT: Nein (Maschine nicht in Citrix gefunden)" "WARN"
}
# PowerState pruefen
$powerState = $brokerMachine.PowerState
$citrixStatus.PowerState = $powerState
$citrixStatus.IsPoweredOn = ($powerState -eq 'On')
Write-Log " -> PowerState: $powerState"
} catch {
Write-Log " -> FEHER bei Citrix-Abfrage fuer $hostname : $_" "ERROR"
$citrixStatus.RegistrationState = "Error"
}
$Results += [PSCustomObject]$citrixStatus
}
# --- CSV schreiben ---
if ($Results.Count -gt 0) {
$Results | Export-Csv -Path $CsvFile -NoTypeInformation -Encoding UTF8 -Delimiter ";"
Write-Log "CSV-Log geschrieben: $CsvFile ($($Results.Count) Eintraege)"
} else {
Write-Log "Keine Ergebnisse zum Schreiben." "WARNING"
}
# --- Zusammenfassung ---
$registered = ($Results | Where-Object { $_.IsRegistered -eq $true }).Count
$notRegistered = ($Results | Where-Object { $_.IsRegistered -eq $false -and $_.RegistrationState -ne "Error" }).Count
$errors = ($Results | Where-Object { $_.RegistrationState -eq "Error" }).Count
Write-Log "=== ZUSAMMENFASSUNG ==="
Write-Log " Gesamt geprueft: $(@($Results).Count)"
Write-Log " Registriert: $registered"
Write-Log " Nicht registriert: $notRegistered"
Write-Log " Fehler: $errors"
Write-Log "Log-Datei: $LogFile"
Write-Log "CSV-Datei: $CsvFile"