Get HBA WWN for Hosts using Powercli

Have a need to get the WWN of all your HBA’s to present storage?  Here is a little one liner that can assist with that and will convert it to hex as well! The single command below will not give you pretty colons in the WWN output though. To do that, see my function below to get a nice spreadsheet. You can pipe any object like Get-Datacenter or Get-Cluster to the command below to specify what hosts you need specifically:

#Get WWN for cluster 
Get-Cluster clustername | Get-VMhost | Get-VMHostHBA -Type FibreChannel | Select VMHost,Device,@{N="WWN";E={"{0:X}" -f $_.PortWorldWideName}} | Sort VMhost,Device

#Get WWN by single host 
Get-VMhost -Name Host | Get-VMHostHBA-Type FibreChannel | Select VMHost,Device,@{N="WWN";E={"{0:X}" -f $_.PortWorldWideName}} | Sort VMhost,Device

I also created a script to gather based on cluster, will format the output to include colons, and output to csv name you specify.

#Set mandatory parameters for cluster and csvname
param (
[Parameter(Mandatory=$true)][string]$Cluster,
[Parameter(Mandatory=$true)][string]$CSVName
)

#Get cluster and all host HBA information and change format from Binary to hex
$list = Get-cluster $cluster | Get-VMhost | Get-VMHostHBA -Type FibreChannel | Select VMHost,Device,@{N="WWN";E={"{0:X}" -f $_.PortWorldWideName}} | Sort VMhost,Device

#Go through each row and put : between every 2 digits
foreach ($item in $list){
   $item.wwn = (&{for ($i=0;$i -lt $item.wwn.length;$i+=2)   
                    {     
                        $item.wwn.substring($i,2)   
                    }}) -join':' 
}

#Output CSV to current directory.
$list | export-csv -NoTypeInformation $CSVName

Finally, here is a function where you can pass a datacenter or cluster object and enter a csv file name.

Usage:

Get-Datacenter | Get-WWN -csv filename
function Get-WWN {    
    #Set mandatory parameters for cluster and csvnameparam 
    ([CmdletBinding()]
    [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelinebyPropertyName=$True)]
    $VMObject,
    [Parameter(Mandatory=$true)]
    [string]$csv
    )

    #Get cluster and all host HBA information and change format from Binary to hex
$list = $VMObject | Get-VMhost | Get-VMHostHBA -Type FibreChannel | Select VMHost,Device,@{N="WWN";E={"{0:X}" -f $_.PortWorldWideName}} | Sort VMhost,Device

    #Go through each row and put : between every 2 digits
foreach ($item in $list){
       $item.wwn = (&{for ($i=0;$i -lt $item.wwn.length;$i+=2)   
                        {     
                            $item.wwn.substring($i,2)   
                        }}) -join':' 
    }

    #Output CSV to current directory.
$list | export-csv -NoTypeInformation $csv.csv

}
Advertisements

Migrate VMkernel Ports from 1000v or VDS to Standard Switch using Powercli

*UPDATE I HAVE UPDATED THIS WITH THE NEW POWERCLI 5.5 cmdlets here:*
Migrate Host and Guests VDS to SS using Powercli 5.5

On occasion, we have a need to be able to remove an ESXi host from our distributed switch to a standard switch. In order to do this you can go through the GUI and choose “Migrate” adapters shown in the screen shot below. The command below assumes that you already have at least 1 physical adapter on both the VDS and Standard Switch. That part can be scripted out as well to move one interface over first, migrate vmkernel ports, and then the other interface.

In my case we need to get hosts from a 4.x Vcenter to a 5.x Vcenter along with all the guests. I will detail this migration to get guests/hosts and script used in a later posting located here.

migrate_networking

This requires multiple clicks and can be tedious if you have multiple hosts that need to be done.  Currently, there is no way that I know of to migrate VMkernel networking from DVS to SS in powercli.  You can go the other direction using the new DVS cmdlet Set-VMHostNetworkAdapter and the port group option.

I wrote a cmdlet to migrate vmkernel ports from DVS to standard. Here it is and the usage:

MigrateDVSwitch-Adapter -VMHost host -Interface vmk1 -NetworkName Vmotion -VirtualSwitch vSwitch -Vlan 77

function MigrateDVSwitch-Adapter{
        #Specify Parameters, VMhostname, VMkernel interface, Network Name, Vlan ID, Standard Switch nameparam (
        [Parameter(Mandatory=$true)][string]$VMHost,
        [Parameter(Mandatory=$true)][string]$Interface,
        [Parameter(Mandatory=$true)][string]$NetworkName,
        [Parameter(Mandatory=$true)][int]$Vlan,
        [Parameter(Mandatory=$true)][string]$VirtualSwitch)

        #Get esxi object
        $VMHostobj = Get-VMHost $VMHost
	#Get Network ID
	$networkid = $VMHostObj.ExtenSionData.Configmanager.NetworkSystem

        # ------- AddPortGroup to Standard Switch
	$portgrp = New-Object VMware.Vim.HostPortGroupSpec
	$portgrp.name = $NetworkName
	$portgrp.vlanId = $Vlan
	$portgrp.vswitchName = $VirtualSwitch
	$portgrp.policy = New-Object VMware.Vim.HostNetworkPolicy
	$_this = Get-View-Id $networkid
	$_this.AddPortGroup($portgrp)

        # ------- UpdateVirtualNic ----Migrates the virtual interface to the standard switch
	$nic = New-Object VMware.Vim.HostVirtualNicSpec
	$nic.portgroup =$NetworkName
	$_this = Get-View-Id $networkid
	$_this.UpdateVirtualNic($Interface, $nic)

VMware’s Online Depot for VMware Image Builder

I am posting this because for some odd reason it seems nearly impossible to find this in any of Vmware’s documentation on ImageBuilder.  It mentions you can add online repo’s but never gives a link to their online repo with all the ESXi builds.

I recently ran across some links and blogs that listed that path.  So, in order to get the online depot imported use this:

Add-EsxSoftwareDepot https://hostupdate.vmware.com/software/VUM/PRODUCTION/main/vmw-depot-index.xml

New Picture

Powercli 5.1 Release 2 released

I was just informed of the release for Powercli 5.1 Release 2 and it appears that they have added some new functions to the Set-NetworkAdapter to help support the DVS switch as well as other DVS functions.  Before this release, it appears you had to get the port key which was quite complicated per this posting here on the communities. http://communities.vmware.com/message/1987845

Release notes are here: http://www.vmware.com/support/developer/PowerCLI/PowerCLI51R2/powercli51r2-releasenotes.html

 Looks like now you can use the -PortGroup switch

Automating ESXi Host Profile and Answer File with Powercli (Part 2)

My previous post  we did a single host profile by creating an answer file and then applying the host profile.  What if you have 10, 20, 30 hosts?

We will use the same functions, but read in a spreadsheet that contains our information and loop through each line in order to apply that to each host. Depending on what answers you need will dictate what is in your csv spreadsheet.  For our environment, we need Management IP, Subnet, Vmotion IP, and subnet.

So, the CSV would look like this:

Untitled

From here you will want to import your CSV and set to a variable:

#Import spreadsheet with hostname,mgmtip,mgmtsubnet,vmoip,vmosubnet for 1000v DVS input
$answerfile = Import-CSV ProfileAnswers.csv

You now have your answer file array set. We will loop through each item and repeat the code in the previous post to apply the host profile:

#Loop through each row and apply host profile

foreach($data in $answerfile) {

#Set VMhost variable to be able to pipe objects

$VMhost = Get-VMhost $data.hostname

Write-Host “Starting $VMhost.name”

#Attach host profile to host

Apply-VMHostProfile -profile $data.profile -entity $VMhost -AssociateOnly -confirm:$false | Out-Null

#Get hash table for answer file and assign to configuration

Write-Host “Getting Hash Table Answer File”

$AdditionalConfiguration =   Apply-VMHostProfile -profile $data.profile -entity $VMHost -ApplyOnly -confirm:$false

#Switch Statement to read in hash table and then set keys for Vmotion IP and Management IP on DVS

$var = @{}

If($AdditionalConfiguration){

switch ($AdditionalConfiguration.GetEnumerator())

{

{$_.name -like ‘*management*.address’ }   {

$var += @{$_.Name = $data.mgmtip}

}

{$_.name -like ‘*management*.subnetmask’} {

$var += @{$_.Name = $data.mgmtsubnet}

}

{$_.name -like ‘*vmotion*.address’} {

$var += @{$_.Name = $data.vmoip}

}

{$_.name -like ‘*vmotion*.subnetmask’}    {

$var += @{$_.Name = $data.vmosubnet}

}

Default {

$value = Read-Host “Please provide a value for ${$_.Name}”

$var +=@{$_.Name = $value}

}

}

}

#Set Host in maintenance mode, apply profile with answer file,  test for compliance

Write-Host “Apply Profile to $VMHost”

Set-VMHost -VMHost $VMhost -State ‘Maintenance’ | Apply-VMHostProfile -Variable $var -Confirm:$false | Test-VMHostProfileCompliance

}

Automating ESXi Host Profile and Answer File with Powercli

Recently we have had an influx of hosts that need to be built out and I am trying to automate as much of the process as possible.  In this post will go over how the answer file works.  Next post I will talk about passing a CSV file to do multiple hosts.  Unfortunately, we do not use DHCP, PXE or Autodeploy so we have to have some manual input for both the install phase and the host profile phase.  So far, I have not found too much information out there on doing this so I have been doing some digging of my own.

