Created
August 30, 2023 03:58
-
-
Save keithga/ca68ca7968eddf56079db5e2f7869533 to your computer and use it in GitHub Desktop.
Create VM with Custom AssetTag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function new-MyVM { | |
[cmdletbinding()] | |
param( | |
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,Position=0)] | |
$name, | |
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,Position=1)] | |
$AssetTag, | |
$Switch = ( Get-VMSwitch -SwitchType External | Select-object -first 1 -ExpandProperty Name ), | |
$DVDPath = 'C:\iso\ipxe\IPXE.NET.23.03.07.iso' | |
) | |
$ErrorActionPreference = 'stop' | |
# Generate a Unique "Name" | |
for ( $i = '' ; get-VM "$($name)$($i)" -EA SilentlyContinue ; $i = "." + ( 1 + $i.trim('.') ) ) { } | |
$Path = join-path ( (get-vmhost).VirtualHardDiskPath ) "$($name)$($i)-$( [guid]::newguid() ).vhdx" | |
write-verbose "Create VM: $($name)$($i) $AssetTag $path" | |
$VM = new-vm -name "$($name)$($i)" -SwitchName $Switch -Generation 2 -NewVHDSizeBytes 120gb -NewVHDPath $path | |
set-vm -VM $VM -ProcessorCount 4 -AutomaticCheckpointsEnabled $False | |
Set-VMFirmware -VM $VM -EnableSecureBoot Off | |
Set-VMMemory -VM $VM -DynamicMemoryEnabled $True -MinimumBytes 1gb -MaximumBytes 8gb -StartupBytes 2GB | |
Set-VMKeyProtector -VM $VM -NewLocalKeyProtector | |
Enable-VMTPM -vm $VM | |
if ( $AssetTag ) { | |
$VSGSD = get-wmiobject -Namespace "Root\virtualization\v2" -class Msvm_VirtualSystemSettingData | | |
Where-Object { ( $_.VirtualSystemType -eq 'Microsoft:Hyper-V:System:Realized' ) -and ( $_.VirtualSystemIdentifier -eq $VM.Id ) } | |
$VSGSD.ChassisAssetTag = $AssetTag | |
$arguments = @($VM , $VSGSD.psbase.GetText([System.Management.TextFormat]::WmiDtd20),$null,$null) | |
$VSMgtSvc = Get-WmiObject -NameSpace "root\virtualization\v2" -Class "MsVM_virtualSystemManagementService" | |
$VSMgtSvc.psbase.InvokeMethod("ModifySystemSettings", $arguments) | out-null | |
} | |
if ( $DVDPath ) { Add-VMDvdDrive -VM $VM -path $DVDPath } | |
# Set boot order Disk, DVD, Network, | |
Set-VMFirmware -VM $VM -BootOrder (Get-VMFirmware -vm $VM | % BootOrder | Sort-Object Device -Descending | sort-Object boottype) | |
# snapshot for repeated installs. | |
Checkpoint-VM -vm $VM -SnapshotName "Clean" | |
Start-VM -VM $VM | |
} | |
########################################### | |
$ErrorActionPreference = 'stop' | |
$VerbosePreference = 'continue' | |
@( | |
[pscustomobject]@{ Name = 'iPXE-Win10-Prod'; AssetTag = '3992-462b-f644-8b6a-835b-ba6c-01' } | |
[pscustomobject]@{ Name = 'iPXE-Win10-PreProd'; AssetTag = '3992-462b-f644-8b6a-835b-ba6c-02' } | |
[pscustomobject]@{ Name = 'iPXE-Win11-Prod'; AssetTag = '3992-462b-f644-8b6a-835b-ba6c-03' } | |
[pscustomobject]@{ Name = 'iPXE-Win11-PreProd'; AssetTag = '3992-462b-f644-8b6a-835b-ba6c-04' } | |
) | Out-GridView -OutputMode Multiple | new-MyVM |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment