vin01
Expert
Expert

Script to convert HBA WWNs to lowercase and add ":"

Jump to solution


Hi

Is it possible to convert the output of below script wwn details to lower case and : after every two characters.

like below 10:00:00:00:c9:ab:cd:ef

script used :

Get-Datacenter "test" | Get-Cluster "testcluster" | Get-VMhost | Get-VMHostHBA -Type FibreChannel | where {$_.Status -eq "online"} | Select @{N="Datacenter";E={$datacenter}},@{N="Cluster";E={$cluster}},VMHost,Device,Status,@{N="WWN";E={"{0:X}"-f$_.PortWorldWideName}} | Export-Csv C:\output.csv

Regards Vineeth.K
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

I did a complete overhaul of the code.

Try it like this

$dcName = "MyDatacenter"
$clusterName = "MyCluster"

$list = Get-Datacenter -Name $dcName | %
 
$datacenter=$_
 
$datacenter | Get-Cluster -Name $clusterName | %{
   
$cluster = $_
   
Get-VMhost -Location $cluster | Get-VMHostHBA -Type FibreChannel | where {$_.Status -eq "online"} |
     
Select @{N="Datacenter";E={$datacenter}},
       
@{N="Cluster";E={$cluster}},
       
@{N="VMHost";E={$_.VMHost.Name}},
       
Device,Status,
       
@{N="WWN";E={((("{0:X}"-f $_.PortWorldWideName).ToLower()) -replace "(\w{2})",'$1:').TrimEnd(':')}}
    }
}

#Output CSV to current directory.
$list | export-csv -NoTypeInformation C:\output.csv


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
18 Replies
markdjones82
Expert
Expert

I actually have a blog posting for this and I modified what I had for yours.


$list = Get-Datacenter "test" | Get-Cluster "testcluster" | Get-VMhost | Get-VMHostHBA -Type FibreChannel | where {$_.Status -eq "online"} | Select @{N="Datacenter";E={$datacenter}},@{N="Cluster";E={$cluster}},VMHost,Device,Status,@{N="WWN";E={("{0:X}"-f$_.PortWorldWideName).ToLower()}}


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 C:\output.csv




http://nutzandbolts.wordpress.com | @markdjones82

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
vin01
Expert
Expert

Thanks a lot Mark this worked for me but need to do small correction...In output Datacenter name field and cluster name field is blank can you please help me out to get cluster name and datacenter name.

sample output :

DatacenterClusterVMHostDeviceStatusWWN
xx.xx.xx.xxvmhba1online21:00:00:e0:8b:94:7e:ab
xx.xx.xx.xxvmhba2online21:01:00:e0:8b:b4:7e:ab
xx.xx.xx.xxvmhba0online21:00:00:e0:8b:94:50:ab
xx.xx.xx.xxvmhba1online21:01:00:e0:8b:b4:50:ab
xx.xx.xx.xxvmhba0online21:00:00:e0:8b:94:68:ab
xx.xx.xx.xxvmhba1online21:01:00:e0:8b:b4:68:ab
Regards Vineeth.K
0 Kudos
markdjones82
Expert
Expert

Just put them in foreach loops, sorry for the bad formatting, you may have to fix the formatting

$list = Get-Datacenter "test" % {  $datacenter=$_

   $_ | Get-Cluster "testcluster" | %{  $cluster = $_

     Get-VMhost | Get-VMHostHBA -Type FibreChannel | where {$_.Status -eq "online"} | Select @{N="Datacenter";E={$datacenter}},@N={"Cluster";E={$cluster}},VMHost,Device,Status,@{N="WWN";E={("{0:X}"-f$_.PortWorldWideName).ToLower()}}

    }

}   

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 C:\output.csv

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
0 Kudos
vin01
Expert
Expert

Hi Mark

Sorry for delay..I tried the given script as below but its not working..and also i need hostname in for esxi host in the out put can you please correct me on the script if any wrong and add host name string...

========================

$list = Get-Datacenter "HYD DC" % {  $datacenter= $_ $_ | Get-Cluster "SUN-UAT" |%{  $cluster = $_ | Get-VMhost | Get-VMHostHBA -Type FibreChannel | where {$_.Status -eq "online"} | Select @{N="Datacenter";E={$datacenter}},@{N="Cluster";E={$cluster}},VMHost,Device,Status,@{N="WWN";E={("{0:X}"-f$_.PortWorldWideName).ToLower()}}
     }
   }
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 -Path SUN-UAT.csv -NoTypeInformation

==========================================

getting error:

errork.JPG

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership

I think you are missing a blank after the format operator (-f).

@{N="WWN";E={("{0:X}"-f $_.PortWorldWideName).ToLower()}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vin01
Expert
Expert

No LucD..Showing same error

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership

I did a complete overhaul of the code.

Try it like this

$dcName = "MyDatacenter"
$clusterName = "MyCluster"

$list = Get-Datacenter -Name $dcName | %
 
$datacenter=$_
 
$datacenter | Get-Cluster -Name $clusterName | %{
   
$cluster = $_
   
Get-VMhost -Location $cluster | Get-VMHostHBA -Type FibreChannel | where {$_.Status -eq "online"} |
     
Select @{N="Datacenter";E={$datacenter}},
       
@{N="Cluster";E={$cluster}},
       
@{N="VMHost";E={$_.VMHost.Name}},
       
Device,Status,
       
@{N="WWN";E={((("{0:X}"-f $_.PortWorldWideName).ToLower()) -replace "(\w{2})",'$1:').TrimEnd(':')}}
    }
}