Using the great Vmware Vsphere Powercli Reference book, by LucD, Alan Renouf I took their section on automating esxi deploys and tailored it to our needs.  The hardest part is that for our host profile there were answers that the host profile needed.   Namely, Management IP for the 1000v (DVS), VMotion address, and Subnet masks.

Using the Cmdlet Apply-VMHostProfile we are able to get the hash table keys that your host profile needs for input.

For example attach your host profile to your host: Apply-VMHostProfile -profile $profilename -entity $VMhost -AssociateOnly

You can then find out find out what keys are needed for your host profile.  In our case I have disabled the isci parameter and the MAC address parameter  so the only need information will be for our 1000v DVS which we have not added these hosts to yet.  To disable asking for a mac address you can follow this guide: http://blogs.vmware.com/vsphere/2013/03/how-to-prevent-host-profiles-from-prompting-for-mac-addresses.html

$AdditionalConfiguration = Apply-VMHostProfile -profile $profilname -entity $VMHost -ApplyOnly -confirm:$false

The out put will look like below, which as you can see with the key names so long you can’t get the whole key.

PowerCLI U:\powercli\hostprofile> $AdditionalConfiguration.GetEnumerator()| select Name

Name
—-
network.dvsHostNic[“key-vim-profile-host-DvsHostVnicProfile-VSM03–management-management…
network.dvsHostNic[“key-vim-profile-host-DvsHostVnicProfile-VSM03–vmotion-vmotion”].ipC…
network.dvsHostNic[“key-vim-profile-host-DvsHostVnicProfile-VSM03-vmotion-vmotion”].ipC…
network.dvsHostNic[“key-vim-profile-host-DvsHostVnicProfile-VSM03-management-management…
network.dvsHostNic[“key-vim-profile-host-DvsHostVnicProfile-VSM03-vmotion-vmotion”].Mac…
network.dvsHostNic[“key-vim-profile-host-DvsHostVnicProfile-VSM03-management-management…

A better output would be to pipe the output to a list:

$AdditionalConfiguration.GetEnumerator()| fl

Once you have the key names, you can then create an answer file.  To create an answer file run the first command and apply it to a variable which is a hash table

$AdditionalConfiguration=Apply-VMHostProfile -profile $profilname -entity $VMHost -ApplyOnly -confirm:$false

From here there are 2 options.  You can either assign the hashtable values manually or you can create a new hash table using wildcards for the key name and assign your variables. Once, you have your hash table created you can apply the host profile.  I did have an issue where if you don’t answer all the questions needed it will not apply the host profile it will just print out your keys and values plus the empty ones.

Assigning directly:

$AdditionalConfiguration[‘network.dvsHostNic[“key-vim-profile-host-DvsHostVnicProfile-SVD-VSM03-management-management”].ipConfig.IpAddressPolicy.address’] = “10.10.10.10”
$AdditionalConfiguration[‘network.dvsHostNic[“key-vim-profile-host-DvsHostVnicProfile-SVD-VSM03-management-management”].ipConfig.IpAddressPolicy.subnetmask’] = “255.255.255.0”
$AdditionalConfiguration[‘network.dvsHostNic[“key-vim-profile-host-DvsHostVnicProfile-SVD-VSM03-vmotion-vmotion”].ipConfig.IpAddressPolicy.address’] = “10.10.10.11”
$AdditionalConfiguration[‘network.dvsHostNic[“key-vim-profile-host-DvsHostVnicProfile-SVD-VSM03-vmotion-vmotion”].ipConfig.IpAddressPolicy.subnetmask’] = “255.255.255.0”

Or create a switch and looking for a wildcard for your variables that need answers:

$var = @{}
switch ($AdditionalConfiguration.GetEnumerator())
{
{$_.name -like ‘*management*.address’ }    {
$var += @{$_.Name = “10.10.10.10”}
}
{$_.name -like ‘*management*.subnetmask’}    {
$var += @{$_.Name = “255.255.255.0”}
}
{$_.name -like ‘*vmotion*.address’}    {
$var += @{$_.Name = “10.10.10.11”}
}
{$_.name -like ‘*vmotion*.subnetmask’}    {
$var += @{$_.Name = “255.255.255.0”}
}
{$_.name -like ‘*vmotion*.mac’}    {
$var += @{$_.Name = “”}
}
{$_.name -like ‘*management*.mac’}    {
$var += @{$_.Name = “”}

Depending on whichever method you used you could run either of these commands to actually apply your profile. Your host must be in maintenance mode first and we can go over how to automate that in the next post.

Apply-VMHostProfile -Entity $hostname -Variable $var -Confirm:$false

Apply-VMHostProfile -Entity $hostname -Variable $AdditionalConfiguration -Confirm:$false

I have part 2 linked below that shows how to pass a csv file with the information and apply profiles to hosts:

Automating ESXi Host Profile and Answer File with Powercli (Part 2)