2013-03-04

Easy generation of configuration file for Pools with multiple network labels


One of the new features of VMware Horizon View 5.2 allows you to create pools with different portgroups or network labels.

As part of the configuration process you have to generate a configuration file, and for that you need to run a command with lots of parameters.

I found a way to generate most of the difficult parameters, so you don't spend half an hour (or more) trying different combinations of parameters.

All the info below.


All you have to do is to copy & paste the function below in a "View PowerCLI" console (not the same as "PowerCLI" alone) and then run the command GenerateNetworkConfFile

I include some execution examples below.

If you have issues or suggestions please submit a comment.


#====== SOURCE CODE ============================================================================

function GenerateNetworkConfFile {

# Copyright: Ruben Miguelez Garcia @ 2013 -> http://vmutils.blogspot.com

# Log in to VC if not already done
echo ""
$loggedinvc = read-host -Prompt "Have you already logged into the vCenter used by View using the command Connect-VIServer ?  [y]/n"
if ($loggedinvc -eq "n"){ Connect-VIServer };

# Request a few details
echo ""
$vmname= read-host -Prompt "What is the name of the VM you want to generate a network configuration file for?"


echo "";
$MaxVMsPerNetworkLabel= read-host -Prompt "How many IPs do you want to set up as default limit per network label? [240]"
if ($MaxVMsPerNetworkLabel -eq "") { $MaxVMsPerNetworkLabel="240"; }


# The two functions below come from the View PowerCLI documentation (Integration guide)
# The 1st function has a small modification
#------------------------------------------
function Get-SnapshotPath {
 param(
  $Snapshot
 )
 PROCESS {
  if ($Snapshot) {
   $SnapPath = $Snapshot.Name
   do {
    $SnapPath = "$($snapshot.Parent.Name)/" + $SnapPath
    $Snapshot = $Snapshot.Parentsnapshot
   } while ($Snapshot.Parent -ne $null)

   #$SnapPath = "/" + $SnapPath
   if ($SnapPath[0] -ne "/") { $SnapPath = "/" + $SnapPath }
   $SnapPath
  } Else {
   Write-Error "Snapshot not found"
  }
 }
}
#------------------------------------------
function VVGetPath($InvObject){
    if($InvObject){
 
        $objectType = $InvObject.GetType().Name
        $objectBaseType = $InvObject.GetType().BaseType.Name
        if($objectType.Contains("DatastoreImpl")){
            Write-Error "Use the VVGetDataStorePath function to determine datastore paths."
            break
        }
        if(-not ($objectBaseType.Contains("InventoryItemImpl") -or $objectBaseType.Contains("FolderImpl") -or $objectBaseType.Contains("DatacenterImpl") -or $objectBaseType.Contains("VMHostImpl") ) ){
            Write-Error ("The provided object is not an expected vSphere object type. Object type is " + $objectType)
            break
        }
 
        $path = ""
        # Recursively move up through the inventory hierarchy by parent or folder.
        if($InvObject.ParentId){
            $path = VVGetPath(Get-Inventory -Id $InvObject.ParentId)
        } elseif ($InvObject.FolderId){
            $path = VVGetPath(Get-Folder -Id $InvObject.FolderId)
        }
 
        # Build the path, omitting the "Datacenters" folder at the root.
        if(-not $InvObject.isChildTypeDatacenter){ # Add object to the path.
            $path = $path + "/" + $InvObject.Name
        }
        $path
    }
}
#------------------------------------------


# Get the rest of the details needed automatically
$vm=get-vm -name $vmname
$vc_id=(Get-ViewVC).vc_id ;
$ClusterPath =   "/" + (Get-Datacenter -vm $vm ).name + "/host/" + (get-cluster -vm $vm)
$ParentVmPath = vvgetpath($vm) ;

# Use [0] to get the name of 1st snapshot, [1] for the 2nd, and so on. [-1] for the last one.
# This may not work well if there are multiple snapshot branches.
# The if/else is there to account for cases where there is 1 or multiple snapshots
# If only one, we take that, if multiple, we take the last one
$snaps=$vm | get-snapshot
if ($snaps.gettype().tostring() -eq "VMware.VimAutomation.ViCore.Impl.V1.VM.SnapshotImpl")
 { $ParentSnapshotPath= Get-SnapshotPath ($snaps) }
 else { $ParentSnapshotPath= Get-SnapshotPath ($snaps)[-1] }



# The file will be placed on C:\
$NetworkLabelConfigFile= "C:\" + $vm.name + "NetSpec.txt" ;

# Display what we got  and verify they have valid values
echo ""
echo "Here are the details collected:"
echo "vc_id = $vc_id" ; # Example value: e53c44c5-d5a6-43ec-bab4-f91f7f3fa91e
echo "ClusterPath = $ClusterPath" ; # Example value: /Training/host/ViewCluster
echo "ParentVmPath=$ParentVmPath "; # Example value: /Training/vm/Windows8
echo "ParentSnapshotPath=$ParentSnapshotPath "; # Example value: /Base1/With License/With perf improv/end
echo "NetworkLabelConfigFile=$NetworkLabelConfigFile "; # Example value: C:\Windows8NetSpec.txt


# Generate the configuration file
Export-NetworkLabelSpecForLinkedClone `
-Vc_id $vc_id `
-ClusterPath $ClusterPath `
-ParentVmPath $ParentVmPath `
-ParentSnapshotPath $ParentSnapshotPath `
-MaxVMsPerNetworkLabel $MaxVMsPerNetworkLabel `
-NetworkLabelConfigFile $NetworkLabelConfigFile

}
# End of GenerateNetworkConfFile


