Monday, September 24, 2012

How (NOT) to create 4 test VM’s with Windows server 2012 in PowerShell

How (NOT) to create 4 test VM’s with Windows server 2012 in PowerShell:
While testing with Hyper V in Windows 8 I Created 4 virtual machines with Windows 2012, for testing resource metering, partly in PowerShell (except for console input WS12 installation and where I cheated in the GUI ;) )

I decided to go as stupid as could be (yes I thought about removing some lines ;), but resisted, and with 82 lines in my history I (kind off) succeeded.

** Disclaimer ** Do not run this code as is in a production environment!!!

This is not a good example of how to provisioning VM's and I made mistakes on purpose (looking for mandatory property handling), and did some very nasty things on the way (adding an $null disk), but the process was very educational and in the end everything running without much more as a lot of red errors ;)

And I ended up with VM's with dynamic memory enabled and the resource monitoring to show it works ;).

Comments (green) and some inline console output

Id CommandLine
-- -----------
1 new-vm # just think and type .. Oops that worked (expected mandatory parameters prompt)
2 new-vm
# let's try it again
3 get-vm # Yep 2 with same name, look if I can do stupid things with that
4 start-vm

PS C:\Windows\system32> start-vm



cmdlet Start-VM at command pipeline position 1


Supply values for the following parameters:


Name[0]: New Virtual Machine


Name[1]:


PS C:\Windows\system32> get-vm



Name                State   CPUUsage(%) MemoryAssigned(M) Uptime   Status


----                -----   ----------- ----------------- ------   ------


MowW12              Running 23          2048              00:02:45 Operating normally


New Virtual Machine Running 0           512               00:00:04 Operating normally


New Virtual Machine Running 0           512               00:00:05 Operating normally

5 get-vm # Yep both Running
6 get-vm new*
7 get-vm new* | Restart-VM
8 Get-VM new* | gm # Added 1 DVD GUI
9 Get-VM new* | % {$_.DVDDrives}

PS C:\Windows\system32> Get-VM new* | % {$_.DVDDrives}



VMName              ControllerType ControllerNumber ControllerLocation DvdMediaType Path


------              -------------- ---------------- ------------------ ------------ ----


New Virtual Machine IDE            1                0                  ISO          C:\Users\Marc\Downloads\en_windo...


New Virtual Machine IDE            1                0                  None


10 gcm *dvd* # look for commands with DVD in them then blindly try to add one
11 Add-VMDvdDrive -VMName (Get-VM new*)[1]
12 Add-VMDvdDrive -VMName (Get-VM new*)[1].vmname
13 Add-VMDvdDrive -VMName (Get-VM new*)[1].vmname -Path (Get-VM new*)[1].DVDDrives[0].PATH
14 (Get-VM new*)[1].DVDDrives[0]
15 (Get-VM new*)[1].DVDDrives[1]
16 (Get-VM new*)[0].DVDDrives[0]
17 (Get-VM new*)[0].DVDDrives[0].PATH
18 Add-VMDvdDrive -VM (Get-VM new*)[1] -Path (Get-VM new*)[0].DVDDrives[0].PATH
19 Add-VMDvdDrive -? # OK maybe need some help here ;)
20 get-help Add-VMDvdDrive -examples
21 Add-VMDvdDrive -VM (GET-VM NEW*)[1] -Path C:\Users\Marc\Downloads\en_windows_server_2012_x64_dvd_915478.iso

PS C:\Windows\system32> Add-VMDvdDrive -VM (GET-VM NEW*)[1] -Path C:\Users\Marc\Downloads\en_windows_server_2012_x64_dvd


_915478.iso


Add-VMDvdDrive : 'New Virtual Machine' failed to add device 'Synthetic DVD Drive'. (Virtual machine ID


B251745B-EBEA-46C3-8CCF-DF5F5E27FF5E)


At line:1 char:1


+ Add-VMDvdDrive -VM (GET-VM NEW*)[1] -Path C:\Users\Marc\Downloads\en_windows_ser ...


+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


    + CategoryInfo          : NotSpecified: (Microsoft.HyperV.PowerShell.VMTask:VMTask) [Add-VMDvdDrive], Virtualizati


   onOperationFailedException


    + FullyQualifiedErrorId : OperationFailed,Microsoft.HyperV.PowerShell.Commands.AddVMDvdDriveCommand


22 Get-VM new* | % {$_.DVDDrives} # Strange should have worked now, but …
23 Add-VMDvdDrive -VM (GET-VM NEW*)[1] -Path C:\Users\Marc\Downloads\en_windows_server_2012_x64_dvd_915478.iso -ControllerNumber 1
24 (GET-VM NEW*)[1]
25 (GET-VM NEW*)[1] | Restart-VM # and let the installation begin
26 get-vm | Remove-VMDvdDrive # now we have the VM's running let's clean up install media

Remove-VMDvdDrive : The input object cannot be bound to any parameters for the command either because the command does


not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.


At line:1 char:10


+ get-vm | Remove-VMDvdDrive


+          ~~~~~~~~~~~~~~~~~


    + CategoryInfo          : InvalidArgument: (Microsoft.Hyper....VirtualMachine:PSObject) [Remove-VMDvdDrive], Param


   eterBindingException


    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.HyperV.PowerShell.Commands.RemoveVMDvdDriveCommand

27 get-vm|%{Remove-VMDvdDrive -VMName $_} # Grrrr hate that, but as is not the same noun
28 Get-VMDvdDrive
# maybe my mistake ;)

29 Get-VMDvdDrive -VMName * | Remove-VMDvdDrive

Remove-VMDvdDrive : The operation cannot be performed while the virtual machine is in its current state.


At line:1 char:28


+ Get-VMDvdDrive -VMName * | Remove-VMDvdDrive


+                            ~~~~~~~~~~~~~~~~~


    + CategoryInfo          : InvalidArgument: (Microsoft.HyperV.PowerShell.DvdDrive:DvdDrive) [Remove-VMDvdDrive], Vi


   rtualizationOperationFailedException


    + FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.RemoveVMDvdDriveCommand

30 Get-VMDvdDrive -VMName * # But I can do it in GUI why not here, Aha path to None, not DVD drive gone ;)
31 Get-VMDvdDrive -VMName * | Set-VMDvdDrive -Path none
32 Get-VMDvdDrive -VMName * | Set-VMDvdDrive -Path ""
33 Get-VMDvdDrive -VMName *
34 gcm *dvd*
35 Set-VMDvdDrive -?
36 get-help Set-VMDvdDrive -examples
37 Get-VMDvdDrive -VMName * | Set-VMDvdDrive -Path $null # In the end there is some logic ;)
38 Get-VMResourcePool # Now I cleaned up the Installation CD's and Installed

PS C:\Windows\system32> Get-VMResourcePool # let's look at the new resource metering ..



Name       ResourcePoolType       ParentName ResourceMeteringEnabled


----       ----------------       ---------- -----------------------


Primordial FibreChannelConnection            False


Primordial FibreChannelPort                  False


Primordial ISO                               False


Primordial VHD                               False


Primordial VFD                               False


Primordial Ethernet                          False


Primordial Memory                            False


Primordial Processor                         False


39 gcm *resou*
40 Enable-VMResourceMetering
41 Get-VMResourcePool
42 Measure-VMResourcePool
43 Measure-VMResourcePool *
44 Measure-VM * # Cool stuff !!!

PS C:\Windows\system32> Measure-VMResourcePool *



Name       ResourcePoolType                   AvgCPU(MHz) AvgRAM(M) TotalDisk(M) NetworkInbound(M) NetworkOutbound(M)


----       ----------------                   ----------- --------- ------------ ----------------- ------------------


Primordial {Ethernet, Memory, Processor, VHD} 28          3072      390144       0                 0




PS C:\Windows\system32> Measure-VM *



VMName AvgCPU(MHz) AvgRAM(M) MaxRAM(M) MinRAM(M) TotalDisk(M) NetworkInbound(M) NetworkOutbound(M)


------ ----------- --------- --------- --------- ------------ ----------------- ------------------


W12VM2 18          512       512       512       130048       0                 0


W12VM1 6           512       512       512       130048       1                 0


MowW12 3           2048      2048      2048      130048       1                 0

45 Measure-VM *
46 Measure-VM *
47 Measure-VM *
48 Measure-VM *
49 Measure-VM * # (Time lap testing with VM's) .. I need some more VM's, let's try again ;)
50 New-VM | Add-VMDvdDrive -Path C:\Users\Marc\Downloads\en_windows_server_2012_x64_dvd_915478.iso -ControllerLocation 0 -ControllerNumber 1
51 New-VM | Add-VMDvdDrive -Path C:\Users\Marc\Downloads\en_windows_server_2012_x64_dvd_915478.iso -ControllerLocation 0 -ControllerNumber 0
52 Get-VM new*
53 Get-VM new* | add-VMDvdDrive -Path C:\Users\Marc\Downloads\en_windows_server_2012_x64_dvd_915478.iso -ControllerLocation 0 -ControllerNumber 0
# I need some Dynamic memory to test resource metering
54 Get-VM new* | Set-VM -MemoryStartupBytes 2048 -DynamicMemory $true # realy
GRRR here


PS C:\Windows\system32> Get-VM new* | Set-VM -MemoryStartupBytes 2048 -DynamicMemory $true


Set-VM : The input object cannot be bound to any parameters for the command either because the command does not take


pipeline input or the input and its properties do not match any of the parameters that take pipeline input.


At line:1 char:15


+ Get-VM new* | Set-VM -MemoryStartupBytes 2048 -DynamicMemory $true


+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


    + CategoryInfo          : InvalidArgument: (Microsoft.Hyper....VirtualMachine:PSObject) [Set-VM], ParameterBinding


   Exception


    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.HyperV.PowerShell.Commands.SetVM

55 Get-VM new* |% {Set-VM -vm $_ -MemoryStartupBytes 2048 -DynamicMemory $true } # Grrrrr misleading
56 Get-VM new* |% {Set-VM -vm $_ -MemoryStartupBytes 2048 -DynamicMemory:$true } # LOL to much GUI

PS C:\Windows\system32> Get-VM new* |% {Set-VM -vm $_ -MemoryStartupBytes 2028 -DynamicMemory:$true }


Set-VM : 'New Virtual Machine' failed to modify device 'Memory'. (Virtual machine ID


E826DCD0-73BC-4121-B228-69F13DCFAF54)


Invalid startup memory amount assigned for 'New Virtual Machine'. The minimum amount of memory you can assign to a


virtual machine is '8' MB. (Virtual machine ID E826DCD0-73BC-4121-B228-69F13DCFAF54)


A parameter that is not valid was passed to the operation.


At line:1 char:17


+ Get-VM new* |% {Set-VM -vm $_ -MemoryStartupBytes 2028 -DynamicMemory:$true }


+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


    + CategoryInfo          : InvalidArgument: (Microsoft.HyperV.PowerShell.VMTask:VMTask) [Set-VM], VirtualizationOpe


   rationFailedException


    + FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.SetVM

57 Get-VM new* |% {Set-VM -vm $_ -MemoryStartupBytes 2048mb -DynamicMemory:$true } # but easy to fix
58 Get-VM new* | Start-vm # As you might be wondering , I cheated with the Disk's (GUI) let's do them in PowerShell now
59 Get-VM new* | Add-VMHardDiskDrive # let's do stupid again :
60 Get-VM new* | stop-vm
61 Get-VM new* | Add-VMHardDiskDrive # Hmm that should not work !! …
62 Get-VM new* | Start-vm # And it doesn't ;)

PS C:\Windows\system32> Get-VM new* | Start-vm


Start-vm : 'New Virtual Machine' failed to start. (Virtual machine ID E826DCD0-73BC-4121-B228-69F13DCFAF54)


'New Virtual Machine' Microsoft Emulated IDE Controller (Instance ID 83F8638B-8DCA-4152-9EDA-2CA8B33039B4): Failed to


Power on with Error 'One or more arguments are invalid' (0x80070057). (Virtual machine ID


E826DCD0-73BC-4121-B228-69F13DCFAF54)


'New Virtual Machine': Failed to open attachment ''. Error: 'One or more arguments are invalid' (0x80070057). (Virtual


machine ID E826DCD0-73BC-4121-B228-69F13DCFAF54)


'New Virtual Machine': Failed to open attachment ''. Error: 'One or more arguments are invalid' (0x80070057). (Virtual


machine ID E826DCD0-73BC-4121-B228-69F13DCFAF54)


At line:1 char:15


+ Get-VM new* | Start-vm


+               ~~~~~~~~


    + CategoryInfo          : NotSpecified: (Microsoft.HyperV.PowerShell.VMTask:VMTask) [Start-VM], VirtualizationOper


   ationFailedException


    + FullyQualifiedErrorId : OperationFailed,Microsoft.HyperV.PowerShell.Commands.StartVMCommand

63 Get-VMHardDiskDrive
64 Get-VM new* | Add-VMHardDiskDrive -Path 'C:\Users\Public\Documents\Hyper-V\Virtual hard disks\new1.vhdx'
65 Get-VMHardDiskDrive
66 Get-VMHardDiskDrive -VMName new*
67 Get-VMHardDiskDrive -VMName new* | Remove-VMHardDiskDrive
68 Get-VM new* | Add-VMHardDiskDrive -Path 'C:\Users\Public\Documents\Hyper-V\Virtual hard disks\new1.vhdx'
69 New-VHD -Path 'C:\Users\Public\Documents\Hyper-V\Virtual hard disks\hd1.vhdx' -Dynamic -SizeBytes 127gb
70 New-VHD -Path 'C:\Users\Public\Documents\Hyper-V\Virtual hard disks\hd2.vhdx' -Dynamic -SizeBytes 127gb
71 Get-VM new* | Add-VMHardDiskDrive -Path 'C:\Users\Public\Documents\Hyper-V\Virtual hard disks\hd1.vhdx'
72 Get-VM new* | start-vm
73 (Get-VM new*)[1] | Add-VMHardDiskDrive -Path 'C:\Users\Public\Documents\Hyper-V\Virtual hard disks\hd2.vhdx'
74 Get-VM new* | start-vm
75 Get-VMHardDiskDrive -VM (Get-VM new*)[1] | Remove-VMHardDiskDrive
76 (Get-VM new*)[1] | Add-VMHardDiskDrive -Path 'C:\Users\Public\Documents\Hyper-V\Virtual hard disks\hd2.vhdx'
77 Get-VM new* | start-vm
78 h -c 100
79 h -c 100
80 Measure-VM *
81 Enable-VMResourceMetering -VMName *
82 Measure-VM *
You can see below very well using resource metering, that dynamic memory works (Note : as the 2 New virtual machines are still in Windows Setup, Dynamic memory cannot trim there )

PS C:\Windows\system32> Measure-VM *


VMName              AvgCPU(MHz) AvgRAM(M) MaxRAM(M) MinRAM(M) TotalDisk(M) NetworkInbound(M) NetworkOutbound(M)

------              ----------- --------- --------- --------- ------------ ----------------- ------------------

W12VM2              137         512       512       512       130048       0                 0

W12VM1              29          600       2048      512       130048       2                 4

New Virtual Machine 1           2048      2048      2048      130048       0                 0

New Virtual Machine 1           2048      2048      2048      130048       0                 0

MowW12              35          786       2048      514       130048       2                 6


So even as I tried to sabotage as much as possible I managed to do it with good result, learned a lot an probably can change the code above to a oneliner later ;)

Enjoy,

Greetings MOW


DIGITAL JUICE

No comments:

Post a Comment

Thank's!