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
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
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
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 :
Datacenter | Cluster | VMHost | Device | Status | WWN |
xx.xx.xx.xx | vmhba1 | online | 21:00:00:e0:8b:94:7e:ab | ||
xx.xx.xx.xx | vmhba2 | online | 21:01:00:e0:8b:b4:7e:ab | ||
xx.xx.xx.xx | vmhba0 | online | 21:00:00:e0:8b:94:50:ab | ||
xx.xx.xx.xx | vmhba1 | online | 21:01:00:e0:8b:b4:50:ab | ||
xx.xx.xx.xx | vmhba0 | online | 21:00:00:e0:8b:94:68:ab | ||
xx.xx.xx.xx | vmhba1 | online | 21:01:00:e0:8b:b4:68:ab |
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
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:
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
No LucD..Showing same error
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
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.
Isn't the hostname there under the VMHost column ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
Datacenter | Cluster | VMHost | Device | Status | WWN | |
Testdatacenter | Testcluster | 10.75.6.110 | vmhba1 | online | 50:06:0b:00:00:c2:66:38 | |
Testdatacenter | Testcluster | 10.75.6.110 | vmhba4 | online | 50:06:0b:00:00:c2:66:3e |
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
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...
Thanks LucD Now Im able to get desired OutPut..
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"
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
thanks for quick reply Luc
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.
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
Excellent Luc It worked perfectly..Thanks a lot Guru.