From f906f41364ec140ca79acfcd8fb93d2097250f19 Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Wed, 7 May 2025 11:12:19 -0400 Subject: [PATCH] Add padded counter and AutoSMBIOS parameters to New-ProxmoxVMFromTemplate --- .../cmdlets/New-ProxmoxVMFromTemplate.md | 76 ++++++++++++++++++- ...Advanced-MultipleVMsWithPaddedCounters.ps1 | 56 ++++++++++++++ Module/PSProxmox.psd1 | 5 +- .../Cmdlets/NewProxmoxVMFromTemplateCmdlet.cs | 57 ++++++++++++-- PSProxmox/Models/ProxmoxVMBuilder.cs | 8 +- 5 files changed, 188 insertions(+), 14 deletions(-) create mode 100644 Documentation/examples/Advanced-MultipleVMsWithPaddedCounters.ps1 diff --git a/Documentation/cmdlets/New-ProxmoxVMFromTemplate.md b/Documentation/cmdlets/New-ProxmoxVMFromTemplate.md index 7b6d2bb..1e86dce 100644 --- a/Documentation/cmdlets/New-ProxmoxVMFromTemplate.md +++ b/Documentation/cmdlets/New-ProxmoxVMFromTemplate.md @@ -19,6 +19,8 @@ New-ProxmoxVMFromTemplate [-NetworkBridge ] [-Description ] [-IPPool ] + [-AutomaticSMBIOS] + [-SMBIOSProfile ] [-Start] [] ``` @@ -31,6 +33,7 @@ New-ProxmoxVMFromTemplate -Prefix -Count [-StartIndex ] + [-CounterDigits ] [-Memory ] [-Cores ] [-DiskSize ] @@ -39,6 +42,8 @@ New-ProxmoxVMFromTemplate [-NetworkBridge ] [-Description ] [-IPPool ] + [-AutomaticSMBIOS] + [-SMBIOSProfile ] [-Start] [] ``` @@ -161,6 +166,22 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -CounterDigits + +The number of digits to use for the counter in VM names (e.g., 3 would result in "Prefix-001"). + +```yaml +Type: Int32 +Parameter Sets: MultipleVMs +Aliases: + +Required: False +Position: Named +Default value: 1 +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -VMID The VM ID. If not specified, the next available ID will be used. @@ -289,6 +310,38 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -AutomaticSMBIOS + +Whether to automatically generate SMBIOS values. + +```yaml +Type: SwitchParameter +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: False +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -SMBIOSProfile + +The manufacturer profile to use for SMBIOS values. Valid values are: Proxmox, Dell, HP, Lenovo, Microsoft, VMware, HyperV, VirtualBox, Random. + +```yaml +Type: String +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: Random +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -IPPool The IP pool to use for assigning an IP address. @@ -339,7 +392,10 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable - If the `-VMID` parameter is not specified, the next available ID will be used. - If the `-Start` parameter is specified, the VM will be started after creation. - If the `-IPPool` parameter is specified, an IP address will be assigned from the specified pool. -- When creating multiple VMs, the names will be in the format `{Prefix}{StartIndex + i}` where `i` is the index of the VM (0 to Count-1). +- When creating multiple VMs, the names will be in the format `{Prefix}{FormattedCounter}` where `FormattedCounter` is the counter (StartIndex + i) formatted with the specified number of digits. +- If the `-CounterDigits` parameter is specified, the counter will be padded with leading zeros to the specified number of digits (e.g., "Prefix-001"). +- If the `-AutomaticSMBIOS` parameter is specified, SMBIOS values will be automatically generated using the specified profile. +- The `-SMBIOSProfile` parameter specifies the manufacturer profile to use for SMBIOS values. Valid values are: Proxmox, Dell, HP, Lenovo, Microsoft, VMware, HyperV, VirtualBox, Random. ## Examples @@ -379,6 +435,24 @@ $vm = New-ProxmoxVMFromTemplate -Connection $connection -Node "pve1" -TemplateNa This example creates a new VM from the "Ubuntu-Template" template, assigns an IP address from the "Production" pool, and starts it. +### Example 5: Create multiple VMs with padded counters + +```powershell +$connection = Connect-ProxmoxServer -Server "proxmox.example.com" -Credential (Get-Credential) +$vms = New-ProxmoxVMFromTemplate -Connection $connection -Node "pve1" -TemplateName "Ubuntu-Template" -Prefix "web-" -Count 5 -CounterDigits 3 -Start +``` + +This example creates five new VMs from the "Ubuntu-Template" template with names "web-001", "web-002", "web-003", "web-004", and "web-005", and starts them. + +### Example 6: Create VMs with automatic SMBIOS settings + +```powershell +$connection = Connect-ProxmoxServer -Server "proxmox.example.com" -Credential (Get-Credential) +$vms = New-ProxmoxVMFromTemplate -Connection $connection -Node "pve1" -TemplateName "Windows-Template" -Prefix "win-" -Count 3 -AutomaticSMBIOS -SMBIOSProfile "Dell" -Start +``` + +This example creates three new VMs from the "Windows-Template" template with Dell SMBIOS settings, which can be useful for software licensing that's tied to hardware identifiers. + ## Related Links - [Connect-ProxmoxServer](Connect-ProxmoxServer.md) diff --git a/Documentation/examples/Advanced-MultipleVMsWithPaddedCounters.ps1 b/Documentation/examples/Advanced-MultipleVMsWithPaddedCounters.ps1 new file mode 100644 index 0000000..6388176 --- /dev/null +++ b/Documentation/examples/Advanced-MultipleVMsWithPaddedCounters.ps1 @@ -0,0 +1,56 @@ +# Advanced-MultipleVMsWithPaddedCounters.ps1 +# This script demonstrates how to create multiple VMs with padded counters and automatic SMBIOS settings. + +# Import the PSProxmox module +Import-Module PSProxmox + +# Connect to the Proxmox VE server +$credential = Get-Credential -Message "Enter your Proxmox VE credentials" +$connection = Connect-ProxmoxServer -Server "proxmox.example.com" -Credential $credential -Realm "pam" + +# Method 1: Create multiple VMs with padded counters +$vms = New-ProxmoxVMFromTemplate -Connection $connection -Node "pve1" -TemplateName "Ubuntu-Template" ` + -Prefix "web-" -Count 3 -CounterDigits 3 -Start + +# Display the VM information +Write-Host "Created VMs with padded counters:" +$vms | Select-Object Name, VMID, Status | Format-Table + +# Method 2: Create multiple VMs with padded counters and custom starting index +$vms = New-ProxmoxVMFromTemplate -Connection $connection -Node "pve1" -TemplateName "Ubuntu-Template" ` + -Prefix "db-" -Count 2 -StartIndex 10 -CounterDigits 4 -Memory 4096 -Cores 2 -Start + +# Display the VM information +Write-Host "Created VMs with padded counters and custom starting index:" +$vms | Select-Object Name, VMID, Status | Format-Table + +# Method 3: Create multiple VMs with automatic SMBIOS settings +$vms = New-ProxmoxVMFromTemplate -Connection $connection -Node "pve1" -TemplateName "Windows-Template" ` + -Prefix "win-" -Count 3 -CounterDigits 2 -AutomaticSMBIOS -SMBIOSProfile "Dell" -Start + +# Display the VM information +Write-Host "Created VMs with Dell SMBIOS settings:" +$vms | Select-Object Name, VMID, Status | Format-Table + +# Method 4: Create multiple VMs with different SMBIOS profiles +$manufacturers = @("Dell", "HP", "Lenovo", "Microsoft") +$count = $manufacturers.Count + +$vms = New-ProxmoxVMFromTemplate -Connection $connection -Node "pve1" -TemplateName "Windows-Template" ` + -Prefix "test-" -Count $count -CounterDigits 2 -Start + +# Update each VM with a different SMBIOS profile +for ($i = 0; $i -lt $count; $i++) { + $profile = $manufacturers[$i] + $vmid = $vms[$i].VMID + + Write-Host "Setting $profile SMBIOS profile for VM $vmid..." + Set-ProxmoxVMSMBIOS -Connection $connection -Node "pve1" -VMID $vmid -UseProfile -Profile $profile +} + +# Display the VM information +Write-Host "Created VMs with different SMBIOS profiles:" +$vms | Select-Object Name, VMID, Status | Format-Table + +# Disconnect from the server when done +Disconnect-ProxmoxServer -Connection $connection diff --git a/Module/PSProxmox.psd1 b/Module/PSProxmox.psd1 index a184db3..0095fb6 100644 --- a/Module/PSProxmox.psd1 +++ b/Module/PSProxmox.psd1 @@ -1,7 +1,7 @@ @{ RootModule = 'PSProxmox.psm1' NestedModules = @('bin\PSProxmox.dll') - ModuleVersion = '2025.05.07.1046' + ModuleVersion = '2025.05.07.1111' GUID = 'd24f0894-3d0c-4ef1-a41e-b273c3db86ad' Author = 'PSProxmox Team' CompanyName = 'PSProxmox' @@ -100,3 +100,6 @@ + + + diff --git a/PSProxmox/Cmdlets/NewProxmoxVMFromTemplateCmdlet.cs b/PSProxmox/Cmdlets/NewProxmoxVMFromTemplateCmdlet.cs index 12b9b73..5e1d414 100644 --- a/PSProxmox/Cmdlets/NewProxmoxVMFromTemplateCmdlet.cs +++ b/PSProxmox/Cmdlets/NewProxmoxVMFromTemplateCmdlet.cs @@ -67,6 +67,13 @@ namespace PSProxmox.Cmdlets [Parameter(Mandatory = false, ParameterSetName = "MultipleVMs")] public int StartIndex { get; set; } = 1; + /// + /// The number of digits to use for the counter in VM names (e.g., 3 would result in "Prefix-001"). + /// + [Parameter(Mandatory = false, ParameterSetName = "MultipleVMs")] + [ValidateRange(1, 10)] + public int CounterDigits { get; set; } = 1; + /// /// The VM ID. If not specified, the next available ID will be used. /// @@ -127,6 +134,19 @@ namespace PSProxmox.Cmdlets [Parameter(Mandatory = false)] public string Description { get; set; } + /// + /// Whether to automatically generate SMBIOS values. + /// + [Parameter(Mandatory = false)] + public SwitchParameter AutomaticSMBIOS { get; set; } + + /// + /// The manufacturer profile to use for SMBIOS values. Valid values are: Proxmox, Dell, HP, Lenovo, Microsoft, VMware, HyperV, VirtualBox, Random. + /// + [Parameter(Mandatory = false)] + [ValidateSet("Proxmox", "Dell", "HP", "Lenovo", "Microsoft", "VMware", "HyperV", "VirtualBox", "Random")] + public string SMBIOSProfile { get; set; } = "Random"; + /// /// Processes the cmdlet. /// @@ -165,7 +185,11 @@ namespace PSProxmox.Cmdlets var vms = new List(); for (int i = 0; i < Count; i++) { - string vmName = $"{Prefix}{StartIndex + i}"; + // Format the counter with the specified number of digits + string counterFormat = new string('0', CounterDigits); + string formattedCounter = (StartIndex + i).ToString(counterFormat); + string vmName = $"{Prefix}{formattedCounter}"; + var vm = CreateVMFromTemplate(client, template, vmName, null); vms.Add(vm); } @@ -231,14 +255,31 @@ namespace PSProxmox.Cmdlets vm.VMID = vmId.Value; // Update network settings if specified - if (!string.IsNullOrEmpty(NetworkModel) || !string.IsNullOrEmpty(NetworkBridge)) + if (!string.IsNullOrEmpty(NetworkModel) || !string.IsNullOrEmpty(NetworkBridge) || AutomaticSMBIOS.IsPresent) { - var networkParams = new Dictionary(); - string netModel = NetworkModel ?? "virtio"; - string netBridge = NetworkBridge ?? "vmbr0"; - networkParams["net0"] = $"{netModel},bridge={netBridge}"; + var configParams = new Dictionary(); - client.Put($"nodes/{Node}/qemu/{vmId.Value}/config", networkParams); + // Add network settings if specified + if (!string.IsNullOrEmpty(NetworkModel) || !string.IsNullOrEmpty(NetworkBridge)) + { + string netModel = NetworkModel ?? "virtio"; + string netBridge = NetworkBridge ?? "vmbr0"; + configParams["net0"] = $"{netModel},bridge={netBridge}"; + } + + // Add SMBIOS settings if requested + if (AutomaticSMBIOS.IsPresent) + { + var smbios = Models.ProxmoxVMSMBIOSProfile.GetProfile(SMBIOSProfile); + string smbiosString = smbios.ToProxmoxString(); + if (!string.IsNullOrEmpty(smbiosString)) + { + configParams["smbios"] = smbiosString; + WriteVerbose($"Using automatic SMBIOS settings with profile: {SMBIOSProfile}"); + } + } + + client.Put($"nodes/{Node}/qemu/{vmId.Value}/config", configParams); } // Assign IP if pool is specified @@ -262,7 +303,7 @@ namespace PSProxmox.Cmdlets { WriteVerbose($"Starting VM {vmName}"); client.Post($"nodes/{Node}/qemu/{vmId.Value}/status/start", null); - + // Refresh VM status vmResponse = client.Get($"nodes/{Node}/qemu/{vmId.Value}/status/current"); vm = JsonUtility.DeserializeResponse(vmResponse); diff --git a/PSProxmox/Models/ProxmoxVMBuilder.cs b/PSProxmox/Models/ProxmoxVMBuilder.cs index cad466c..4695dbf 100644 --- a/PSProxmox/Models/ProxmoxVMBuilder.cs +++ b/PSProxmox/Models/ProxmoxVMBuilder.cs @@ -74,7 +74,7 @@ namespace PSProxmox.Models /// /// Gets or sets the SMBIOS settings for the VM. /// - public ProxmoxVMSMBIOS SMBIOS { get; set; } = new ProxmoxVMSMBIOS(); + public PSProxmox.Models.ProxmoxVMSMBIOS SMBIOS { get; set; } = new PSProxmox.Models.ProxmoxVMSMBIOS(); /// /// Initializes a new instance of the class. @@ -417,9 +417,9 @@ namespace PSProxmox.Models /// /// The SMBIOS settings. /// The builder instance. - public ProxmoxVMBuilder WithSMBIOS(ProxmoxVMSMBIOS smbios) + public ProxmoxVMBuilder WithSMBIOS(PSProxmox.Models.ProxmoxVMSMBIOS smbios) { - SMBIOS = smbios ?? new ProxmoxVMSMBIOS(); + SMBIOS = smbios ?? new PSProxmox.Models.ProxmoxVMSMBIOS(); return this; } @@ -430,7 +430,7 @@ namespace PSProxmox.Models /// The builder instance. public ProxmoxVMBuilder WithSMBIOSProfile(string manufacturer) { - SMBIOS = ProxmoxVMSMBIOSProfile.GetProfile(manufacturer); + SMBIOS = PSProxmox.Models.ProxmoxVMSMBIOSProfile.GetProfile(manufacturer); return this; }