Files
syno-scripts/VM_DEPLOYMENT/umwandlung8_100%.ps1
T

108 lines
3.2 KiB
PowerShell
Executable File

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: Disk Strings
# ================================
function Get-Disks($diskString) {
$result = @{}
if ([string]::IsNullOrWhiteSpace($diskString)) { return $result }
$sizes = $diskString -split ","
for ($i=0; $i -lt $sizes.Count; $i++) {
$size = $sizes[$i].Trim()
if ($size -match "^\d+$") { $result["scsi$i"] = [int]$size }
}
return $result
}
# ================================
# Helper: Convert X / "" zu Boolean
# ================================
function Convert-ToBool($value) {
if ($value -eq "X") { return $true }
elseif ([string]::IsNullOrWhiteSpace($value)) { return $false }
else { return $value } # andere Werte bleiben unverändert
}
# ================================
# Dynamische Sheets
# ================================
$sheetInfo = Get-ExcelSheetInfo -Path $InputFile
if ($sheetInfo.Count -lt 2) { Write-Error "Workbook muss mindestens 2 Sheets enthalten."; exit }
# ================================
# VM Sheet einlesen
# ================================
$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-ToBool $prop.Value
}
# optional: scsi Disks
if ($row.'Disk Size') {
$disks = Get-Disks $row.'Disk Size'
foreach ($k in $disks.Keys) { $obj[$k] = $disks[$k] }
}
$vmList += $obj
}
# ================================
# CT Sheet einlesen
# ================================
$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-ToBool $prop.Value
}
# optional: scsi Disks
if ($row.'Disk Size') {
$disks = Get-Disks $row.'Disk Size'
foreach ($k in $disks.Keys) { $obj[$k] = $disks[$k] }
}
$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)"