VMware Cloud Community
brianfrank
Contributor
Contributor
Jump to solution

PowerShell Script to find FC Target

We have been searching all over and cannot find the correct commands to pull what we are searching for.  Hopefully someone can help.

If you log into vCenter and Goto Configuration / Storage / Right click on a Datastore and click properties.  Then click Manage Paths.  Under Fibre Channel there is a Target: value.  How do we pull that value from PowerCLI?  Thank you so much for your help!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

In the vSphere Client you see the Node and the Port WWN concatenated together.

To get the same output, use this

$report = foreach($esx in (Get-VMHost | where{$_.PowerState -eq "PoweredOn"})){
    foreach($lun in (Get-ScsiLun -VmHost $esx -LunType disk)){
        Get-ScsiLunPath -ScsiLun $lun | `
        Select @{N="Host";E={$esx.Name}},             @{N="LUN";E={$lun.CanonicalName}},             @{N="Target";E={                 "{0:x} {1:x}" -f ($_.Extensiondata.Transport.NodeworldwideName,                                   $_.Extensiondata.Transport.PortworldwideName)             }}     } } $report | fl


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

View solution in original post

0 Kudos
15 Replies
LucD
Leadership
Leadership
Jump to solution

Try it like this

foreach($esx in Get-VMHost){
    foreach($lun in (Get-ScsiLun -VmHost $esx -LunType disk)){
        Get-ScsiLunPath -ScsiLun $lun | `
        Select @{N="Host";E={$esx.Name}},             @{N="LUN";E={$lun.CanonicalName}},             @{N="Target";E={$_.SanId}}     } }


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

brianfrank
Contributor
Contributor
Jump to solution

LucD thank you for your reply.  When I try to run your example code I am getting an error.  I think it's because the variable $esx and possibly $lun are not properly defined but I am very green on this scripting so I am not 100% sure how to fix the error.

I really appriactate the help!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you connected to the vCenter ?

Did you do a Connect-VIServer before running the script ?

Could you perhaps show the error message(s) you see ?


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

0 Kudos
brianfrank
Contributor
Contributor
Jump to solution

Yes I am connected to our vCenter server.  The error is "Object reference not set to an instance of an object"

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Normally an error message includes the line of code where the error happened, could you include the complete error message ?


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

0 Kudos
brianfrank
Contributor
Contributor
Jump to solution

It wasn't in the error message but I think it was the first line.  foreach($esx in Get-VMHost)

0 Kudos
LucD
Leadership
Leadership
Jump to solution

From where did you run the script ? The PowerCLI prompt, PowerGui,... ?

Does this produce an error message ?

Get-VMHost

Or is there perhaps an ESX(i) that is not powered on ?

Change the original script like this and see if it still produces the error message ?

foreach($esx in (Get-VMHost | where{$_.PowerState -eq "PoweredOn"})){
    foreach($lun in (Get-ScsiLun -VmHost $esx -LunType disk)){
        Get-ScsiLunPath -ScsiLun $lun | `
        Select @{N="Host";E={$esx.Name}},             @{N="LUN";E={$lun.CanonicalName}},             @{N="Target";E={$_.SanId}}     } }


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

0 Kudos
brianfrank
Contributor
Contributor
Jump to solution

Same error on the new code.  I am running this from within PowerGUI and yes I can run Get-VMhost without any errors.  Let try to get another set of eyes on this and see if I can get to the bottom of the error.

0 Kudos
brianfrank
Contributor
Contributor
Jump to solution

OK we were able to get the script to run in PowerGUI and PowerCLI I was trying to run it orignially via PowerGUI Script Editor no idea why it was failing there but moving on.  Now we are seeing the columns and target is getting us some of what we want.  But it is only the first half of the target that you see in vCenter.

On another note we're having trouble making Get-ScsiLunPath to run.  Following this guide http://www.vmware.com/support/developer/windowstoolkit/wintk40u1/html/Get-ScsiLunPath.html

If I use

$scsilun = Get-ScsiLun -VMHost 10.23.123.100 -LunType disk
Get-ScsiLunPath $scsilun

PowerCLI displays

cmdlet Get-ScsiLunPath at command pipeline position 1
Supply values for the following parameters:
ScsiLun[0]:

No idea what it is looking for here.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The problem I suspect you are seeing is that the OBN (Object By Name) does not work with IP addresses.

In fact the Get-VMHost cmdlet doesn't work with an IP address, it requires a hostname.

You can do a simple reverse lookup like this

$IP = "10.23.123.100" 
$hostname
= ([System.Net.Dns]::GetHostEntry($IP)).HostName Get-ScsiLun -VmHost $hostname -LunType disk | Get-ScsiLunPath

Don't forget the pipeline (|) between both cmdlets.


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

LucD
Leadership
Leadership
Jump to solution

In the vSphere Client you see the Node and the Port WWN concatenated together.

To get the same output, use this

$report = foreach($esx in (Get-VMHost | where{$_.PowerState -eq "PoweredOn"})){
    foreach($lun in (Get-ScsiLun -VmHost $esx -LunType disk)){
        Get-ScsiLunPath -ScsiLun $lun | `
        Select @{N="Host";E={$esx.Name}},             @{N="LUN";E={$lun.CanonicalName}},             @{N="Target";E={                 "{0:x} {1:x}" -f ($_.Extensiondata.Transport.NodeworldwideName,                                   $_.Extensiondata.Transport.PortworldwideName)             }}     } } $report | fl


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

0 Kudos
brianfrank
Contributor
Contributor
Jump to solution

That works and gives us what we need!  Smiley Happy

We're trying to figure out, and this is certainly a nice to have feature, if we can get the datastore name into the output so when we send this over to the SAN team they know what we are talking about on our side.  Get-Datastore |fl in the name feild is what we are hoping for.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, try it like this

$report = foreach($esx in (Get-VMHost | where{$_.PowerState -eq "PoweredOn"})){
    $ds = $esx.ExtensionData.Datastore | %{Get-View $_}
    foreach($lun in (Get-ScsiLun -VmHost $esx -LunType disk)){
        Get-ScsiLunPath -ScsiLun $lun | `
        Select @{N="Host";E={$esx.Name}},             @{N="LUN";E={$lun.CanonicalName}},             @{N="Target";E={                 "{0:x} {1:x}" -f ($_.Extensiondata.Transport.NodeworldwideName,                                   $_.Extensiondata.Transport.PortworldwideName)             }},             @{N="Datastore";E={                 ($ds | where{ $_.Info.Vmfs.Extent | where {$_.DiskName -eq $lun.CanonicalName}}).Name             }}     } } $report | fl

The script will scan the extents of each datastore known on the host to see if one of the extents corresponds with the CanonicalName of the LUN.

If you have datastores with multiple extents, the same datastorename will appear with different LUNs.

If you have LUNs that are not used in a datastore (RDM disks, free LUNs), there will be no datastorename.


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

0 Kudos
brianfrank
Contributor
Contributor
Jump to solution

We've been trying and can't seem to get that code to run.  It just seems to bounce around inside the where loop and then go back up to the

Get-ScsiLun foreach loop and then repeat.  
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try the following.

First all the extents from which your datastores are made

$esx = Get-VMHost MyHost

$ds = $esx.ExtensionData.Datastore | %{Get-View $_}

$ds | %{
    $_.Info.Vmfs.Extent | Select DiskName
}

Then list the LUNs with

$esx = Get-VMHost MyHost

Get-ScsiLun -VmHost $esx -LunType disk | Select CanonicalName

You should see that one or more of the LUNs also appear in the list of the datastore extents.

Just to make sure, we are talking about datastores that are created on FC connected LUNs ?


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

0 Kudos