Monthly Archive

Categories

Hyper-V

Hyper-V VM start time

Its fairly easy to see how long a VM has been running – but how do you know the Hyper-V VM start time?

In Hyper-V the VM uptime is easy to find

PS> Get-VM | where State -eq 'Running'

Name       State   CPUUsage(%) MemoryAssigned(M) Uptime           Status             Version 
 ----       -----   ----------- ----------------- ------           ------             ------- 
 W16AS01    Running 6           1246              00:12:18.6480000 Operating normally 8.0 
 W16CN01    Running 0           538               00:09:18.2180000 Operating normally 8.2 
 W16DC01    Running 0           940               00:15:19.1550000 Operating normally 8.0 
 W17035CN01 Running 0           540               00:06:17.7980000 Operating normally 8.2 
 W1709CN01  Running 0           512               00:03:15.8540000 Operating normally 8.2

 

Sometimes you might want to know when the VM was started

The Uptime property is a TimeSpan so you can calculate the start time

PS> $now = Get-Date 
PS> Get-VM | where State -eq 'Running' | select Name, @{N='StartTime'; E={$now - $_.Uptime}}

Name       StartTime 
 ----       --------- 
 W16AS01    02/12/2017 10:23:15 
 W16CN01    02/12/2017 10:26:16 
 W16DC01    02/12/2017 10:20:15 
 W17035CN01 02/12/2017 10:29:16 
 W1709CN01  02/12/2017 10:32:18

 

Once you’ve added the calculated property you can use like any other property

PS> Get-VM | where State -eq 'Running' | select Name, @{N='StartTime'; E={$now - $_.Uptime}} | sort StartTime

Name       StartTime 
----       --------- 
 W16DC01    02/12/2017 10:18:46 
 W16AS01    02/12/2017 10:21:47 
 W16CN01    02/12/2017 10:24:47 
 W17035CN01 02/12/2017 10:27:48 
 W1709CN01  02/12/2017 10:30:50

 

Using calculated fields like this is a handy technique for changing the way data is displayed.

Deal of the Day – 15 June 2017

My book is Manning’s Deal of the Day  - 15 June 2017:

Half off Learn Hyper-V in a Month of Lunches. Use code dotd061517au at http://bit.ly/2rZXI9x

Sign up for DoD notifications at https://www.manning.com/dotd

PowerShell Direct failure

PowerShell Direct is introduced with Server 2016/Windows 10. it enables you to create a remoting session from the Hyper-V host to a VM using the VM name or ID. I recent discovered a PowerShell Direct failure that I couldn’t explain until now.

Normally you do this:

PS> New-PSSession -VMName w16cn01 -Credential (Get-Credential w16cn01\administrator)

Id Name    ComputerName  ComputerType  State  ConfigurationName  Availability
-- ----     ------------  ------------    -----  -----------------  ------------
1 Session1       W16CN01  VirtualMachine  Opened                       Available

But on one particular machine I was getting this

PS> New-PSSession -VMName w16as01 -Credential (Get-Credential w16as01\administrator)
New-PSSession : [W16AS01] An error has occurred which Windows PowerShell cannot handle. A remote session might have ended.
At line:1 char:1
+ New-PSSession -VMName w16as01 -Credential (Get-Credential w16as01\adm ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotin
gDataStructureException
+ FullyQualifiedErrorId : PSSessionOpenFailed

I couldn’t find an explanation for this particular PowerShell Direct failure

I’ve been working with PowerShell v6 and OpenSSH the last few days and I noticed that the PowerShell directory had been removed from the system path by the installation of one of these pieces of software.

W16AS01 had been the first machine I experimented with PowerShell v6/OpenSSH and it was the first to experience this PowerShell direct failure.

I checked W16AS01 and sure enough the PowerShell folder was missing from the system path. Adding the Powershell folder back onto the path (and restarting the machine for luck) then retrying PowerShell Direct gives:

PS> New-PSSession -VMName W16AS01 -Credential (Get-Credential W16AS01\Administrator)

Id Name            ComputerName    ComputerType    State         ConfigurationName     Availability
-- ----            ------------    ------------    -----         -----------------     ------------
1 Session1        W16AS01         VirtualMachine  Opened                                 Available

Looks like I’ve found a solution for this particular PowerShell direct failure

Hyper-V book deal

March 11 2017 – My book Learn Hyper-V in a Month of Lunches is Manning’s Deal of the day. Get 50% off using code dotd031117au at http://bit.ly/2niU715

Also see https://www.manning.com/dotd

Using Hyper-V

When you think of using Hyper-V most people think of virtualising their infrastructure – big servers running 10s, 100s or even 1000s of virtual machines.

 

There is another reason for using Hyper-V

 

You can use Hyper-V to create a VM so you can install an application that may conflict with you standard workstation – as an example you may need access to a component of an older version of Office for instance Infopath 2010 but you don’t want that application to conflict with your installation of the latest and greatest version of Office.

 

The Windows client operating system has the ability to install Hyper-V since Windows 8. So if you’re running Windows 8, Windows 8.1 or Windows 10 AND your machine is capable of supporting Hyper-V:

- Windows Enterprise, Professional or Educational

- 64-bit processor with SLAT

- CPU support for VM monitor mode extension

- Minimum of 4GB of memory

 

Install Hyper-V

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V –All

 

Create a virtual switch and create your VMs

 

See https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/quick-start/enable-hyper-v and subsequent links

 

Better still get a copy of Learn Hyper-V in a Month of Lunches

https://www.manning.com/books/learn-hyper-v-in-a-month-of-lunches

Learn Hyper-V – Deal of the day – January 28 2017

Deal of the Day January 28: Half off my book Learn Hyper-V in a Month of Lunches. Use code dotd012817au at http://bit.ly/2jAIfWK

 

More information from DOTD's page at https://www.manning.com/dotd

Christmas treat

If you’re looking for something to treat yourself for Christmas then garb a copy of My new book – Learn Hyper-V in a Month of Lunches.

 

Better still tomorrow 18 December 2016 you can get the book for half price:

 

Deal of the Day December 18: Half off Learn Hyper-V in a Month of Lunches. Use code dotd121816au at http://bit.ly/2hOkhKb

Hyper-V book

I’ve been working with Andy Syrewicze on Learn Hyper-V in a Month of Lunches.

 

Its now available in Manning’s Early Access program (MEAP) https://www.manning.com/books/learn-hyper-v-in-a-month-of-lunches

 

Until 14 November 2016 you can get the MEAP for half price using code mlsyrewicze

New Hyper-V switch on Windows 10

My test/lab machine had been running Windows Server 2016 TP 5. With Server 2016 now RTM it was time for a rebuild.  Unfortunately, 2016 RTM carried on from TP5 and decided not to work with my wireless card.

 

Decided to try using Hyper-V on Windows 10 – it recognises the wifi card and is happy to work with it.

 

After installing the Hyper-V feature I needed to create some Hyper-v virtual switches

PS> New-VMSwitch -SwitchType External -Name 'LAN' -NetAdapterName 'LAN'
New-VMSwitch : Cannot validate argument on parameter 'SwitchType'. The argument "External" does not belong to the set
"Internal,Private" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the
command again.
At line:1 char:26
+ New-VMSwitch -SwitchType External -Name 'LAN' -NetAdapterName 'LAN'
+                          ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [New-VMSwitch], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.HyperV.PowerShell.Commands.NewVMSwitch

 

But External is a valid switch type

 

PS> New-VMSwitch -SwitchType x
New-VMSwitch : Cannot bind parameter 'SwitchType'. Cannot convert value "x" to type
"Microsoft.HyperV.PowerShell.VMSwitchType". Error: "Unable to match the identifier name x to a valid enumerator name.
Specify one of the following enumerator names and try again:
Private, Internal, External"
At line:1 char:26
+ New-VMSwitch -SwitchType x
+                          ~
    + CategoryInfo          : InvalidArgument: (:) [New-VMSwitch], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.HyperV.PowerShell.Commands.NewVMSwitch

 

Wonder how many of these issues I’m going to find!

Shutting down machines in parallel

My test lab is a set of virtual machines running on a Lenovo w1510 laptop. When I’ve finished working for the day I want to shut down the virtual machines and the laptop. I may have anywhere between 2 and 8 (or more) VMs running so scripting the shutdown helps a lot.

 

Machines can be shutdown independently so this is an action that is ideal for parallel execution through a workflow.

 

workflow stop-allvms {
$vms = Get-VM | where State -eq "Running" 
foreach -parallel ($vm in $vms) {
Stop-Computer -PSComputerName $vm.Name -Force -Verbose
}
}

stop-allvms

 

Get the running VMs. use foreach –parallel to run stop-computer against each VM. Notice I’ve had to change the parameter on Stop-Computer from –ComputerName to –PSComputerName

 

Another little workflow that makes life easier. I looks like workflows aren’t going to be the game changer that we originally thought but they do add some interesting options.