Add padded counter and AutoSMBIOS parameters to New-ProxmoxVMFromTemplate

This commit is contained in:
Alphaeus Mote
2025-05-07 11:12:19 -04:00
parent fce40d9351
commit f906f41364
5 changed files with 188 additions and 14 deletions
@@ -19,6 +19,8 @@ New-ProxmoxVMFromTemplate
[-NetworkBridge <String>]
[-Description <String>]
[-IPPool <String>]
[-AutomaticSMBIOS]
[-SMBIOSProfile <String>]
[-Start]
[<CommonParameters>]
```
@@ -31,6 +33,7 @@ New-ProxmoxVMFromTemplate
-Prefix <String>
-Count <Int32>
[-StartIndex <Int32>]
[-CounterDigits <Int32>]
[-Memory <Int32>]
[-Cores <Int32>]
[-DiskSize <Int32>]
@@ -39,6 +42,8 @@ New-ProxmoxVMFromTemplate
[-NetworkBridge <String>]
[-Description <String>]
[-IPPool <String>]
[-AutomaticSMBIOS]
[-SMBIOSProfile <String>]
[-Start]
[<CommonParameters>]
```
@@ -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)
@@ -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
+4 -1
View File
@@ -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 @@
@@ -67,6 +67,13 @@ namespace PSProxmox.Cmdlets
[Parameter(Mandatory = false, ParameterSetName = "MultipleVMs")]
public int StartIndex { get; set; } = 1;
/// <summary>
/// <para type="description">The number of digits to use for the counter in VM names (e.g., 3 would result in "Prefix-001").</para>
/// </summary>
[Parameter(Mandatory = false, ParameterSetName = "MultipleVMs")]
[ValidateRange(1, 10)]
public int CounterDigits { get; set; } = 1;
/// <summary>
/// <para type="description">The VM ID. If not specified, the next available ID will be used.</para>
/// </summary>
@@ -127,6 +134,19 @@ namespace PSProxmox.Cmdlets
[Parameter(Mandatory = false)]
public string Description { get; set; }
/// <summary>
/// <para type="description">Whether to automatically generate SMBIOS values.</para>
/// </summary>
[Parameter(Mandatory = false)]
public SwitchParameter AutomaticSMBIOS { get; set; }
/// <summary>
/// <para type="description">The manufacturer profile to use for SMBIOS values. Valid values are: Proxmox, Dell, HP, Lenovo, Microsoft, VMware, HyperV, VirtualBox, Random.</para>
/// </summary>
[Parameter(Mandatory = false)]
[ValidateSet("Proxmox", "Dell", "HP", "Lenovo", "Microsoft", "VMware", "HyperV", "VirtualBox", "Random")]
public string SMBIOSProfile { get; set; } = "Random";
/// <summary>
/// Processes the cmdlet.
/// </summary>
@@ -165,7 +185,11 @@ namespace PSProxmox.Cmdlets
var vms = new List<ProxmoxVM>();
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, string>();
string netModel = NetworkModel ?? "virtio";
string netBridge = NetworkBridge ?? "vmbr0";
networkParams["net0"] = $"{netModel},bridge={netBridge}";
var configParams = new Dictionary<string, string>();
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<ProxmoxVM>(vmResponse);
+4 -4
View File
@@ -74,7 +74,7 @@ namespace PSProxmox.Models
/// <summary>
/// Gets or sets the SMBIOS settings for the VM.
/// </summary>
public ProxmoxVMSMBIOS SMBIOS { get; set; } = new ProxmoxVMSMBIOS();
public PSProxmox.Models.ProxmoxVMSMBIOS SMBIOS { get; set; } = new PSProxmox.Models.ProxmoxVMSMBIOS();
/// <summary>
/// Initializes a new instance of the <see cref="ProxmoxVMBuilder"/> class.
@@ -417,9 +417,9 @@ namespace PSProxmox.Models
/// </summary>
/// <param name="smbios">The SMBIOS settings.</param>
/// <returns>The builder instance.</returns>
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
/// <returns>The builder instance.</returns>
public ProxmoxVMBuilder WithSMBIOSProfile(string manufacturer)
{
SMBIOS = ProxmoxVMSMBIOSProfile.GetProfile(manufacturer);
SMBIOS = PSProxmox.Models.ProxmoxVMSMBIOSProfile.GetProfile(manufacturer);
return this;
}