Files

163 lines
4.7 KiB
PowerShell
Executable File

# Import necessary modules
Import-Module Corsinvest.ProxmoxVE.Api
Import-Module ImportExcel
Add-Type -AssemblyName System.Windows.Forms
# Proxmox connection info
$ProxmoxURL = "https://your-proxmox-url:8006"
$ProxmoxUser = "root"
$ProxmoxPassword = "P@ssw0rd"
$ProxmoxRealm = "pam"
# Connect to Proxmox
$credentials = New-Object System.Management.Automation.PSCredential ($ProxmoxUser, (ConvertTo-SecureString $ProxmoxPassword -AsPlainText -Force))
$null = Connect-PveCluster -Server $ProxmoxURL -User $ProxmoxUser -Realm $ProxmoxRealm -Credential $credentials
# Read Excel data
$excelPath = "C:\path\to\depl_systems.xlsx"
$hostsData = Import-Excel -Path $excelPath
# Function to get VMs from Proxmox
function Get-ProxmoxVMs {
$vms = Get-PveVMList
return $vms
}
# Function to check missing VMs
function Get-MissingVMs {
$proxmoxVMs = Get-ProxmoxVMs
$missingVMs = @()
foreach ($host in $hostsData) {
$vmName = $host.'VM-Name'
if ($proxmoxVMs | Where-Object { $_.name -eq $vmName } -eq $null) {
$missingVMs += $host
}
}
return $missingVMs
}
# Function to get node usage
function Get-NodeUsage {
param ($node)
$usage = Get-PveNodeStatus -NodeName $node
return @{
Node = $node
MaxMem = $usage.memory.total
Mem = $usage.memory.used
MaxDisk = $usage.rootfs.total
Disk = $usage.rootfs.used
}
}
# Function to create and start VM
function Create-VM {
param (
$node,
$vmId,
$vmName,
$vmCores,
$vmMemory,
$vmDiskSize,
$isoFile
)
$params = @{
NodeName = $node
VMID = $vmId
Name = $vmName
Cores = $vmCores
Memory = $vmMemory
DiskSize = $vmDiskSize
ISOImage = $isoFile
}
New-PveVM @params
Start-PveVM -NodeName $node -VMID $vmId
Write-Host "VM wurde auf dem Node $node erstellt und gestartet"
}
# Create GUI
$form = New-Object System.Windows.Forms.Form
$form.Text = "Proxmox VM Manager"
$form.Size = New-Object System.Drawing.Size(600, 400)
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Size = New-Object System.Drawing.Size(560, 300)
$listBox.Location = New-Object System.Drawing.Point(10, 10)
$buttonCheck = New-Object System.Windows.Forms.Button
$buttonCheck.Text = "Check Missing VMs"
$buttonCheck.Size = New-Object System.Drawing.Size(120, 30)
$buttonCheck.Location = New-Object System.Drawing.Point(10, 320)
$buttonCheck.Add_Click({
$missingVMs = Get-MissingVMs
$listBox.Items.Clear()
if ($missingVMs.Count -eq 0) {
$listBox.Items.Add("Es fehlen keine VMs.")
} else {
foreach ($vm in $missingVMs) {
$listBox.Items.Add("$($vm.'VM-Name')")
}
}
})
$buttonDeploy = New-Object System.Windows.Forms.Button
$buttonDeploy.Text = "Deploy Missing VMs"
$buttonDeploy.Size = New-Object System.Drawing.Size(120, 30)
$buttonDeploy.Location = New-Object System.Drawing.Point(140, 320)
$buttonDeploy.Add_Click({
$missingVMs = Get-MissingVMs
if ($missingVMs.Count -eq 0) {
[System.Windows.Forms.MessageBox]::Show("Es fehlen keine VMs.")
return
}
foreach ($hostData in $missingVMs) {
# Create custom ISO for host (implement your own logic)
$customIsoPath = "C:\path\to\custom.iso"
# Upload ISO to Proxmox
$isoUploadPath = "/var/lib/vz/template/iso/custom.iso"
$null = Upload-PveFile -NodeName $bestNode.Node -ContentType iso -FilePath $customIsoPath -Storage local
# Get cluster nodes and their usage
$nodes = Get-PveNode
$nodeUsages = @()
foreach ($node in $nodes) {
$nodeUsages += Get-NodeUsage -node $node.name
}
# Select the best node based on available resources
$bestNode = $nodeUsages | Sort-Object {[math]::Min($_.MaxMem - $_.Mem, $_.MaxDisk - $_.Disk)} -Descending | Select-Object -First 1
Write-Host "Der beste Node zur Erstellung der VM ist: $($bestNode.Node)"
# VM creation parameters
$vmId = $hostData.'VM-ID'
$vmName = $hostData.'VM-Name'
$vmCores = $hostData.'Cores'
$vmMemory = $hostData.'Memory'
$vmDiskSize = $hostData.'Disk-Size'
$isoFile = "local:iso/custom.iso"
# Create and start the VM
Create-VM -node $bestNode.Node -vmId $vmId -vmName $vmName -vmCores $vmCores -vmMemory $vmMemory -vmDiskSize $vmDiskSize -isoFile $isoFile
}
[System.Windows.Forms.MessageBox]::Show("Alle fehlenden VMs wurden erstellt.")
})
$form.Controls.Add($listBox)
$form.Controls.Add($buttonCheck)
$form.Controls.Add($buttonDeploy)
# Load existing VMs into the listbox
$listBox.Items.Clear()
$vms = Get-ProxmoxVMs
foreach ($vm in $vms) {
$listBox.Items.Add($vm.name)
}
$form.ShowDialog()