125 lines
4.1 KiB
PowerShell
125 lines
4.1 KiB
PowerShell
<#
|
|
.SYNOPSIS
|
|
VMs aus VMware suchen, deren Notes/Hinweise ein bestimmtes Datum enthalten.
|
|
|
|
.DESCRIPTION
|
|
Fragt alle virtuellen Maschinen aus VMware ab und filtert nach VMs,
|
|
deren Notes-Feld ein bestimmtes Datum enthaelt (z.B. *29.07.26*).
|
|
Das Suchmuster unterstuetzt Wildcards (* und ?).
|
|
|
|
.PARAMETER VIServer
|
|
Adresse des vCenter Servers (z.B. vcenter.firma.de)
|
|
|
|
.PARAMETER SearchPattern
|
|
Das Suchmuster fuer die Notes mit Wildcards.
|
|
Standard: *29.07.26* (Tag.Monat.Jahr im 2-stelligen Format)
|
|
Beispiele:
|
|
"*29.07.26*" - Alle VMs mit "29.07.26" irgendwo in den Notes
|
|
"*07.26*" - Alle VMs mit "07.26" (Monat.Jahr)
|
|
"*2026*" - Alle VMs mit "2026" in den Notes
|
|
|
|
.PARAMETER DateFormat
|
|
Optionales Datumsformat-Muster (Regex). Wenn gesetzt, werden alle VMs
|
|
gefunden die irgendein Datum im Format TT.MM.JJ in den Notes haben.
|
|
|
|
.EXAMPLE
|
|
.\Get-VMByDateInNotes.ps1 -VIServer vcenter.firma.de -SearchPattern "*29.07.26*"
|
|
|
|
.EXAMPLE
|
|
.\Get-VMByDateInNotes.ps1 -VIServer vcenter.firma.de -SearchPattern "*31.12.25*"
|
|
|
|
.EXAMPLE
|
|
# Alle VMs mit irgendeinem Datum im Format TT.MM.JJ:
|
|
.\Get-VMByDateInNotes.ps1 -VIServer vcenter.firma.de -DateFormat "\d{2}\.\d{2}\.\d{2}"
|
|
#>
|
|
|
|
[CmdletBinding(DefaultParameterSetName = "Wildcard")]
|
|
param(
|
|
[Parameter(Mandatory = $true)]
|
|
[string]$VIServer,
|
|
|
|
[Parameter(ParameterSetName = "Wildcard")]
|
|
[string]$SearchPattern = "*29.07.26*",
|
|
|
|
[Parameter(ParameterSetName = "Regex")]
|
|
[string]$DateFormat = ""
|
|
)
|
|
|
|
# PowerCLI-Modul laden
|
|
if (-not (Get-Module -Name VMware.PowerCLI -ListAvailable)) {
|
|
Write-Error "VMware.PowerCLI ist nicht installiert. Bitte installieren via: Install-Module -Name VMware.PowerCLI -Scope CurrentUser"
|
|
exit 1
|
|
}
|
|
|
|
Import-Module VMware.PowerCLI -ErrorAction Stop
|
|
|
|
# Verbindung zum vCenter herstellen
|
|
Write-Host "Verbinde mit vCenter: $VIServer ..." -ForegroundColor Cyan
|
|
try {
|
|
$null = Connect-VIServer -Server $VIServer -ErrorAction Stop
|
|
Write-Host "Verbindung erfolgreich." -ForegroundColor Green
|
|
}
|
|
catch {
|
|
Write-Error "Verbindung zum vCenter fehlgeschlagen: $_"
|
|
exit 1
|
|
}
|
|
|
|
# Alle VMs abrufen
|
|
Write-Host "Abfragen aller VMs ..." -ForegroundColor Cyan
|
|
$vms = Get-VM
|
|
|
|
$results = @()
|
|
|
|
if ($PSCmdlet.ParameterSetName -eq "Wildcard" -and -not $DateFormat) {
|
|
# Wildcard-Suche (Standard)
|
|
Write-Host "Suche in VM-Notes nach Muster: $SearchPattern" -ForegroundColor Yellow
|
|
|
|
foreach ($vm in $vms) {
|
|
$notes = $vm.Notes
|
|
if ($notes -and $notes -like $SearchPattern) {
|
|
$results += [PSCustomObject]@{
|
|
VMName = $vm.Name
|
|
Notes = $notes
|
|
State = $vm.PowerState
|
|
Cluster = (Get-Cluster -VM $vm -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Name -ErrorAction SilentlyContinue)
|
|
Host = ($vm.VMHost.Name)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
# Regex-Suche - alle VMs mit irgendeinem Datum im Format finden
|
|
if (-not $DateFormat) {
|
|
$DateFormat = '\d{2}\.\d{2}\.\d{2}'
|
|
}
|
|
Write-Host "Suche in VM-Notes nach Regex-Muster: $DateFormat" -ForegroundColor Yellow
|
|
|
|
foreach ($vm in $vms) {
|
|
$notes = $vm.Notes
|
|
if ($notes -and $notes -match $DateFormat) {
|
|
$results += [PSCustomObject]@{
|
|
VMName = $vm.Name
|
|
Notes = $notes
|
|
State = $vm.PowerState
|
|
Cluster = (Get-Cluster -VM $vm -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Name -ErrorAction SilentlyContinue)
|
|
Host = ($vm.VMHost.Name)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
# Ergebnisse anzeigen
|
|
Write-Host ""
|
|
if ($results.Count -eq 0) {
|
|
Write-Host "Keine VMs mit dem Suchmuster '$SearchPattern' in den Notes gefunden." -ForegroundColor Red
|
|
}
|
|
else {
|
|
Write-Host "Gefundene VMs: $($results.Count)" -ForegroundColor Green
|
|
Write-Host ("=" * 80)
|
|
$results | Format-Table -AutoSize -Wrap
|
|
}
|
|
|
|
# Verbindung trennen
|
|
Disconnect-VIServer -Server $VIServer -Confirm:$false
|
|
Write-Host "Verbindung getrennt. Fertig." -ForegroundColor Cyan
|