#Output CSV to current directory.
$list | export-csv -NoTypeInformation C:\output.csv


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vin01
Expert
Expert

that work perfect LucD... Thank a lot.. I need a small addition in the script...I need hostname of the esxi...Can you help me out by adding host name string for the above script.

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership

Isn't the hostname there under the VMHost column ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vin01
Expert
Expert

If I'm wrong that should be the hostname but in out put i am not getting host name

sample output for the above script:


In vmhost coloum it is writing IP address but i need host name in another coloum along with ip address

DatacenterClusterVMHostDeviceStatusWWN
TestdatacenterTestcluster10.75.6.110vmhba1online50:06:0b:00:00:c2:66:38
TestdatacenterTestcluster10.75.6.110vmhba4online50:06:0b:00:00:c2:66:3e
Regards Vineeth.K
0 Kudos
kunaludapi
Expert
Expert

You are getting IP addresses in VMhost because they are added to vCenter by IP addresses not FQDN. Below script should provide you desired results. (only the The bold highlighted area has been replaced, Other code is as it is.)

$dcName = "MyDatacenter"

$clusterName = "MyCluster"

$list = Get-Datacenter -Name $dcName | % {

  $datacenter=$_

  $datacenter | Get-Cluster -Name $clusterName | %{

    $cluster = $_

    Get-VMhost -Location $cluster | Get-VMHostHBA -Type FibreChannel | where {$_.Status -eq "online"} |

      Select @{N="Datacenter";E={$datacenter}},

        @{N="Cluster";E={$cluster}},

        @{N="VMHost";E={$($_.VMHost | Get-VMHostNetwork).HostName}},

        Device,Status,

        @{N="WWN";E={((("{0:X}"-f $_.PortWorldWideName).ToLower()) -replace "(\w{2})",'$1:').TrimEnd(':')}}

    }

}

#Output CSV to current directory.

$list | export-csv -NoTypeInformation C:\output.csv

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
vin01
Expert
Expert

Hi Kunal


Thanks for update

yes above LucD script is correct but i need IP address and Host name also in output.After adding this @{N="IP";E={$_.VMHost.Name}}, I'm able to get IP also...

Regards Vineeth.K
0 Kudos
vin01
Expert
Expert

Thanks LucD Now Im able to get desired OutPut..

Regards Vineeth.K
0 Kudos
vin01
Expert
Expert

Hello Luc

Is it possible to get host hba wwn details when the hosts are not in cluster with the above script.

I mean in "MyDatacenter" some hosts are not in cluster (Standalone hosts), I'm missing those host information in output..

$dcName = "MyDatacenter"

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership

You mean like this

$dcName = "MyDatacenter"

$list = Get-Datacenter -Name $dcName | %

  $datacenter=$_

  Get-VMhost -Location $datacenter | Get-VMHostHBA -Type FibreChannel | where {$_.Status -eq "online"} |

  Select @{N="Datacenter";E={$datacenter}},

      @{N="VMHost";E={$_.VMHost.Name}},

      Device,Status,

      @{N="WWN";E={((("{0:X}"-f $_.PortWorldWideName).ToLower()) -replace "(\w{2})",'$1:').TrimEnd(':')}}

}

#Output CSV to current directory.

$list | export-csv -NoTypeInformation C:\output.csv


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vin01
Expert
Expert

thanks  for quick reply LucSmiley Happy

Here we cannot get cluster details if the hosts are cluster..

my scenario  is in "my datacenter" some hosts are in cluster and few are standalone host,With earlier script I can get all the hosts in cluster there I miss standalone servers.If I'm wrong kindly ignore my mistake..Can we get both at one instance by writing if else..

like if the host is in cluster need to get cluster details also..if not get only server details in datacenter.

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership

Yes, we can find the cluster. Try like this

$dcName = "MyDatacenter"

$list = Get-Datacenter -Name $dcName | %

  $datacenter=$_

  Get-VMhost -Location $datacenter | Get-VMHostHBA -Type FibreChannel | where {$_.Status -eq "online"} |

  Select @{N="Datacenter";E={$datacenter}},

  @{N="Cluster";E={

        if($_.VMHost.ExtensionData.Parent.Type -ne "ClusterComputeResource"){"Stand alone host"}

        else{

            Get-view -Id $_.VMHost.ExtensionData.Parent | Select -ExpandProperty Name

        }

      }},

  @{N="VMHost";E={$_.VMHost.Name}},

  Device,Status,

  @{N="WWN";E={((("{0:X}"-f $_.PortWorldWideName).ToLower()) -replace "(\w{2})",'$1:').TrimEnd(':')}}

}

#Output CSV to current directory.

$list | export-csv -NoTypeInformation C:\output.csv


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vin01
Expert
Expert

Excellent Luc Smiley Happy It worked perfectly..Thanks a lot Guru.

Regards Vineeth.K
0 Kudos