Windows PowerShell and Desired State Configuration HOL

June 12th, 2013 No comments

On Teched 2013 there has been a massive announcement about the future of Windows 2012 R2 and System Center 2012 R2 and there was an session with Jeffrey Snover and Kenneth Hansen about Desired State Configuration DSC and how that works,

Now the Hands-On Lab has been released for DSC and accessible for everyone, this also means that you can access the other parts of PowerShell v4.

Screen Shot 2013-06-12 at 13.05.35

Here is a link to the HOL-310

I have done the lab and it is quite impressive how simple and powerfull it is and being able to keep a desired state of installed/configurered servers with a service on them.

As I was in the lab environment I could not resist to check if there was any new powershell cmdlets for the Hyper-V module in R2

Screen Shot 2013-06-12 at 13.15.44

And with this command you can see that there is 14 new cmdlets in the R2 version of the hyper-v module compared to the 2012 version.

Screen Shot 2013-06-12 at 13.13.01

Here is a list of all the HOL that is available online and probably there will be more added later on.

Categories: Hyper-V, Powershell, Virtualization, Win2012 Tags:

Adding several networks to your VMM 2012 SP1 with PowerShell

June 10th, 2013 No comments

I have created an environment at a customer with bare metal deployment with Hyper-V 2012 and SC VMM 2012 SP1 and in the bare metal profile I am utilizing the logical switches to get a consistent configuration on all hosts and not needing to configure each host every time a new network is going to be set up.

In an earlier post I made a script for adding networks into VMM 2012 but now in SP1 we have logical switches and now also a VM network, the later that adds a bit of complexity in the adding process.

In this case we are using VLAN´s and I want to be able to configure and add them without going through the console and all dialogs in the GUI

To get VLAN properly working you also need to check a box in the logical network properties

Screen Shot 2013-06-10 at 22.23.35

So this script asumes that you have a Logical Network already defined and what we do here is adding the subnets in the network sites. If someone finds another way to why I have to add all the subnetVLan objects every time I update the Logical Network Definition, that would be super.. If I do not add all the objects the networks without dependencies are removed.. not so smart

As you can see on this error message when I just try to update with a new Subnet VLAN and not including the other VLAN objects the Set-SCLogicalNetworkDefinition tries to remove and as there is already an VM network it fails. If you look at the script from the gui wizard you will see and understand what I am fuzzing about.

Screen Shot 2013-06-10 at 22.41.01

The structure of the CSV file is as the next screendump and If you already have an excel document or some other information table you could easily change the script to suit your environment and deployment.

Screen Shot 2013-06-10 at 22.34.42

And when running the following PowerShell script it will create not only the subnets in the Network Site but also the VM Networks. Here is the logical network before running the script

Screen Shot 2013-06-10 at 22.57.23

And here is after

Screen Shot 2013-06-10 at 23.02.18

And the VM network, as you can see on the details on the ADM-Servers details it is connected to the VM Subnet with the VLAN 399

Screen Shot 2013-06-10 at 23.03.17Screen Shot 2013-06-10 at 23.03.33

And here is the script:

# Add Networks to VMM 
# Niklas Åkerlund 2013-06-10
$LogicalNetName = "VMNET"
$LogicalNetDefName = "DevNets"
$ImportFile = "C:\PowerShell\networkvms.csv"

# The networks that is going to be imported
$vlans = import-csv $ImportFile -Delimiter ";"
$LogicalNet = Get-SCLogicalNetwork -Name $LogicalNetName

$allSubnetVlan = @()
foreach ($vlan in $vlans) {
    # First in fabric 
    $LogicalNetDef = Get-SCLogicalNetworkDefinition -LogicalNetwork $LogicalNet -Name $LogicalNetDefName
    $allSubnetVlan = $LogicalNetDef.SubnetVLans    
    $Subnet = $vlan.IPnet + $vlan.Octet
    $SubnetVlan = New-SCSubnetVLan -Subnet $Subnet -VLanID $vlan.VLAN
    $allSubnetVlan += $SubnetVLAN
    Set-SCLogicalNetworkDefinition -LogicalNetworkDefinition $LogicalNetDef -SubnetVLan $allSubnetVlan

    # VM Networks
    $vmNetwork = New-SCVMNetwork -Name $vlan.Name -LogicalNetwork $LogicalNet -IsolationType "VLANNetwork"
    New-SCVMSubnet -Name $vlan.Name -LogicalNetworkDefinition $LogicalNetDef -SubnetVLan $SubnetVLAN -VMNetwork $vmNetwork


There are some things that I am working on the next version of this simple script, I am going to make it as a function and also some error checking and also verify if the networks already exists or not, that will be posted in another article :-)

SCVMM DB Backup and cleaning with PowerShell

June 6th, 2013 No comments

I have been working on some Virtual Machine Manager deployment and configuring at a customer and after some configuration and misstakes a backup would have been nice. There is a PowerShell cmdlet in the VMM and what it does is to create a DB dump on a path that you specify. Remembering to do this every day that you work with the environment so you can recover is not always so easy and it is often that just after you make a misstake or the system does it for you, the need for backup arrises.

So setting it up with a scheduled job in PowerShell is the way to go, and the script I have also removes the backups after 7 days.

here is the simple script that does the backup and cleaning

# Backup VMM Server
# Schedule with either PSScheduledJobs or Task manager
# Niklas Akerlund 2013-06-04
$backupPath = "C:\temp"

ipmo virtualmachinemanager
Backup-SCVMMServer -Path $backupPath

get-item -Path "$backupPath\*" | where {$_.LastWriteTime -lt (get-date).AddDays(-7) -and $_.Name -match "bak"} | Remove-Item

And here is the scheduling that is done with PS Scheduling


$cred = Get-Credential
$dailybackup = New-JobTrigger -Daily -At 10:45PM
Register-ScheduledJob -Name "VMM Backup" -FilePath C:\PowerShell\backupVMM.ps1 -Trigger $dailybackup -Credential $cred

This way you at least have a backup once a day to get you to recover, and talking of recover, if you want to recover the database for the VMM you can use the binary SCVMMRecover.exe and the parameter -Path. You can find the SCVMMRecover in the following path if the VMM is installed with default settings,  “C:\Program Files\Microsoft System Center 2012\Virtual Machine Manager\bin”

vExpert 2013

May 29th, 2013 3 comments


This year I got the vExpert award again, this is the second year I get the title and feel honored to be part of the program :-)

I have two former colleagues from Sweden and my time at Real Time Services that also got the title this year and they are serious mega-experts :-)

Thank you John Troyer and all other involved in the vExpert program!

You can find the rest of the 580 vExperts on this link.

Categories: General, Virtualization, VMware Tags:

SysCtr PDT deployment session on PSUG Sweden

May 24th, 2013 No comments

Yesterday I was at the Swedish PowerShell User Group Community Day and I had a session about the System Center and deployment with PowerShell and talked about the PowerShell Deployment Toolkit.

Here is a screendump where I have installed VMM, AC,ORCH,SM,DPM,OM and also as you can see the automatic creation of shortcuts both on the desktop and the taskbar on the server that I have configured for console. As the PDT is designed to not kill itself I had to restart the deployment after rebooting the HV03 (the reason for this was the installation of .Net 3.51 which required the reboot) which was also the server running the deployment and the last part took 9 minutes. In a youtube video you can see how the deployment takes about an hour with the whole system center suite of roles and integrations between them, quite amazing!


One thing that is wrong in the package of PDT is the size of the so when the installer.ps1 is validating the size of the files the validation fails. This mp is not wrong it is just updated. Edit the workflow.xml file and set the right size and you should be fine.

Screen Shot 2013-05-24 at 13.17.00

In the workflow.xml file find the line and update the size to the correct value

Screen Shot 2013-05-24 at 13.45.24

I have created a small scripted file that creates all the AD accounts in the PDT as this is not included. This also create an OU that it puts all objects in so it will be more manageable and easy to find.

# Add all Accounts nesssesary for SC Deploy with PDT
# Niklas Akerlund

# Create OU´s for groups, users and Server objects
$OU = New-ADOrganizationalUnit -Name SCPDT -PassThru
$SAccounts = "installer","vmm","or","spf","ac","om_saa","om_das","om_dra","om_dwa","sm_s","sm_w","sm_r","sm_a","sm_p"

