54 lines
1.8 KiB
PowerShell
54 lines
1.8 KiB
PowerShell
Set-StrictMode -Version Latest
|
|
|
|
function New-AlphabeticalSchedulePlan {
|
|
param(
|
|
[Parameter(Mandatory)][string]$JobName,
|
|
[AllowEmptyCollection()][string[]]$VirtualMachines = @(),
|
|
[Parameter(Mandatory)][object[]]$ScheduleGroups
|
|
)
|
|
|
|
$Results = @()
|
|
foreach($Group in $ScheduleGroups) {
|
|
foreach($Property in @("Name", "From", "To", "Time")) {
|
|
if($Group.PSObject.Properties.Name -notcontains $Property) {
|
|
throw "Zeitplangruppe enthält nicht den Pflichtwert '$Property'."
|
|
}
|
|
}
|
|
|
|
if([string]$Group.Time -notmatch '^(?:[01]\d|2[0-3]):[0-5]\d$') {
|
|
throw "Ungültige Uhrzeit in Gruppe '$($Group.Name)': $($Group.Time)"
|
|
}
|
|
|
|
$Other = ([string]$Group.From -eq "Other") -and ([string]$Group.To -eq "Other")
|
|
$Members = @()
|
|
|
|
foreach($VM in $VirtualMachines) {
|
|
if([string]::IsNullOrWhiteSpace($VM)) { continue }
|
|
$Initial = $VM.Substring(0, 1).ToUpperInvariant()
|
|
$Letter = $Initial -match '^[A-Z]$'
|
|
$Match = $Other -and !$Letter
|
|
|
|
if(!$Other -and $Letter) {
|
|
$From = ([string]$Group.From).ToUpperInvariant()
|
|
$To = ([string]$Group.To).ToUpperInvariant()
|
|
$Match = ($Initial -ge $From) -and ($Initial -le $To)
|
|
}
|
|
|
|
if($Match) { $Members += $VM }
|
|
}
|
|
|
|
if($Members.Count -gt 0) {
|
|
$Results += [PSCustomObject]@{
|
|
Group = [string]$Group.Name
|
|
JobName = "{0}-{1}" -f $JobName, $Group.Name
|
|
Time = [string]$Group.Time
|
|
VirtualMachines = $Members
|
|
}
|
|
}
|
|
}
|
|
|
|
return $Results
|
|
}
|
|
|
|
Export-ModuleMember -Function New-AlphabeticalSchedulePlan
|