Updated Add-SCNetworks function for virtual Networks in SCVMM 2012 SP1

Yesterday I described how I could get the networking info out from the VMM before a reinstall or if for another reason you need to get all networks defined.

In an earlier post I described my add networks script and now I have extended it a bit. I have this week made a function of it and also I have changed the import file format to fit the export-file as you get with the earlier post mentioned above. In the function I create a logic network if it is not already in place, that is also done for the network site in the logic network. The logic network created utilize VLAN.

So If you create or export a file with the following columns, Name, Subnet,VlanID you are going to success.

Screen Shot 2013-09-21 at 18.00.22

And when running the function:

Screen Shot 2013-09-21 at 18.05.13

One thing that I wanted also was the possibilty to create just one network with the script and that has also been added.

Screen Shot 2013-09-21 at 18.07.10

The function also handles if you already had setup a network with the same subnet and gives you a warning and do not create that net (maybe you accidentally try to add the same net twice and that is being handled)

Screen Shot 2013-09-21 at 18.11.55

Here is the function

# Add Networks to VMM 
#
# Niklas Åkerlund 2013-09-16
<#
.Synopsis
   Add networks to the logical network and creates virtual networks to them
.DESCRIPTION
   this function can take either a csv file as input for creating virtual networks
.EXAMPLE
   Add-SCNetworks -importfile .\networks.csv
.EXAMPLE
   Add-SCNetworks -Name DMZ -Subnet 192.168.10.0/24 -VlanID 100 -VMHostGroup DMZHosts
.Notes
Niklas Akerlund 2013-09-16
#>
function Add-SCNetworks
{
    [CmdletBinding()]
    Param
    (
        # Logical Network Name
        [Parameter(Position=0)]
        $LogicalNetworkName = "LumaNets",
        
        [Parameter(ValueFromPipelineByPropertyName=$true)]
        [Microsoft.SystemCenter.VirtualMachineManager.LogicalNetwork]
        $LogicalNetwork,

        # Network site name
        [string]
        $LogicalNetDefName="VMnets",
        
        # CSV file to be imported
        [string]
        $ImportFile = " ",

        # Name of the VM network
        $Name,
        
        $Subnet,
        
        $VlanId,
        
        $VMHostGroup= "All Hosts"

    )

    Process
    {
        
    if(!$LogicalNetwork){
        $LogicalNet = Get-SCLogicalNetwork -Name $LogicalNetworkName
        # If the logical network not exist, create it
        if(!$LogicalNet){
            $logicalNet = New-SCLogicalNetwork -Name $LogicalNetworkName -LogicalNetworkDefinitionIsolation $true -EnableNetworkVirtualization $false -UseGRE $false -IsPVLAN $false
        }
        Write-Verbose $LogicalNet.Name
    }else{
        $LogicalNet = $LogicalNetwork
    }

    $allSubnetVlan = @()

    # The networks that is going to be imported
    if ($ImportFile -ne " "){
        $vlans = import-csv $ImportFile -Delimiter ";"
       

        foreach ($vlan in $vlans) {
            # Check that the actual network not already is configured
            if(!((Get-SCLogicalNetworkDefinition).SubnetVLans.Subnet -contains $vlan.Subnet)){

                # First in fabric 
                $LogicalNetDef = Get-SCLogicalNetworkDefinition -LogicalNetwork $LogicalNet -Name $LogicalNetDefName
                $allSubnetVlan = $LogicalNetDef.SubnetVLans    
                $Subnet = $vlan.Subnet 
                Write-Verbose $Subnet
                $SubnetVlan = New-SCSubnetVLan -Subnet $Subnet -VLanID $vlan.VlanID
                $allSubnetVlan += $SubnetVLAN
                if($LogicalNetDef){
                    Set-SCLogicalNetworkDefinition -LogicalNetworkDefinition $LogicalNetDef -SubnetVLan $allSubnetVlan
                }else {
                    $LogicalNetDef= New-SCLogicalNetworkDefinition -LogicalNetwork $LogicalNet -Name $LogicalNetDefName -SubnetVLan $allSubnetVlan -VMHostGroup $VMHostGroup
                }

                # Then create VM Networks
                $vmNetwork = New-SCVMNetwork -Name $vlan.Name -LogicalNetwork $LogicalNet -IsolationType "VLANNetwork"
                #$vmSubnet = 
                New-SCVMSubnet -Name $vlan.Name -LogicalNetworkDefinition $LogicalNetDef -SubnetVLan $SubnetVLAN -VMNetwork $vmNetwork
            }else{
                Write-Host "Subnet $Subnet already exists" -ForegroundColor Red
            }
        }
    }else{
        # add only a single network
            if(!((Get-SCLogicalNetworkDefinition).SubnetVLans.Subnet -contains $Subnet)){
            
                # First in fabric 
                $LogicalNetDef = Get-SCLogicalNetworkDefinition -LogicalNetwork $LogicalNet -Name $LogicalNetDefName
                $allSubnetVlan = $LogicalNetDef.SubnetVLans    
                #$Subnet = $IPnet + $Octet
                Write-Verbose $Subnet
                $SubnetVlan = New-SCSubnetVLan -Subnet $Subnet -VLanID $vlanId
                $allSubnetVlan += $SubnetVLAN
                if($LogicalNetDef){
                    Set-SCLogicalNetworkDefinition -LogicalNetworkDefinition $LogicalNetDef -SubnetVLan $allSubnetVlan
                }else {
                    New-SCLogicalNetworkDefinition -LogicalNetwork $LogicalNet -Name $LogicalNetDefName -SubnetVLan $allSubnetVlan -VMHostGroup $VMHostGroup
                }

                # Then create VM Networks
                $vmNetwork = New-SCVMNetwork -Name $Name -LogicalNetwork $LogicalNet -IsolationType "VLANNetwork"
                #$vmSubnet = 
                New-SCVMSubnet -Name $Name -LogicalNetworkDefinition $LogicalNetDef -SubnetVLan $SubnetVLAN -VMNetwork $vmNetwork
            }else{
                Write-Host "Subnet $Subnet already exists" -ForegroundColor Red
            }
    
    }
    }
}

