And why do I want to add an registry key under AlternateShell instead of the Shell registry key that holds Explorer.exe. One reason is that when I enable the GUI from Core mode I do not get the full GUI. So when I run Get-WindowsFeature *gui-shell* | Add-WindowsFeature and then reboot the server it looks like the following screendump:
As you can see in the following screendump and in Andrew´s post, I keep the CMD and just set an new registry key that has a higher value, but not as high as Explorer. Doing this I get the Powershell console when in Core and the Explorer when in Full GUI mode. When in full GUI mode I get a temporary Pagefile error though 🙁 , the system seems to work correctly.. I have tested the Get-WindowsFeature *gui-shell* | Add-WindowsFeature on an installation where I have not done any alternation with the shells and there the page file dialog appears also so this is caused by another issue that I will investigate in another blog post.
In twitter yesterday I saw that there was a discussion about setting default shell to Powershell in Windows Core, Jeffery Hicks has done this in Windows 2008 R2 and I wanted to test if his little trick worked in Windows 2012 also (by the way, why is it not default by default in 2012 Core? )
I have my testmachine ready and so lets go
Interestingly, When I check the registry I got that my shell in the 2012 Core was Explorer.exe,
but when I got a bit deeper I could see under AlternativeShells that the CMD was there
I tried to edit that but got an error 🙁
well lets try to exchange the explorer.exe in shell to powershell.exe instead, I use the $env:userprofile to get the same directory as with cmd
And here you can see in my registry, notice that the $env:userprofile is still there thanks to the in the string variable and that means that when another user log in they will get their own directory:
And when I try to log in again I get the Powershell Console by default and also in the “right” directory
Lets hope Microsoft set Powershell console as default also in the Core version when Windows 2012 becomes RTM 🙂
Update: I did apparently not follow the tweets so far that I noticed that the PS master Thomas Lee already had made a blog about this, in his post I would just add this to get the users directory
A colleague wanted a script that could make a Host-VM affinity rule so that certain VM´s that resided on one storage at one site would run on the hosts on that site, the cluster spanns over two sites so the VM´s can run at both sites but one datastore is primary at the first site and the other at the second site. So He wanted to get the VM´s that have their storage on datastoreLocation1 to be in an host-vm affinity rule “Should Run” on the hosts that is in that site. In this community post I found most of the code, I made som adjustments but mostly the cred goes to @lucd22 and GotMoo.
Here you can see the screendump from the cluster settings
Here is the powerCLI that has been run
One thing here was also that this VMgroup should be updated once a day if there was new VM´s that needed to be included. So this lines should be run in a scheduled task, this will make the DRSVMGroup VMLocationDC1 to be updated with the VM´s currently located at that datastore:
Here are the PowerCLI functions for doing this. As you can see I have an switch parameter for -MustRun, that will create the rule as a Must Run on these hosts.
function New-DRSGroupRule{
<#
.SYNOPSIS
Create a new DRSGroupRule for VMs to reside on some hosts in a cluster
.DESCRIPTION
Use this function to create vms in a group and hosts in a group and a host-vm affinity
.PARAMETER MustRun
A switch that will create the rule with Must Run on these host, if not set it will create the rule with should run.
.NOTES
Author: Niklas Akerlund / RTS (most of the code came from http://communities.vmware.com/message/1667279 @LucD22 and GotMoo)
Date: 2012-06-28
#>
param (
[Parameter(Position=0,Mandatory=$true,HelpMessage="A Cluster",
ValueFromPipeline=$True)]
$cluster,
$VMHosts,
$VMs,
[string]$Name,
[switch]$MustRun
)
$cluster = Get-Cluster $cluster
$spec = New-Object VMware.Vim.ClusterConfigSpecEx
$groupVM = New-Object VMware.Vim.ClusterGroupSpec
$groupVM.operation = "add"
$groupVM.Info = New-Object VMware.Vim.ClusterVmGroup
$groupVM.Info.Name = "VM$Name"
Get-VM $VMs | %{
$groupVM.Info.VM += $_.Extensiondata.MoRef
}
$spec.GroupSpec += $groupVM
$groupESX = New-Object VMware.Vim.ClusterGroupSpec
$groupESX.operation = "add"
$groupESX.Info = New-Object VMware.Vim.ClusterHostGroup
$groupESX.Info.Name = "Host$Name"
Get-VMHost $VMHosts | %{
$groupESX.Info.Host += $_.Extensiondata.MoRef
}
$spec.GroupSpec += $groupESX
$rule = New-Object VMware.Vim.ClusterRuleSpec
$rule.operation = "add"
$rule.info = New-Object VMware.Vim.ClusterVmHostRuleInfo
$rule.info.enabled = $true
$rule.info.name = $Name
if($MustRun){
$rule.info.mandatory = $true
}else{
$rule.info.mandatory = $false
}
$rule.info.vmGroupName = "VM$Name"
$rule.info.affineHostGroupName = "Host$Name"
$spec.RulesSpec += $rule
$cluster.ExtensionData.ReconfigureComputeResource($spec,$true)
}
function Update-DrsVMGroup {
<#
.SYNOPSIS
Update DRS VM group with a new collection of VM´s
.DESCRIPTION
Use this function to update the ClusterVMgroup with VMs that are sent in by parameters
.PARAMETER xyz
.NOTES
Author: Niklas Akerlund / RTS (most of the code came from http://communities.vmware.com/message/1667279 @LucD22 and GotMoo)
Date: 2012-06-28
#>
param (
$cluster,
$VMs,
$groupVMName)
$cluster = Get-Cluster $cluster
$spec = New-Object VMware.Vim.ClusterConfigSpecEx
$groupVM = New-Object VMware.Vim.ClusterGroupSpec
#Operation edit will replace the contents of the GroupVMName with the new contents seleced below.
$groupVM.operation = "edit"
$groupVM.Info = New-Object VMware.Vim.ClusterVmGroup
$groupVM.Info.Name = $groupVMName
Get-VM $VMs | %{
$groupVM.Info.VM += $_.Extensiondata.MoRef
}
$spec.GroupSpec += $groupVM
#Apply the settings to the cluster
$cluster.ExtensionData.ReconfigureComputeResource($spec,$true)
}
When I get the time I might add some more functionality to also remove rules and groups.
I made a blog post some time ago, where i created a powerCLI function for creating a VM affinity rule that would keep my VM´s in a vApp together in the cluster for better VM-VM performance. I then checked for MoRef to get the VM correlated between vCloud Director and vCenter.
The answer to my problem was much closer than I thought, the vCloud director creates a folder in the “VM´s and Template view” for each vApp and guess what, all VM´s are in there.. so I do not need to ask the vCloud Director anymore (well if I want the Cloud vApp name I do need to ask it..) but ironically I had not looked there and found out that the folders actually where created for each deployed vCloud vApp :-/
So the code is a bit simpler and to get all VM´s from a folder I just type this, the asterisk is because the folder also gets a unique id
Get-Folder | where {$_.Name -like "ProdvCPod*"} | Get-VM
And then I just need to put those VM´s into an array and create a DRS rule, and here is the function that does this
<#
.Synopsis
Add an affinity rule for a vCloud Director vAPP
.DESCRIPTION
This function takes a vApp name (string) as parameter and creates an affinity rule for them to keep them together
.EXAMPLE
Add-vAppAffinity -CIVApp ProdCust01
.NOTES
Author: Niklas Akerlund
Date: 2012-06-21
#>
function Add-vAppAffinity
{
Param
(
# Parameter for the vAPP
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
$CIVApp,
# If the rule should apply on a different cluster
$Cluster = "Cluster01"
)
if ($CIVApp -ne " " -or $CIVApp -ne $null -or $CIVApp.length -ne 0 ){
$Cluster = Get-Cluster $Cluster
$VAppname = $CIVApp + "*"
$VMs = Get-Folder | where {$_.Name -like $VAppname} | Get-VM
if (!(Get-DrsRule -Cluster $Cluster -Name $CIVApp -ErrorAction SilentlyContinue)){
New-DrsRule -Name $CIVApp -Cluster $Cluster -KeepTogether $true -VM $VMs
}else{
Remove-DrsRule -Rule (Get-DrsRule -Cluster $Cluster -Name $CIVApp) -Confirm:$false
New-DrsRule -Name $CIVApp -Cluster $Cluster -KeepTogether $true -VM $VMs
}
}
}
To run it:
and it looks something like this if I check the cluster settings
preferably you have set your DRS cluster to fully automated or not so much will happen 🙂
I wanted to create a function that could help an IT Pro Admin with the task to create a clone of a running VM, Yes you can use the Export-VM cmdlet but then your VM must be turned off and in some cases, for example when you want to test a new release or patch on a production system but first test it in a safe environment and you are not allowed to stop the original VM.
The function exists in System Center Virtual Machine Manager but there it also must be turned off
So how do I do? I have made a PowerShell function that take a snapshot, copies the vhd files and creates a new VM, connect the vhd´s and network cards, configures the VM with number of processors, dynamic memory etc.
As the Snapshot merges the vhdx files when the VM is running in this new Hyper-V version I thought it was an sufficient way to solve that the VM actually was not writing to the .vhdx files when I copy them but into the .avhdx diff file.
This is a version 0.1 and yes it need some development but right now I do not have the time to make it supernice,
Here you can see it in action
And when It is finished It looks like this in the gui
And here is the PowerShell function
<#
.Synopsis
Function to clone a running VM
.DESCRIPTION
This function can be used to clone a running vm and connect the copied vhd´s and network
.EXAMPLE
Clone-VM -VMName vmdisktest -VMCloneName vmdisktest-clone -Path c:\VMs
.EXAMPLE
Clone-VM -VMName vmdisktest -VMCloneName vmdisktest-clone -Path c:\VMs -Switch Private
.Notes
Author: Niklas Akerlund
Date: 2012-06-19
#>
function Clone-VM
{
[CmdletBinding()]
[OutputType([int])]
Param
(
# Name of the VM to be cloned
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
$VMName,
[string] $VMCloneName,
# Where to store the VM-clone
[string] $Path,
# Set to another network switch
[string] $Switch = " "
)
# Get VM
$VM = Get-VM $VMName
$VHDs = Get-VMHardDiskDrive -VM $VM
#$VHDPath = (Get-VMHardDiskDrive -VM $VM).Path
$VHDClonePath = "$Path\$VMCloneName\Virtual Hard Disks\"
# Create a new array of VHD info
$VHDOrg = @()
foreach ($VHD in $VHDs){
$data = New-Object PSObject -property @{
VHDName = (Get-Item $VHD.Path).Name
VHDPath = $VHD.Path
ControllerType = $VHD.ControllerType
ControllerNumber = $VHD.ControllerNumber
ControllerLocation = $VHD.ControllerLocation
}
$VHDOrg +=$data
}
$VMNics = Get-VMNetworkAdapter -VM $VM
# Take snapshot
$VM | Checkpoint-VM
New-Item -ItemType directory -Path $VHDClonePath
New-VM -Name $VMCloneName -Path $Path -NoVHD -MemoryStartupBytes $VM.MemoryStartup -BootDevice IDE
$VMClone = Get-VM -Name $VMCloneName
Get-VMNetworkAdapter -VMName $VMCloneName | Remove-VMNetworkAdapter
# Configure VM-Clon
if ($VM.DynamicMemoryEnabled){
Set-VM -VMName $VMCloneName -ProcessorCount $VM.ProcessorCount -DynamicMemory -MemoryMinimumBytes $VM.MemoryMinimum -MemoryMaximumBytes $VM.MemoryMaximum
}else{
Set-VM -VMName $VMCloneName -ProcessorCount $VM.ProcessorCount -StaticMemory
}
# Add all network cards
foreach ($VMNic in $VMNics){
if ($Switch -eq " "){
Add-VMNetworkAdapter -VMName $VMCloneName -SwitchName $VMNic.SwitchName -IsLegacy $VMNic.IsLegacy
}else{
Add-VMNetworkAdapter -VMName $VMCloneName -SwitchName $Switch -IsLegacy $VMNic.IsLegacy
}
}
# Copy all VHDs
foreach ($VHDcopy in $VHDOrg){
$Dest = $VHDClonePath + $VHDcopy.VHDName
Copy-Item -Path $VHDcopy.VHDPath -Destination $Dest
Add-VMHardDiskDrive -VMName $VMCloneName -ControllerType $VHDCopy.ControllerType -ControllerLocation $VHDCopy.ControllerLocation -ControllerNumber $VHDCopy.ControllerNumber -Path $Dest
}
# Remove snapshot
$VM | Remove-VMSnapshot
}
Good luck in testing, but do take in consideration that this is an copy of the running VM so do not start it at the same time on the same network or you will get IP collision etc, as you can see above I have added an -Switch parameter that you can use to set the network cards of the VM to be connected on another switch to avoid any problems.. 🙂
A colleague wanted a script for a reconfiguration of a VM´s settings. Once the VM is powered off that is not a big issue. The smart thing is when using Powershell version 3 I can configure a scheduled job that I want to execute off hours.
Imagine that you have the task to add a vCPU to a VM but you can only do it during the service window that happens to be around midnight, I don´t know about you but I rather sleep then if I can automate it. Yes I could in earlier versions use scheduled task but now I am using Win 2012 and Hyper-V 3 and the latest powershell.
So what do I need to do then, Jan Egil Ring has made a blog post about scheduled jobs and I want to show an example regarding Virtual Machine management..
In Powershell version 3 there are 16 cmdlets regarding scheduled jobs
First I need a Trigger
$once = New-JobTrigger -Once -At 11:59PM
Then I need a job with the trigger, I could use a parameter -FilePath to use a script file instead of -ScriptBlock
And here you can see a screendump of the changing, Now I changed the trigger time to not have to wait to midnight for the blog post but I think you can imagine…
To check that the Job went ok, you run the cmdlet Get-Job ( and if you do not get any job you might need to run import-module PSScheduledJob to get the right Get-Job in that session)
As the buzz around twitter said that their result was showing I checked yesterday but on the Prometric site it just said “Tested”. When I woke up today I had to check again and now it looked better 🙂
As you can see I took the test at 4:30 PM on the first day of MMS 2012 so this means I am one of the first passing 🙂 , until someone else claims an earlier time I will say that I am the first MCSE : Private Cloud in Sweden 🙂
This as the requirement is MCSA and that I can count my 70-659 towards this certification 🙂
In the PowerCLI world there is a kind of evil oneliner to remove all VM´s from a datacenter, I was searching for something alike in the Hyper-V v3 world
And of course that is pure evil because the parameter -deletepermanently will remove not only the VM but also it´s files from the datastore, The % is to take care of each VM and in case the VM is running I will shut it down (otherwise if I only run like Get-VM | Stop-VM -Confirm:$false | Remove-VM -deletePermanently -Confirm:$false I will only remove the ones running, cause I will get an error on the others because I cannot change state to what it is already)
In Hyper-V and the powershell v3 It is not quite as easy but of course it can be done and don´t you dare use the gui 😉
This approach requires that you have put your VM in a separate folder for each of them (or of course it will be clean in your default Hyper-V VM folder..)
The Remove-VM cmdlet does not allow you to actually remove the VM´s virtual hard disk and the folders (which in some times can become a bit messy after a while)
Here is my VM´s
And here is my folder
So if I remove one VM now with the Remove-VM TestVM1, this also requires that the VM is off.
And lets see in the folders what do we have
So to clean both VM´s and folder data I run all at once and this looks like this
Today my adventures continues, I had to test and install the PowerCLI on a Windows 2012 RC to see if it works and also if it could be used in the new Windows 2012 feature PowerShell Webaccess.
In an earlier post I showed how to configure the PowerShell Webaccess, then I was not successful to run the PowerCLI, the difference here is that I now installed the PowerCLI on the Win 2012 server.
To be able to install i had to enable the .net 2.0 (why the PowerCLI is built on that version is another discussion we will not go into here), as you can see on the dialog I had to enable that
Ok, and that should be done with PowerShell or? My server did not have an active Internet connection so I got a failure when trying to enable the .Net Framework, I had to use the Dism tool with the install media to get the .Net installed as it otherwise downloads the files necessary from MS.
When this is enabled then there was no problem installing the PowerCLI and when installed I can use for example the V3 functions Get-VM | where powerstate -eq “PoweredOn” <- notice I am not using any curly brackets and $_
Now to the Webaccess, I connect to the site https://pstest.vniklas.com/pswa and log on with an authorized account. After that I run Add-PSSnapin VMware.VimAutomation.Core to enable the PowerCLI functionality . Then i need to get the credential before connecting to the vCenter because the pswa could not show the promt for credential.
But that is easily fixed by using $cred = Get-Credential and then Connect-VIServer vc.vniklas.com -Credential $cred 🙂 and as you can see I can now connect and use the PowerCLI in a web browser.
Now that the Windows 2012 RC has arrived with the Powershell v3 I wanted to explore the functionality a bit more, I have seen some posts about how to use the Workflows but none when deploying VM´s in Hyper-V.
I have loaned some code from Mikael Nyström (Deployment Bunny) but had to rewrite a little to make it work with the workflow -parallel.
Also when I downloaded Mikael´s scripts they where automatically blocked ( I had set my demo system to -Unrestricted) but as you see in the screenshot they are still blocked. but luckily I can use the Unblock-File cmdlet in the Powershell v3, when using the v2 there was a utility tool streams.exe from SysInternals that could help.
and here is it in the properties dialog
But if I have like 5-10 or 100 files I would like to use Powershell (Anyone out there unblocking like 100 scriptfiles by clicking in a dialog?)
To create the “Master” vhdx I used the Convert-WindowsImage.ps1 script, I am creating the master to be deployed with win 2012 RC datacenter core version (Updated:I got a tip in the comments to look at the Convert-WindowsImage instead of the Wim2VHD.ps1)
Now to my workflow, I am testing to create 5 VM´s at the same time with differential disks connected to the master. the workflows also set all VM´s to dynamic memory and starts them.
It is quite simple but still powerfull, If I for example had like more hosts i can deploy many VM´s at the same time and the thing that would stop me is the hardware and the storage 😛