• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1979
  • Last Modified:

Powershell Script to Create Virtual Machines in VMM keeps creating a new hardware profiles and templates.

I've created the following script to ask pass a few parameters and the will create VM's the script works great the first time but if you run it a 2nd time it encounters errors because it tries to use the same Hardware Profile and Template name that was already created the first time the script ran.

How can I get around this.  The code is below:

# ------------------------------------------------------------------------------
# Create Virtual Machine From SEA-VSCVMM - EDIT:  9/19/13 
# ------------------------------------------------------------------------------
# Script generated on Thursday, September 19, 2013 10:40:55 PM by Virtual Machine Manager
# For additional help on cmdlet usage, type get-help <cmdlet name>
# ------------------------------------------------------------------------------
#What should the VM be called

$Name = Read-Host "Enter the Virtual Machine name (Press [Enter] to choose 
if ($Name -eq ""){$Name="Srv01"} ; if ($Name -eq $NULL){$Name="Srv01"}

#What should the Computer Name Be?
$CPUName = Read-Host "Enter the Computer Name (Default is None)"
if ($CPUName -eq ""){$CPUName="*"} ; if ($CPUName -eq $NULL){$CPUName="*"}

#Describe This VM (Details are imporant here visible in VMM)
$CPUDescription = Read-Host "Enter a description for this virtual machine"
if ($CPUDescription -eq ""){$CPUDescription="*"} ; if ($CPUDescription -eq $NULL){$CPUDescription="*"} 

New-SCVirtualScsiAdapter -VMMServer sea-vscvmm -JobGroup f4d8236c-30d5-4566-b21a-ae7d041790d7 -AdapterID 7 -ShareVirtualScsiAdapter $false -ScsiControllerType DefaultTypeNoType 

New-SCVirtualDVDDrive -VMMServer sea-vscvmm -JobGroup f4d8236c-30d5-4566-b21a-ae7d041790d7 -Bus 1 -LUN 0 

$VMNetwork = Get-SCVMNetwork -VMMServer sea-vscvmm -Name "InsynQ 2.0" -ID "4159271d-8ace-4ad1-8d74-54b2d196a0f7"
$PortClassification = Get-SCPortClassification -VMMServer sea-vscvmm | where {$_.Name -eq "Data Network"}

New-SCVirtualNetworkAdapter -VMMServer sea-vscvmm -JobGroup f4d8236c-30d5-4566-b21a-ae7d041790d7 -MACAddress "00-00-00-00-00-00" -MACAddressType Static -Synthetic -EnableVMNetworkOptimization $false -IPv4AddressType Dynamic -IPv6AddressType Dynamic -VMNetwork $VMNetwork -PortClassification $PortClassification 

Set-SCVirtualCOMPort -NoAttach -VMMServer sea-vscvmm -GuestPort 1 -JobGroup f4d8236c-30d5-4566-b21a-ae7d041790d7 

Set-SCVirtualCOMPort -NoAttach -VMMServer sea-vscvmm -GuestPort 2 -JobGroup f4d8236c-30d5-4566-b21a-ae7d041790d7 

Set-SCVirtualFloppyDrive -RunAsynchronously -VMMServer sea-vscvmm -NoMedia -JobGroup f4d8236c-30d5-4566-b21a-ae7d041790d7 

$CPUType = Get-SCCPUType -VMMServer sea-vscvmm | where {$_.Name -eq "3.60 GHz Xeon (2 MB L2 cache)"}

$CapabilityProfile = Get-SCCapabilityProfile -VMMServer sea-vscvmm | where {$_.Name -eq "Hyper-V"}

#Nulled out Call to Create new HW Profile for TESTING
#New-SCHardwareProfile -VMMServer sea-vscvmm -CPUType $CPUType -Name "Profile7e612a6a-4844-45df-aa99-f989b26ee292" -Description "Profile used to create a VM/Template" -CPUCount 6 -MemoryMB 1024 -DynamicMemoryEnabled $true -DynamicMemoryMinimumMB 512 -DynamicMemoryMaximumMB 15360 -DynamicMemoryBufferPercentage 20 -MemoryWeight 5000 -VirtualVideoAdapterEnabled $false -CPUExpectedUtilizationPercent 20 -DiskIops 0 -CPUMaximumPercent 100 -CPUReserve 0 -NumaIsolationRequired $false -NetworkUtilizationMbps 0 -CPURelativeWeight 100 -HighlyAvailable $true -HAVMPriority 2000 -DRProtectionRequired $false -NumLock $false -BootOrder "CD", "IdeHardDrive", "PxeBoot", "Floppy" -CPULimitFunctionality $false -CPULimitForMigration $false -CapabilityProfile $CapabilityProfile -JobGroup f4d8236c-30d5-4566-b21a-ae7d041790d7 

$Template = Get-SCVMTemplate -VMMServer sea-vscvmm -ID "a9806749-14bd-468c-868d-23d1b47eade0" | where {$_.Name -eq "Windows Datacenter 2012"}
$HardwareProfile = Get-SCHardwareProfile -VMMServer sea-vscvmm | where {$_.Name -eq "Cloud Runner - VC setup"}

$OperatingSystem = Get-SCOperatingSystem -VMMServer sea-vscvmm -ID "ff0fe0a6-7166-44d3-8a0c-379195e2f9bb" | where {$_.Name -eq "64-bit edition of Windows Server 2012 Datacenter"}

New-SCVMTemplate -Name "IQ VMM Template" -Template $Template -HardwareProfile $HardwareProfile -JobGroup f4d8236c-30d5-4566-b21a-ae7d041790d7 -ComputerName $CPUName -TimeZone 4  -FullName "" -OrganizationName "" -AnswerFile $null -OperatingSystem $OperatingSystem 

$template = Get-SCVMTemplate -All | where { $_.Name -eq "IQ VMM Template" }
$virtualMachineConfiguration = New-SCVMConfiguration -VMTemplate $template -Name $Name
Write-Output $virtualMachineConfiguration
$vmHost = Get-SCVMHost -ID "108744e5-d740-48ea-9a12-98cb0bc6a2f9"
Set-SCVMConfiguration -VMConfiguration $virtualMachineConfiguration -VMHost $vmHost
Update-SCVMConfiguration -VMConfiguration $virtualMachineConfiguration
Set-SCVMConfiguration -VMConfiguration $virtualMachineConfiguration -ComputerName "$CPUName"

Update-SCVMConfiguration -VMConfiguration $virtualMachineConfiguration
New-SCVirtualMachine -Name $Name -VMConfiguration $virtualMachineConfiguration -Description "$CPUDescription" -BlockDynamicOptimization $false -JobGroup "f4d8236c-30d5-4566-b21a-ae7d041790d7" -ReturnImmediately -DelayStartSeconds "0"

Open in new window

1 Solution
seanabAuthor Commented:
I've figured it out myself... I'm just appending to that script a command to delete the profile at the end.  That does what I need so I do not need to go back in the VMM Library and remove any profiles or templates it creates in the process.

Question closed....
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now