It works on both VMM 2012 SP1 and VMM 2012 R2 (preview)

I have looked at Alvin Morales latest blog post about creating networks and will see if I can add some more functionality into my code later on, I think that using parameters instead is a better way. There is always improvements that can be done and the only problem is the time 😉

Report of Logical Networks in SCVMM 2012 SP1

After working with an implementation and upgrade from SCVMM 2012 to SCVMM 2012 SP1 I have been trying to mitigate the issues with the old database and that with the network changes that arrived in SP1 has not been the best way.

Now I have decided to reinstall the VMM database to get a fresh start :-). Yes I lose some history but at least I get a system that will probably work!

I still want to get all the network information that I had in the old db to be able to create new virtual networks with the same network information

Here is an simple PowerShell script that helped me in getting the info

# Easy report of Logical Networks and subnet sites with vlans
#
# Niklas Akerlund

# Import Module
Import-Module virtualmachinemanager

# all Logical network definitions
$LogicalNetsdef = Get-SCLogicalNetworkDefinition

Foreach ($def in $LogicalNetsdef){
    
    foreach ($SubnetVlan in $def.SubnetVLans){
        $data=[ordered]@{
                    LogicalNetworkName = $def.LogicalNetwork.Name
                    Name=$def.Name
                    Subnet=$SubnetVlan.Subnet
                    VlanID=$SubnetVlan.VLanID
                }
         $Obj=New-Object -TypeName PSObject -Property $data  
         Write-Output $Obj   
    }

}

And when running it I pipe the info to a csv export

Screen Shot 2013-09-20 at 10.29.31

And here is the csv file

Screen Shot 2013-09-20 at 10.54.21

Configure updates for Microsoft products in Windows Update

I have installed a new Windows 2012 Server for System Center VMM SP1 and when I wanted to check what updates that were applicable I found a small issue in the Windows Update and that when configuring it for receiving more updates than just for Windows. After installing the System Center Virtual Machine Manager I definitely want the UR3 for example and that is distributed via Windows update.

By default the setting is as the following screen dump, only Windows updates are being analysed and considered!

Screen Shot 2013-09-17 at 10.44.14

 

And when pressing the link “Find out more” I get a web page as the following and here you can see that the active-x plugin fails to load

Screen Shot 2013-09-17 at 10.47.11

So to fix this I add the url to trusted sites on IE

Screen Shot 2013-09-17 at 10.47.57

And now when I reload the web page I can agree to the terms of use

Screen Shot 2013-09-17 at 10.48.16

And once that is completed I now get updates for more than just Windows on my server 🙂

Screen Shot 2013-09-17 at 10.48.47

Rescue Missing VM with unmerged snapshots on HyperV/VMM

Today I have been at a customer and helping them in a SC VMM issue where the VM went missing during a storage migration from one CSV volume to another. We are working on a project to upgrade this environment from Hyper-V 2008 R2 Sp1 to 2012 and VMM 2012 SP1 but this issue is from the VMM 2012.

felvmmsm

On the vm there were three avhd files from snaphots taken at different times and the files was quite big.. the virtual machine original vhd was 53 GB and the snapshot differencing disks where about 40 GB each. So quite a bit of data to merge. The customer removed the snapshots but was a bit too eager to start the move so the vm had not merged the snapshots files before they started the migration.

During the migration the VMM server lost connection for a very short moment to the host and the result was that the vm got missing in the VMM console and also in the Hyper-V manager. The job had removed the VM´s configuration file but not moved it to the new location so what was left in the virtual machine folder was just the vhd and avhd files.

To find which avhd file that was the latest and find the snapshot-chain we used the “inspect disk”  and “inspect parent” in Hyper-V manager, in Win 2008 R2 Sp1 it does not show the avhd file by default so you have to copy and paste the file name and then it works. As the files was in a merge process it was not obvious which file was the latest, in a normal case it is quite obvious to see in the date properties of the avhd files.

Screen Shot 2013-08-26 at 11.46.08