foreach ($Account in $SAccounts){
    #Account creation  
    New-ADUser -Name $Account -SamAccountName $Account -AccountPassword (convertto-securestring -string "LUMA15gate" -asplaintext -force) -PasswordNeverExpires $true -Enabled $true -path $OU
    get-adgroup "domain admins" | Add-ADGroupMember -Members (Get-ADUser $Account)

# rest of admin groups and stuff
New-ADGroup -Name SPFAdmins -path $OU -GroupScope Global -GroupCategory Security
New-ADGroup -Name SM_PDT -path $OU -GroupScope Global -GroupCategory Security
New-ADGroup -Name DW_PDT -path $OU -GroupScope Global -GroupCategory Security
New-ADGroup -Name SMAdmins -path $OU -GroupScope Global -GroupCategory Security
New-ADGroup -Name SQLAdmins -path $OU -GroupScope Global -GroupCategory Security
Get-ADGroup "SQLAdmins" | Add-ADGroupMember -Members (Get-ADgroup "domain Admins")

Adding 2016 subnets to AD Sites and Services

May 14th, 2013 No comments

In a recent post I showed how to configure the DHCP server with multiple scopes of subnets with PowerShell and how blazing fast that could be done in the Windows 2012 compared to Windows 2008 R2.

One thing we might forget is to populate the Active Directory Sites and Services subnets with this information to get the clients to access the domain controllers that is nearest. As you can read in this technet post the reason for populating this:

“Domain controllers register service (SRV) resource records in Domain Name System (DNS) that identify their site names. Domain controllers also register host (A) resource records in DNS that identify their IP addresses. When a client requests a domain controller, it provides its site name to DNS. DNS uses the site name to locate a domain controller in that site (or in the next closest site to the client). DNS then provides the IP address of the domain controller to the client for the purpose of connecting to the domain controller. For this reason, it is important to ensure that the IP address that you assign to a domain controller maps to a subnet that is associated with the site of the respective server object. Otherwise, when a client requests a domain controller, the IP address that is returned might be the IP address of a domain controller in a distant site. When a client connects to a distant site, the result can be slow performance and unnecessary traffic on expensive WAN links.”

So as we do not want to update 2016 subnets manually we do this with PowerShell instead, and as you can see with the Windows 2012 it goes quite fast (I have tested on Win 2008 R2 and it works there also).


and here is the small PowerShell bits that do the magic

# Populate Subnets in AD Sites and Services
# Niklas Akerlund 2013-05-14 

for($b=1;$b -le 63 ; $b++){
    for($i=0;$i -le 255){ 
        $Name = "10.10.$b.$i" + "/29"
        $Description = "10.10.$b.$i" + "/"
        New-ADObject -Name $Name -Type subnet -Description $Description -OtherAttributes @{location="RemoteVPN";siteObject="CN=HQ,CN=Sites,CN=Configuration,DC=lumademo,DC=local"} -Path "CN=Subnets,CN=Sites,CN=Configuration,DC=lumademo,DC=local"     
        $i = $i +8

And then when you look in the Active Directory Sites and Services you will find all the subnets and that they correlate to the right site.

Categories: Powershell, Win2012 Tags:

Upgrading to WMF 3.0 on Win 2008 R2 SP1 Hyper-V Core

April 26th, 2013 No comments

If you still have some Windows 2008 R2 SP1 Core Hyper-V hosts running and thinking of upgrading to WMF 3.0 there has been some problems with the VMM 2012.

I have a Win 2008 R2 Core SP1 Hyper-V  in my VMM 2012 SP1 UR2 and wanted to test if this was still an issue. The main reason for upgrading to the WMF 3.0 is the rebuild of the vmm refresh that is described in this blog post.

First I just tried to run the install file for WMF 3.0 and got the following dialog, and after reading a bit more realizing that it was because I need the .Net 4 as a prerequisite.


I installed the .Net 4 and there are some things that need to be configured on the Core server before and that is explained on the download page for .Net for Win 2008 R2 SP1 Core

After that It worked to install,


And after rebooting the host I looked in VMM and also tried some refresh and start/stop on VM´s and setting the host in maintenance mode, everything worked without any issues :-)


Categories: Hyper-V, Powershell, SCVMM, Virtualization Tags:

Adding 2016 DHCP Scopes with PowerShell in Windows 2012

April 24th, 2013 No comments

I have had a lot to do and that is why I have not done so much updating on the blog, soon my third article will be published on Petri IT Knowledge :-) I have also attended the MMS in Las Vegas where I got loads of good information and meet lots of people!

In this post I want to show you the improvements in Windows 2012 and with the PowerShell native cmdlets in this version.

The task was to automatically create 2016 DHCP Scopes for clients on a DCHP server and every scope was a /29 net with 3 lease addresses and one router for each.

I tested this on a Windows 2008 R2 with the community powershell module  and with the following script it took, and I am not kidding, almost 5 hours!! the module utilise the net sh commands with dhcp and probably there could be a lot of improvements for performance, and seriously, just creating one or two scopes can be done in the gui but doing over 2000 will demand a scripted way!

Screen Shot 2013-04-24 at 19.28.45

# Massive DHCP Scope 2016
# Niklas Akerlund 2013-04-24

write-host (get-date)
for($b=1;$b -le 63 ; $b++){
    for($i=0;$i -le 255){ 
        New-DHCPScope -Server localhost -Name 10.10.$b.$i -Address 10.10.$b.$i -SubnetMask | Out-Null
        $startip = $i+2
        $endip = $i+4
        $router = $i+1
        Add-DHCPIPRange -Server localhost -Scope 10.10.$b.$i -StartAddress 10.10.$b.$startip -EndAddress 10.10.$b.$endip | Out-Null
        Get-DHCPScope -Server localhost -Scope 10.10.$b.$i | Set-DHCPOption -OptionID  003 -DataType IPADDRESS -Value 10.10.$b.$router | Out-Null
        $i = $i +8
write-host (get-date)

With the PowerShell DHCP module for WIndows 2012 I have created the following script for creating the same amount of scopes and configuration and running it took only about 1 minute!!

Screen Shot 2013-04-24 at 20.02.35

# Massive DHCP Scope 2016
# Niklas Akerlund 2013-04-24

write-host (get-date)
for($b=1;$b -le 63 ; $b++){
    for($i=0;$i -le 255){ 
        $startip = $i+2
        $endip = $i+4
        $router = $i+1
        Add-DhcpServerv4Scope -Name 10.10.$b.$i -StartRange 10.10.$b.$startip -EndRange 10.10.$b.$endip -SubnetMask
        Set-DhcpServerv4OptionValue -ScopeId 10.10.$b.$i -Router  10.10.$b.$router
        $i = $i +8
write-host (get-date)

As you can see, handling lots of changes and configurations can easily be done in the new Windows 2012 and it´s native cmdlets!

Categories: Powershell, Win2012 Tags:

Manage your Clouds with PowerShell ppt and demofiles

April 4th, 2013 No comments

Today I was presenting on the Technet Sweden Live meeting:System Center Sp1 Springtime series and my session was about:


The presentation can be downloaded on the following link -> SC2012SP1VarenPSClouds and I have also added it to slideshare

I also promised that the powershell files I have created for the demonstrations would be downloadable and here they are, I have made one file for each of the cloud management parts

You can also read some of my other posts about App controller and how to add clouds to it, also the post about how to extend your VMM console with custom properties and also how to download and update help in SCVMM.

I will try to get some time to make some demo movies and publish them in a near future :-)


Categories: Powershell, SCVMM, Virtualization Tags:

Not able to set HA on a running VM with SC VMM 2012 SP1

April 3rd, 2013 No comments

I have described before in blog posts about how to add a running VM to a Hyper-V failover cluster with the PowerShell cmdlets without moving it or turning it off, asuming it already was on a highly available storage such as a CVFS volume or smb 3.0 file share

In my exploration of the virtual machine manager PowerShell module I wanted to test and see if this was also possible in there but I do not succeed, or more correctly maybe should be noted that the SCVMM team has not implemented that for this release :-( what has been implemented is the possibility to make a VM highly available when moving between datastores on a cluster node or from a single hyper-v host to a cluster node. As there is some copying of data it will also take longer time than the below command.

In Hyper-V with failover cluster PowerShell module I can add a VM to the cluster with the following cmdlets without stopping the VM.


Hyper-V\Get-VM vmtest -ComputerName HV02 | Add-VMToCluster -Cluster HVCL30

As you see, the reason why I have added the Hyper-V to the cmdlet is that because in the VMM PowerShell cmdlet there is an alias that is called Get-VM and refers to the Get-SCVirtualMachine and that conflicts with the Hyper-V PowerShell module´s Get-VM and I have imported both modules in the same console. I have also imported the failover cluster module in the same console.

If I try to use the Set-SCVirtualmachine vmtest -HighlyAvailable $true I get the following error.

sethighavailvmmIn the VMM Console this option is also greyed out and cannot be set.


Here is the command to move the VM from one datastore to another and in the same time making it highly available,


I have made a blog post about the issue with Move-SCVirtualMachine not updating the cluster resource and this can be a way to handle this but in my opinion a far more complicated way as you have to move the VM to a new datastore and removing the high availability and then moving it again to another datastore and making it high available again, with a large virtual machine this can take some considerable time when it otherwise just is to run the cluster cmdlet.

Categories: Hyper-V, SCVMM, Virtualization, Win2012 Tags: