param( [string]$InputFile, [string]$OutputVM = "vm_output.json", [string]$OutputCT = "ct_output.json" ) # ================================ # Pfad Resolution # ================================ $basePath = if ($PSScriptRoot) { $PSScriptRoot } else { Split-Path -Parent $MyInvocation.MyCommand.Path } if (-not $InputFile) { $InputFile = Join-Path $basePath "vm_list.xlsx" } $OutputVM = Join-Path $basePath $OutputVM $OutputCT = Join-Path $basePath $OutputCT Write-Host "Using Excel file: $InputFile" # ================================ # ImportExcel Modul prüfen # ================================ if (-not (Get-Module -ListAvailable -Name ImportExcel)) { Write-Error "ImportExcel Modul nicht installiert. Bitte: Install-Module -Name ImportExcel" exit } Import-Module ImportExcel # ================================ # Helper: Value Converter # ================================ function Convert-Value($value) { if ($value -eq "X") { return $true } if ([string]::IsNullOrWhiteSpace($value)) { return $false } if ($value -is [double]) { return [int]$value } if ($value -match "^\d+(\.0)?$") { return [int]$value } return $value } # ================================ # Helper: Disk Mapping # ================================ function Get-DisksMapping($deviceInput, $sizeInput) { $devices = @() if ([string]::IsNullOrWhiteSpace($deviceInput) -or [string]::IsNullOrWhiteSpace($sizeInput)) { return @() } $deviceRaw = [string]$deviceInput $sizeRaw = [string]$sizeInput if ($deviceRaw -match "\." -and $sizeRaw -match "\.") { $deviceList = $deviceRaw -split "\." $sizeList = $sizeRaw -split "\." } elseif ($deviceRaw -match "," -or $sizeRaw -match ",") { $deviceList = $deviceRaw -split "," $sizeList = $sizeRaw -split "," } else { if ($deviceRaw -match "^\d+$" -and $sizeRaw -match "^\d+$") { return @( [PSCustomObject]@{ Device = [int]$deviceRaw Size = [int]$sizeRaw } ) } return @() } for ($i = 0; $i -lt $deviceList.Count; $i++) { $dev = $deviceList[$i].Trim() $size = if ($i -lt $sizeList.Count) { $sizeList[$i].Trim() } else { 0 } if ($dev -match "^\d+$") { $devices += [PSCustomObject]@{ Device = [int]$dev Size = [int]$size } } } return @($devices) } # ================================ # Sheets # ================================ $sheetInfo = Get-ExcelSheetInfo -Path $InputFile if ($sheetInfo.Count -lt 2) { Write-Error "Workbook muss mindestens 2 Sheets enthalten." exit } # ================================ # VM Sheet # ================================ $vmData = Import-Excel -Path $InputFile -WorksheetName $sheetInfo[0].Name $vmList = @() foreach ($row in $vmData) { if (-not $row.Node) { continue } $obj = @{ } foreach ($prop in $row.PSObject.Properties) { $obj[$prop.Name] = Convert-Value $prop.Value } # Disks $obj['Disks'] = @(Get-DisksMapping $row.'Device' $row.'Disk Size') # Alte Felder entfernen $obj.Remove('Device') $obj.Remove('Disk Size') $vmList += $obj } # ================================ # CT Sheet # ================================ $ctData = Import-Excel -Path $InputFile -WorksheetName $sheetInfo[1].Name $ctList = @() foreach ($row in $ctData) { if (-not $row.Node) { continue } $obj = @{ } foreach ($prop in $row.PSObject.Properties) { $obj[$prop.Name] = Convert-Value $prop.Value } # Disks $obj['Disks'] = @(Get-DisksMapping $row.'Device' $row.'Disk Size') # Alte Felder entfernen $obj.Remove('Device') $obj.Remove('Disk Size') $ctList += $obj } # ================================ # Export JSON # ================================ $vmList | ConvertTo-Json -Depth 5 | Out-File -Encoding UTF8 $OutputVM $ctList | ConvertTo-Json -Depth 5 | Out-File -Encoding UTF8 $OutputCT Write-Host "VM JSON: $OutputVM" Write-Host "CT JSON: $OutputCT" Write-Host "VM Count: $($vmList.Count)" Write-Host "CT Count: $($ctList.Count)"