mirror of
https://github.com/Grace-Solutions/PSProxmox.git
synced 2026-08-30 16:59:53 +00:00
Add SMBIOS functionality with manufacturer profiles
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using PSProxmox.Models;
|
||||
using PSProxmox.Session;
|
||||
|
||||
namespace PSProxmox.Cmdlets
|
||||
{
|
||||
/// <summary>
|
||||
/// <para type="synopsis">Gets the SMBIOS settings for a Proxmox VM.</para>
|
||||
/// <para type="description">The Get-ProxmoxVMSMBIOS cmdlet retrieves the SMBIOS settings for a Proxmox VM.</para>
|
||||
/// <example>
|
||||
/// <para>Get SMBIOS settings for a VM</para>
|
||||
/// <code>Get-ProxmoxVMSMBIOS -Connection $connection -Node "pve1" -VMID 100</code>
|
||||
/// </example>
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Get, "ProxmoxVMSMBIOS")]
|
||||
[OutputType(typeof(ProxmoxVMSMBIOS))]
|
||||
public class GetProxmoxVMSMBIOSCmdlet : PSCmdlet
|
||||
{
|
||||
/// <summary>
|
||||
/// <para type="description">The connection to the Proxmox VE server.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = true, Position = 0)]
|
||||
public ProxmoxConnection Connection { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para type="description">The node where the VM is located.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = true, Position = 1)]
|
||||
public string Node { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para type="description">The ID of the VM.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = true, Position = 2)]
|
||||
public int VMID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para type="description">Return the raw JSON response.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = false)]
|
||||
public SwitchParameter RawJson { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Processes the cmdlet.
|
||||
/// </summary>
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
try
|
||||
{
|
||||
var client = new ProxmoxApiClient(Connection, this);
|
||||
|
||||
// Get the VM configuration
|
||||
string response = client.Get($"nodes/{Node}/qemu/{VMID}/config");
|
||||
var configData = JsonUtility.DeserializeResponse<JObject>(response);
|
||||
|
||||
if (RawJson.IsPresent)
|
||||
{
|
||||
WriteObject(response);
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract SMBIOS settings
|
||||
var smbios = new ProxmoxVMSMBIOS();
|
||||
if (configData.TryGetValue("data", out JToken dataToken) &&
|
||||
dataToken is JObject data &&
|
||||
data.TryGetValue("smbios", out JToken smbiosToken))
|
||||
{
|
||||
string smbiosString = smbiosToken.ToString();
|
||||
smbios = ProxmoxVMSMBIOS.FromProxmoxString(smbiosString);
|
||||
}
|
||||
|
||||
WriteObject(smbios);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
WriteError(new ErrorRecord(ex, "GetProxmoxVMSMBIOSError", ErrorCategory.OperationStopped, Connection));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -92,6 +92,55 @@ namespace PSProxmox.Cmdlets
|
||||
[Parameter(Mandatory = false)]
|
||||
public string IPPool { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para type="description">The SMBIOS manufacturer information.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = false)]
|
||||
public string SMBIOSManufacturer { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para type="description">The SMBIOS product information.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = false)]
|
||||
public string SMBIOSProduct { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para type="description">The SMBIOS version information.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = false)]
|
||||
public string SMBIOSVersion { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para type="description">The SMBIOS serial number.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = false)]
|
||||
public string SMBIOSSerial { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para type="description">The SMBIOS family information.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = false)]
|
||||
public string SMBIOSFamily { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para type="description">The SMBIOS UUID.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = false)]
|
||||
public string SMBIOSUUID { 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, VMware, HyperV, VirtualBox, Random.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = false)]
|
||||
[ValidateSet("Proxmox", "Dell", "HP", "Lenovo", "VMware", "HyperV", "VirtualBox", "Random")]
|
||||
public string SMBIOSProfile { get; set; } = "Random";
|
||||
|
||||
/// <summary>
|
||||
/// Processes the cmdlet.
|
||||
/// </summary>
|
||||
@@ -129,6 +178,47 @@ namespace PSProxmox.Cmdlets
|
||||
builder.WithTags(Tags);
|
||||
}
|
||||
|
||||
// Set SMBIOS settings based on parameters
|
||||
if (AutomaticSMBIOS.IsPresent)
|
||||
{
|
||||
// Use the specified profile or Random if not specified
|
||||
builder.WithSMBIOSProfile(SMBIOSProfile);
|
||||
WriteVerbose($"Using automatic SMBIOS settings with profile: {SMBIOSProfile}");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Set individual SMBIOS settings if provided
|
||||
if (!string.IsNullOrEmpty(SMBIOSManufacturer))
|
||||
{
|
||||
builder.WithSMBIOSManufacturer(SMBIOSManufacturer);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(SMBIOSProduct))
|
||||
{
|
||||
builder.WithSMBIOSProduct(SMBIOSProduct);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(SMBIOSVersion))
|
||||
{
|
||||
builder.WithSMBIOSVersion(SMBIOSVersion);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(SMBIOSSerial))
|
||||
{
|
||||
builder.WithSMBIOSSerial(SMBIOSSerial);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(SMBIOSFamily))
|
||||
{
|
||||
builder.WithSMBIOSFamily(SMBIOSFamily);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(SMBIOSUUID))
|
||||
{
|
||||
builder.WithSMBIOSUUID(SMBIOSUUID);
|
||||
}
|
||||
}
|
||||
|
||||
WriteObject(builder);
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
@@ -112,6 +112,19 @@ namespace PSProxmox.Cmdlets
|
||||
[Parameter(Mandatory = false, ParameterSetName = "Direct")]
|
||||
public string IPPool { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para type="description">Whether to automatically generate SMBIOS values.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = false, ParameterSetName = "Direct")]
|
||||
public SwitchParameter AutomaticSMBIOS { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para type="description">The manufacturer profile to use for SMBIOS values. Valid values are: Proxmox, Dell, HP, Lenovo, VMware, HyperV, VirtualBox, Random.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = false, ParameterSetName = "Direct")]
|
||||
[ValidateSet("Proxmox", "Dell", "HP", "Lenovo", "VMware", "HyperV", "VirtualBox", "Random")]
|
||||
public string SMBIOSProfile { get; set; } = "Random";
|
||||
|
||||
/// <summary>
|
||||
/// Processes the cmdlet.
|
||||
/// </summary>
|
||||
@@ -180,6 +193,18 @@ namespace PSProxmox.Cmdlets
|
||||
// Add disk
|
||||
parameters["ide0"] = $"{Storage}:{DiskSize}";
|
||||
|
||||
// Add SMBIOS settings if requested
|
||||
if (AutomaticSMBIOS.IsPresent)
|
||||
{
|
||||
var smbios = ProxmoxVMSMBIOSProfile.GetProfile(SMBIOSProfile);
|
||||
string smbiosString = smbios.ToProxmoxString();
|
||||
if (!string.IsNullOrEmpty(smbiosString))
|
||||
{
|
||||
parameters["smbios"] = smbiosString;
|
||||
WriteVerbose($"Using automatic SMBIOS settings with profile: {SMBIOSProfile}");
|
||||
}
|
||||
}
|
||||
|
||||
vmid = VMID.Value;
|
||||
vmName = Name;
|
||||
ipPool = IPPool;
|
||||
|
||||
@@ -0,0 +1,177 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Management.Automation;
|
||||
using PSProxmox.Models;
|
||||
using PSProxmox.Session;
|
||||
|
||||
namespace PSProxmox.Cmdlets
|
||||
{
|
||||
/// <summary>
|
||||
/// <para type="synopsis">Sets the SMBIOS settings for a Proxmox VM.</para>
|
||||
/// <para type="description">The Set-ProxmoxVMSMBIOS cmdlet sets the SMBIOS settings for a Proxmox VM.</para>
|
||||
/// <example>
|
||||
/// <para>Set SMBIOS settings for a VM</para>
|
||||
/// <code>Set-ProxmoxVMSMBIOS -Connection $connection -Node "pve1" -VMID 100 -Manufacturer "Dell Inc." -Product "PowerEdge R740" -Serial "ABC123"</code>
|
||||
/// </example>
|
||||
/// <example>
|
||||
/// <para>Set SMBIOS settings using a SMBIOS object</para>
|
||||
/// <code>$smbios = New-Object PSProxmox.Models.ProxmoxVMSMBIOS
|
||||
/// $smbios.Manufacturer = "Dell Inc."
|
||||
/// $smbios.Product = "PowerEdge R740"
|
||||
/// $smbios.Serial = "ABC123"
|
||||
/// Set-ProxmoxVMSMBIOS -Connection $connection -Node "pve1" -VMID 100 -SMBIOS $smbios</code>
|
||||
/// </example>
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.Set, "ProxmoxVMSMBIOS", SupportsShouldProcess = true)]
|
||||
[OutputType(typeof(ProxmoxVMSMBIOS))]
|
||||
public class SetProxmoxVMSMBIOSCmdlet : PSCmdlet
|
||||
{
|
||||
/// <summary>
|
||||
/// <para type="description">The connection to the Proxmox VE server.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = true, Position = 0)]
|
||||
public ProxmoxConnection Connection { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para type="description">The node where the VM is located.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = true, Position = 1)]
|
||||
public string Node { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para type="description">The ID of the VM.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = true, Position = 2)]
|
||||
public int VMID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para type="description">The SMBIOS settings object.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = false, ParameterSetName = "SMBIOSObject")]
|
||||
public ProxmoxVMSMBIOS SMBIOS { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para type="description">The manufacturer information.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = false, ParameterSetName = "Individual")]
|
||||
public string Manufacturer { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para type="description">The product information.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = false, ParameterSetName = "Individual")]
|
||||
public string Product { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para type="description">The version information.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = false, ParameterSetName = "Individual")]
|
||||
public string Version { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para type="description">The serial number.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = false, ParameterSetName = "Individual")]
|
||||
public string Serial { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para type="description">The system family.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = false, ParameterSetName = "Individual")]
|
||||
public string Family { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para type="description">The system UUID.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = false, ParameterSetName = "Individual")]
|
||||
public string UUID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para type="description">Return the updated SMBIOS settings.</para>
|
||||
/// </summary>
|
||||
[Parameter(Mandatory = false)]
|
||||
public SwitchParameter PassThru { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Processes the cmdlet.
|
||||
/// </summary>
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
try
|
||||
{
|
||||
var client = new ProxmoxApiClient(Connection, this);
|
||||
|
||||
// Create SMBIOS settings
|
||||
ProxmoxVMSMBIOS smbiosSettings;
|
||||
if (ParameterSetName == "SMBIOSObject")
|
||||
{
|
||||
smbiosSettings = SMBIOS ?? new ProxmoxVMSMBIOS();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Get current settings first
|
||||
string response = client.Get($"nodes/{Node}/qemu/{VMID}/config");
|
||||
var configData = JsonUtility.DeserializeResponse<dynamic>(response);
|
||||
|
||||
string currentSmbiosString = configData.data.smbios;
|
||||
smbiosSettings = string.IsNullOrEmpty(currentSmbiosString)
|
||||
? new ProxmoxVMSMBIOS()
|
||||
: ProxmoxVMSMBIOS.FromProxmoxString(currentSmbiosString);
|
||||
|
||||
// Update with provided values
|
||||
if (!string.IsNullOrEmpty(Manufacturer))
|
||||
smbiosSettings.Manufacturer = Manufacturer;
|
||||
|
||||
if (!string.IsNullOrEmpty(Product))
|
||||
smbiosSettings.Product = Product;
|
||||
|
||||
if (!string.IsNullOrEmpty(Version))
|
||||
smbiosSettings.Version = Version;
|
||||
|
||||
if (!string.IsNullOrEmpty(Serial))
|
||||
smbiosSettings.Serial = Serial;
|
||||
|
||||
if (!string.IsNullOrEmpty(Family))
|
||||
smbiosSettings.Family = Family;
|
||||
|
||||
if (!string.IsNullOrEmpty(UUID))
|
||||
smbiosSettings.UUID = UUID;
|
||||
}
|
||||
|
||||
// Convert to Proxmox format
|
||||
string smbiosString = smbiosSettings.ToProxmoxString();
|
||||
|
||||
if (string.IsNullOrEmpty(smbiosString))
|
||||
{
|
||||
WriteWarning("No SMBIOS settings specified. No changes will be made.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Confirm the operation
|
||||
if (!ShouldProcess($"VM {VMID} on node {Node}", $"Set SMBIOS settings to {smbiosString}"))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Update the VM configuration
|
||||
var parameters = new Dictionary<string, string>
|
||||
{
|
||||
["smbios"] = smbiosString
|
||||
};
|
||||
|
||||
client.Put($"nodes/{Node}/qemu/{VMID}/config", parameters);
|
||||
WriteVerbose($"SMBIOS settings updated for VM {VMID} on node {Node}");
|
||||
|
||||
// Return the updated settings if requested
|
||||
if (PassThru.IsPresent)
|
||||
{
|
||||
WriteObject(smbiosSettings);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
WriteError(new ErrorRecord(ex, "SetProxmoxVMSMBIOSError", ErrorCategory.OperationStopped, Connection));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -71,6 +71,11 @@ namespace PSProxmox.Models
|
||||
/// </summary>
|
||||
public string IPPool { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the SMBIOS settings for the VM.
|
||||
/// </summary>
|
||||
public ProxmoxVMSMBIOS SMBIOS { get; set; } = new ProxmoxVMSMBIOS();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ProxmoxVMBuilder"/> class.
|
||||
/// </summary>
|
||||
@@ -341,6 +346,94 @@ namespace PSProxmox.Models
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the SMBIOS manufacturer for the VM.
|
||||
/// </summary>
|
||||
/// <param name="manufacturer">The manufacturer name.</param>
|
||||
/// <returns>The builder instance.</returns>
|
||||
public ProxmoxVMBuilder WithSMBIOSManufacturer(string manufacturer)
|
||||
{
|
||||
SMBIOS.Manufacturer = manufacturer;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the SMBIOS product name for the VM.
|
||||
/// </summary>
|
||||
/// <param name="product">The product name.</param>
|
||||
/// <returns>The builder instance.</returns>
|
||||
public ProxmoxVMBuilder WithSMBIOSProduct(string product)
|
||||
{
|
||||
SMBIOS.Product = product;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the SMBIOS version for the VM.
|
||||
/// </summary>
|
||||
/// <param name="version">The version.</param>
|
||||
/// <returns>The builder instance.</returns>
|
||||
public ProxmoxVMBuilder WithSMBIOSVersion(string version)
|
||||
{
|
||||
SMBIOS.Version = version;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the SMBIOS serial number for the VM.
|
||||
/// </summary>
|
||||
/// <param name="serial">The serial number.</param>
|
||||
/// <returns>The builder instance.</returns>
|
||||
public ProxmoxVMBuilder WithSMBIOSSerial(string serial)
|
||||
{
|
||||
SMBIOS.Serial = serial;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the SMBIOS family for the VM.
|
||||
/// </summary>
|
||||
/// <param name="family">The family.</param>
|
||||
/// <returns>The builder instance.</returns>
|
||||
public ProxmoxVMBuilder WithSMBIOSFamily(string family)
|
||||
{
|
||||
SMBIOS.Family = family;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the SMBIOS UUID for the VM.
|
||||
/// </summary>
|
||||
/// <param name="uuid">The UUID.</param>
|
||||
/// <returns>The builder instance.</returns>
|
||||
public ProxmoxVMBuilder WithSMBIOSUUID(string uuid)
|
||||
{
|
||||
SMBIOS.UUID = uuid;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets all SMBIOS settings for the VM at once.
|
||||
/// </summary>
|
||||
/// <param name="smbios">The SMBIOS settings.</param>
|
||||
/// <returns>The builder instance.</returns>
|
||||
public ProxmoxVMBuilder WithSMBIOS(ProxmoxVMSMBIOS smbios)
|
||||
{
|
||||
SMBIOS = smbios ?? new ProxmoxVMSMBIOS();
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets SMBIOS settings based on a manufacturer profile.
|
||||
/// </summary>
|
||||
/// <param name="manufacturer">The manufacturer profile to use.</param>
|
||||
/// <returns>The builder instance.</returns>
|
||||
public ProxmoxVMBuilder WithSMBIOSProfile(string manufacturer)
|
||||
{
|
||||
SMBIOS = ProxmoxVMSMBIOSProfile.GetProfile(manufacturer);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds the VM configuration parameters.
|
||||
/// </summary>
|
||||
@@ -366,6 +459,13 @@ namespace PSProxmox.Models
|
||||
parameters["tags"] = Tags;
|
||||
}
|
||||
|
||||
// Add SMBIOS settings if any are specified
|
||||
string smbiosString = SMBIOS.ToProxmoxString();
|
||||
if (!string.IsNullOrEmpty(smbiosString))
|
||||
{
|
||||
parameters["smbios"] = smbiosString;
|
||||
}
|
||||
|
||||
// Add network interfaces
|
||||
for (int i = 0; i < _networkInterfaces.Count; i++)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace PSProxmox.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents SMBIOS settings for a Proxmox VM.
|
||||
/// </summary>
|
||||
public class ProxmoxVMSMBIOS
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the manufacturer information.
|
||||
/// </summary>
|
||||
[JsonProperty("manufacturer")]
|
||||
public string Manufacturer { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the product information.
|
||||
/// </summary>
|
||||
[JsonProperty("product")]
|
||||
public string Product { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the version information.
|
||||
/// </summary>
|
||||
[JsonProperty("version")]
|
||||
public string Version { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the serial number.
|
||||
/// </summary>
|
||||
[JsonProperty("serial")]
|
||||
public string Serial { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the system family.
|
||||
/// </summary>
|
||||
[JsonProperty("family")]
|
||||
public string Family { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the system UUID.
|
||||
/// </summary>
|
||||
[JsonProperty("uuid")]
|
||||
public string UUID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Converts the SMBIOS settings to a Proxmox API compatible string.
|
||||
/// </summary>
|
||||
/// <returns>A string representation of the SMBIOS settings.</returns>
|
||||
public string ToProxmoxString()
|
||||
{
|
||||
var parts = new System.Collections.Generic.List<string>();
|
||||
|
||||
if (!string.IsNullOrEmpty(Manufacturer))
|
||||
parts.Add($"manufacturer={Manufacturer}");
|
||||
|
||||
if (!string.IsNullOrEmpty(Product))
|
||||
parts.Add($"product={Product}");
|
||||
|
||||
if (!string.IsNullOrEmpty(Version))
|
||||
parts.Add($"version={Version}");
|
||||
|
||||
if (!string.IsNullOrEmpty(Serial))
|
||||
parts.Add($"serial={Serial}");
|
||||
|
||||
if (!string.IsNullOrEmpty(Family))
|
||||
parts.Add($"family={Family}");
|
||||
|
||||
if (!string.IsNullOrEmpty(UUID))
|
||||
parts.Add($"uuid={UUID}");
|
||||
|
||||
return string.Join(",", parts);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses a Proxmox API SMBIOS string into a ProxmoxVMSMBIOS object.
|
||||
/// </summary>
|
||||
/// <param name="smbiosString">The SMBIOS string from the Proxmox API.</param>
|
||||
/// <returns>A ProxmoxVMSMBIOS object.</returns>
|
||||
public static ProxmoxVMSMBIOS FromProxmoxString(string smbiosString)
|
||||
{
|
||||
if (string.IsNullOrEmpty(smbiosString))
|
||||
return new ProxmoxVMSMBIOS();
|
||||
|
||||
var result = new ProxmoxVMSMBIOS();
|
||||
var parts = smbiosString.Split(',');
|
||||
|
||||
foreach (var part in parts)
|
||||
{
|
||||
var keyValue = part.Split('=');
|
||||
if (keyValue.Length != 2)
|
||||
continue;
|
||||
|
||||
var key = keyValue[0].Trim();
|
||||
var value = keyValue[1].Trim();
|
||||
|
||||
switch (key.ToLowerInvariant())
|
||||
{
|
||||
case "manufacturer":
|
||||
result.Manufacturer = value;
|
||||
break;
|
||||
case "product":
|
||||
result.Product = value;
|
||||
break;
|
||||
case "version":
|
||||
result.Version = value;
|
||||
break;
|
||||
case "serial":
|
||||
result.Serial = value;
|
||||
break;
|
||||
case "family":
|
||||
result.Family = value;
|
||||
break;
|
||||
case "uuid":
|
||||
result.UUID = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace PSProxmox.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a manufacturer profile for SMBIOS settings.
|
||||
/// </summary>
|
||||
public class ProxmoxVMSMBIOSProfile
|
||||
{
|
||||
private static readonly Random _random = new Random();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the available manufacturer profiles.
|
||||
/// </summary>
|
||||
public static readonly string[] AvailableProfiles = new string[]
|
||||
{
|
||||
"Proxmox",
|
||||
"Dell",
|
||||
"HP",
|
||||
"Lenovo",
|
||||
"VMware",
|
||||
"HyperV",
|
||||
"VirtualBox",
|
||||
"Random"
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Gets a SMBIOS profile for the specified manufacturer.
|
||||
/// </summary>
|
||||
/// <param name="manufacturer">The manufacturer profile to use.</param>
|
||||
/// <returns>A ProxmoxVMSMBIOS object with the manufacturer profile settings.</returns>
|
||||
public static ProxmoxVMSMBIOS GetProfile(string manufacturer)
|
||||
{
|
||||
if (string.IsNullOrEmpty(manufacturer) || manufacturer.Equals("Random", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// Choose a random manufacturer (excluding "Random")
|
||||
var profiles = AvailableProfiles.Where(p => !p.Equals("Random", StringComparison.OrdinalIgnoreCase)).ToArray();
|
||||
manufacturer = profiles[_random.Next(profiles.Length)];
|
||||
}
|
||||
|
||||
switch (manufacturer.ToLowerInvariant())
|
||||
{
|
||||
case "proxmox":
|
||||
return GetProxmoxProfile();
|
||||
case "dell":
|
||||
return GetDellProfile();
|
||||
case "hp":
|
||||
return GetHPProfile();
|
||||
case "lenovo":
|
||||
return GetLenovoProfile();
|
||||
case "vmware":
|
||||
return GetVMwareProfile();
|
||||
case "hyperv":
|
||||
return GetHyperVProfile();
|
||||
case "virtualbox":
|
||||
return GetVirtualBoxProfile();
|
||||
default:
|
||||
throw new ArgumentException($"Unknown manufacturer profile: {manufacturer}");
|
||||
}
|
||||
}
|
||||
|
||||
private static ProxmoxVMSMBIOS GetProxmoxProfile()
|
||||
{
|
||||
return new ProxmoxVMSMBIOS
|
||||
{
|
||||
Manufacturer = "Proxmox",
|
||||
Product = $"Virtual Environment {_random.Next(1, 8)}.{_random.Next(0, 10)}",
|
||||
Version = $"{_random.Next(1, 10)}.{_random.Next(0, 10)}",
|
||||
Serial = GenerateRandomSerial(10),
|
||||
UUID = GenerateRandomUUID(),
|
||||
Family = "Virtual Machine"
|
||||
};
|
||||
}
|
||||
|
||||
private static ProxmoxVMSMBIOS GetDellProfile()
|
||||
{
|
||||
var models = new string[]
|
||||
{
|
||||
"PowerEdge R640",
|
||||
"PowerEdge R740",
|
||||
"PowerEdge R750",
|
||||
"PowerEdge R840",
|
||||
"PowerEdge R940",
|
||||
"PowerEdge T640"
|
||||
};
|
||||
|
||||
return new ProxmoxVMSMBIOS
|
||||
{
|
||||
Manufacturer = "Dell Inc.",
|
||||
Product = models[_random.Next(models.Length)],
|
||||
Version = $"{_random.Next(1, 5)}.{_random.Next(0, 10)}",
|
||||
Serial = $"{GenerateRandomString(5, true)}{_random.Next(10000, 99999)}",
|
||||
UUID = GenerateRandomUUID(),
|
||||
Family = "PowerEdge"
|
||||
};
|
||||
}
|
||||
|
||||
private static ProxmoxVMSMBIOS GetHPProfile()
|
||||
{
|
||||
var models = new string[]
|
||||
{
|
||||
"ProLiant DL360 Gen10",
|
||||
"ProLiant DL380 Gen10",
|
||||
"ProLiant DL560 Gen10",
|
||||
"ProLiant ML350 Gen10",
|
||||
"ProLiant BL460c Gen10"
|
||||
};
|
||||
|
||||
return new ProxmoxVMSMBIOS
|
||||
{
|
||||
Manufacturer = "HP",
|
||||
Product = models[_random.Next(models.Length)],
|
||||
Version = $"U{_random.Next(10, 50)}",
|
||||
Serial = $"USE{_random.Next(100000, 999999)}",
|
||||
UUID = GenerateRandomUUID(),
|
||||
Family = "ProLiant"
|
||||
};
|
||||
}
|
||||
|
||||
private static ProxmoxVMSMBIOS GetLenovoProfile()
|
||||
{
|
||||
var models = new string[]
|
||||
{
|
||||
"ThinkSystem SR650",
|
||||
"ThinkSystem SR630",
|
||||
"ThinkSystem SR850",
|
||||
"ThinkSystem SR950",
|
||||
"ThinkSystem ST550"
|
||||
};
|
||||
|
||||
return new ProxmoxVMSMBIOS
|
||||
{
|
||||
Manufacturer = "Lenovo",
|
||||
Product = models[_random.Next(models.Length)],
|
||||
Version = $"ThinkSystem {_random.Next(1, 5)}",
|
||||
Serial = $"{GenerateRandomString(4, true)}{GenerateRandomString(6, false)}",
|
||||
UUID = GenerateRandomUUID(),
|
||||
Family = "ThinkSystem"
|
||||
};
|
||||
}
|
||||
|
||||
private static ProxmoxVMSMBIOS GetVMwareProfile()
|
||||
{
|
||||
return new ProxmoxVMSMBIOS
|
||||
{
|
||||
Manufacturer = "VMware, Inc.",
|
||||
Product = $"VMware Virtual Platform {_random.Next(1, 8)}",
|
||||
Version = $"VMware-{_random.Next(1, 10)}.{_random.Next(0, 10)}",
|
||||
Serial = GenerateRandomSerial(10),
|
||||
UUID = GenerateRandomUUID(),
|
||||
Family = "Virtual Machine"
|
||||
};
|
||||
}
|
||||
|
||||
private static ProxmoxVMSMBIOS GetHyperVProfile()
|
||||
{
|
||||
return new ProxmoxVMSMBIOS
|
||||
{
|
||||
Manufacturer = "Microsoft Corporation",
|
||||
Product = $"Hyper-V {_random.Next(2012, 2023)}",
|
||||
Version = $"{_random.Next(1, 10)}.{_random.Next(0, 10)}",
|
||||
Serial = $"MS-{GenerateRandomString(8, false)}",
|
||||
UUID = GenerateRandomUUID(),
|
||||
Family = "Virtual Machine"
|
||||
};
|
||||
}
|
||||
|
||||
private static ProxmoxVMSMBIOS GetVirtualBoxProfile()
|
||||
{
|
||||
return new ProxmoxVMSMBIOS
|
||||
{
|
||||
Manufacturer = "Oracle Corporation",
|
||||
Product = $"VirtualBox {_random.Next(5, 8)}.{_random.Next(0, 10)}",
|
||||
Version = $"{_random.Next(1, 10)}.{_random.Next(0, 10)}",
|
||||
Serial = $"VB-{GenerateRandomString(8, false)}",
|
||||
UUID = GenerateRandomUUID(),
|
||||
Family = "Virtual Machine"
|
||||
};
|
||||
}
|
||||
|
||||
private static string GenerateRandomSerial(int length)
|
||||
{
|
||||
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
return new string(Enumerable.Repeat(chars, length)
|
||||
.Select(s => s[_random.Next(s.Length)]).ToArray());
|
||||
}
|
||||
|
||||
private static string GenerateRandomString(int length, bool uppercase)
|
||||
{
|
||||
const string upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
const string lowerChars = "abcdefghijklmnopqrstuvwxyz";
|
||||
const string digits = "0123456789";
|
||||
|
||||
string chars = uppercase ? upperChars : lowerChars + digits;
|
||||
|
||||
return new string(Enumerable.Repeat(chars, length)
|
||||
.Select(s => s[_random.Next(s.Length)]).ToArray());
|
||||
}
|
||||
|
||||
private static string GenerateRandomUUID()
|
||||
{
|
||||
return Guid.NewGuid().ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user