#====== USAGE EXAMPLES ============================================================================



######### Using default values in 2 questions

> GenerateNetworkConfFile

Have you already logged into the vCenter used by View using the command Connect-VIServer ?  [y]/n:

What is the name of the VM you want to generate a network configuration file for?: Windows8_empty


How many IPs do you want to set up as default limit per network label? [240]:


Here are the details collected:
vc_id = e53c44c5-d5a6-43ec-bab4-f91f7f3fa91e
ClusterPath = /Training/host/ViewCluster
ParentVmPath=/Training/vm/Windows8_empty
ParentSnapshotPath=/Base Line
NetworkLabelConfigFile=C:\Windows8_emptyNetSpec.txt




######## With problem because VM has no snapshots


PS C:\Users\training> GenerateNetworkConfFile

Have you already logged into the vCenter used by View using the command Connect-VIServer ?  [y]/n: n

cmdlet Connect-VIServer at command pipeline position 1
Supply values for the following parameters:
Server[0]: vc.company.com
Server[1]:

Name                           Port  User
----                           ----  ----
vc.company.com           443   admin

What is the name of the VM you want to generate a network configuration file for?: VM_HW_v9


How many IPs do you want to set up as default limit per network label? [240]:

You cannot call a method on a null-valued expression.
At line:74 char:19
+ if ($snaps.gettype <<<< ().tostring() -eq "VMware.VimAutomation.ViCore.Impl.V1.VM.SnapshotImpl")
    + CategoryInfo          : InvalidOperation: (gettype:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull


Here are the details collected:
vc_id = e53c44c5-d5a6-43ec-bab4-f91f7f3fa91e
ClusterPath = /Training/host/ViewCluster
ParentVmPath=/Training/vm/VM_HW_v9
ParentSnapshotPath=/snap1/snap2
NetworkLabelConfigFile=C:\VM_HW_v9NetSpec.txt
Export-NetworkLabelSpecForLinkedClone : PowershellService::ExportNetworkLabelSpecForLinkedClone FAILED, error=No NIC found f
or base VM: /Training/vm/VM_HW_v9 and snapshot: /snap1/snap2
At line:88 char:38
+ Export-NetworkLabelSpecForLinkedClone <<<<  `
    + CategoryInfo          : InvalidResult: (vmware.view.pow...cForLinkedClone:ExportNetworkLabelSpecForLinkedClone) [Expo
   rt-NetworkLabelSpecForLinkedClone], Exception
    + FullyQualifiedErrorId : PowershellService::ExportNetworkLabelSpecForLinkedClone FAILED,vmware.view.powershell.cmdlets
   .ExportNetworkLabelSpecForLinkedClone



############ Another example

> GenerateNetworkConfFile

Have you already logged into the vCenter used by View using the command Connect-VIServer ?  [y]/n:

What is the name of the VM you want to generate a network configuration file for?: Windows7


How many IPs do you want to set up as default limit per network label? [240]: 999


Here are the details collected:
vc_id = e53c44c5-d5a6-43ec-bab4-f91f7f3fa91e
ClusterPath = /Training/host/ViewCluster
ParentVmPath=/Training/vm/Windows7
ParentSnapshotPath=/Base Line
NetworkLabelConfigFile=C:\Windows7NetSpec.txt




################ Checking the output file from console


> cat C:\XP_tiny_emptyNetSpec.txt
#Network Label Configuration Spec

#WARNING! Setting enabled flag to false will
#turn off the automatic network label assignment
#for newly provisioned desktops.
enabled=true

#Parameter Definition for NIC
nic1=Network adapter 1

#Parameter Definition for Network
network01=VDI_Net1
network02=VDI_Net2
network03=VDI_Net3
network04=VM Network
network05=VM Network 2

#Network Label Attribute Definition
#Expected format:
#..maxvm=

####nic1.network01.maxvm=240
####nic1.network02.maxvm=240
####nic1.network03.maxvm=240
####nic1.network04.maxvm=240
####nic1.network05.maxvm=240

No comments:

Post a Comment