When we found which was the right avhd file we created a new VM and attached the avhd file, same procedure as with the inspect disk, copy and paste the file name in the field. If you would take the wrong avhd file and start the VM the differencing chain would break and all changes in the files after would be lost!

Now We could start the VM and verify that we got all data until the latest snapshot and that the customers user not had lost any data! As it now was a new VM with a new network card we had to enter the IP settings on that network connection.

The easiest way we thought of to get the VM to merge the avhd´s was to use the “Clone” feature in the VMM console.

Screen Shot 2013-08-26 at 13.09.12

And when the clone of the VM was finished that job had also merged the disks and the virtual machine started without any errors and avhd files 🙂

I have made two blog posts a while ago about checking if a vm have snapshots before migrating it to another cluster and also a post about how you can extend the console with info on what virtual machines have snapshots to easily have control!

I have said it before and will emphasize it again, do not use snapshots as backup solution and remove them as soon as you are done as they do impact on performance !

I look forward to when we have the environment on hyper-v 2012 where the live-merge feature exist and also later the R2 feature with the live clone!

Download all System Center R2 eval VHD with PowerShell

I have already blogged about how to use PowerShell to download all of the System Center 2012 SP1 VHD evaluation files with PowerShell and BITS.

As Microsoft has released an updated version with Windows 2012 R2 and System Center 2012 R2 Preview I wanted to give you the script with updated url´s so you can easily download and try out the different roles! Note though that the Configuration Manager evaluation VHD has not yet been released, I still have the link in the script so when it will be available you can download it! They have also changed the packages to .bin instead of .rar.

Make sure that you have enough space on your volume and then run the following script and you will within a time that of course depends on your internet connection have all files necessary to start evaluate. I will update the script with the right size of R2 once I have downloaded all but think that 70 GB will be enough at lest now when SCCM is not available 🙂

# Download all SC 2012 R2 Preview VHD Eval
#
# Niklas Akerlund / Lumagate 2013-07-10

# Downloadfolder
$Dest = "D:\VHDEVALR2"
$SizeOfSCR2 = 70GB

# Get Space on volume
$SpaceRemaining = (Get-Volume $Dest.Split(":")[0]).SizeRemaining

if($SpaceRemaining -gt $SizeOfSCR2){
# SCVMM
((Invoke-WebRequest -Uri "http://www.microsoft.com/en-us/download/confirmation.aspx?id=34803").links | ? href -match "rar$|exe$|docx$|bin$").href | %{Start-BitsTransfer -Source $_ -Destination $Dest}
# SCOM
((Invoke-WebRequest -Uri "http://www.microsoft.com/en-us/download/confirmation.aspx?id=34780").links | ? href -match "rar$|exe$|docx$|bin$").href | %{Start-BitsTransfer -Source $_ -Destination $Dest}
# SCORCH
((Invoke-WebRequest -Uri "http://www.microsoft.com/en-us/download/confirmation.aspx?id=34778").links | ? href -match "rar$|exe$|docx$|bin$").href | %{Start-BitsTransfer -Source $_ -Destination $Dest}
# SCAC
((Invoke-WebRequest -Uri "http://www.microsoft.com/en-us/download/confirmation.aspx?id=39369").links | ? href -match "rar$|exe$|docx$|bin$").href | %{Start-BitsTransfer -Source $_ -Destination $Dest}
# SCDPM
((Invoke-WebRequest -Uri "http://www.microsoft.com/en-us/download/confirmation.aspx?id=34779").links | ? href -match "rar$|exe$|docx$|bin$").href | %{Start-BitsTransfer -Source $_ -Destination $Dest}
# SCCM
((Invoke-WebRequest -Uri "http://www.microsoft.com/en-us/download/confirmation.aspx?id=39368").links | ? href -match "rar$|exe$|docx$|bin$").href | %{Start-BitsTransfer -Source $_ -Destination $Dest}
# SCSM
((Invoke-WebRequest -Uri "http://www.microsoft.com/en-us/download/confirmation.aspx?id=34777").links | ? href -match "rar$|exe$|docx$|bin$").href | %{Start-BitsTransfer -Source $_ -Destination $Dest}
# SCSM DW
((Invoke-WebRequest -Uri "http://www.microsoft.com/en-us/download/confirmation.aspx?id=39367").links | ? href -match "rar$|exe$|docx$|bin$").href | %{Start-BitsTransfer -Source $_ -Destination $Dest}
}else{
[int]$Sum = ($SizeOfSCR2 - $SpaceRemaining)/1GB
Write-Host "Free up at least $Sum GB and try again!"
}

Start downloading and explore while your vacation rains away (actually the first day it rains here for some weeks)!

Adding several networks to your VMM 2012 SP1 with PowerShell

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.17
Screen 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

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

backupschedule
$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”

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

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.

notapplicablewmf3

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,

install1wmf3

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 🙂

hoststatuswmf3

Manage your Clouds with PowerShell ppt and demofiles

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

manageclouds


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 🙂

 

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

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.

sethighavailpshv
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.

sethighavailvmm

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

isavailablegui

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

movevmworks

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.