VMware Cloud Community
rlongley
Contributor
Contributor
Jump to solution

Using Get-VMHost connected to an ESX host vs. connected to a VirtualCenter server

Hello,

The script here returns a list of dead storage paths but only works when you run it against an ESX Host. How can I tweak it to report on all ESX hosts managed by a VirtualCenter server?

$vc = Read-Host “Enter the Name or IP address of your VC Server”

Connect-VIServer –Server $vc

Get-VMHost | Get-ScsiLun | Get-scsilunpath | select Lunpath, State, Preferred, SanID |

where {$_.SanId -ine $null -and $_.State -eq "Dead"}

Disconnect-VIServer -Confirm:$false

I am guessing it's a loop along the lines of "ForEach ($VMhosts in Get-VMHost)" but have been unable to come up with the proper Syntax.

Any suggestions?

Thanks!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The following script will show the properties in the HTML file as in the first script.

For further formatting of the output (column separation, frame, color...) you will have to use HTML formatting.


$report = @()
Get-VMHost | Sort-Object -property Name | % {
	$server = $_
	($_ | Get-View).config.storagedevice.multipathinfo.Lun | % {
		$preferred = $_.Policy.Prefer
		$_.Path | where {$_.PathState -eq "dead"} | % {
			$row = New-Object System.Object
			$row | Add-Member -memberType NoteProperty -name Server -value $server.Name
			$row | Add-Member -memberType NoteProperty -name LunPath -value $_.Name
			$row | Add-Member -memberType NoteProperty -name State -value $_.PathState
                      $IsPreferred = $false
			if($_.Name -eq $preferred){
				$IsPreferred = $true
			}
			$row | Add-Member -memberType NoteProperty -name Preferred -value $IsPreferred
			$sanId = ""
			if($_.Transport.NodeWorldWideName -ne $null){
				$sanId = "{0:x}" -f $_.Transport.NodeWorldWideName
				for($i = $sanId.length-2 ;$i -ge 2; $i -= 2){$sanId = $sanId.insert($i,":")}
			}
			$row | Add-Member -memberType NoteProperty -name SanId -value $sanId
			$report += $row
		}

	}
}
$report | ConvertTo-Html -head "Dead Path's Found" | Out-File "C:\pathreport1.html"


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

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

You could try something along these lines

Connect-ViServer -Server <VC-server>
Get-VmHost | %{
      $ESXserver = Connect-ViServer -Server $_.Name -User <ESX-account> -Password <ESX-password>
      $_ | Get-ScsiLun | Get-scsilunpath | select Lunpath, State, Preferred, SanID | where {$_.SanId -ine $null -and $_.State -eq "Dead"}
      Disconnect-ViServer $ESXserver
}


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

rlongley
Contributor
Contributor
Jump to solution

Hi Luc,

Thanks for the response. In your example, it looks like we have to individually connect to all ESX hosts managed by that VC server?

Here is a sample of a script that does connect to a VC Server and queries all hosts for dead paths;

$vc = Read-Host "Enter the Name or IP address of your VirtualCenter server"

Connect-VIServer -Server $vc

Get-VMHost | % {

$server = $_

($_ | Get-View).config.storagedevice.multipathinfo.Lun | %{$_.Path} | `

select Name, PathState | `

where {$_.PathState -eq "dead"} | `

Add-Member -pass NoteProperty Server $Server.Name | `

Select Server, Name, PathState

} | ConvertTo-Html -head "Dead Path's Found" | Out-File "C:\reports\pathreport1.html"

Disconnect-VIServer -Confirm:$false

It works, but I like the output that the original script I posted gives. What I am struggling with is converting the first script into one that works against all ESX Hosts managed by the VC server, rather than only working against a single ESX Host.

Thanks.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The following script will show the properties in the HTML file as in the first script.

For further formatting of the output (column separation, frame, color...) you will have to use HTML formatting.


$report = @()
Get-VMHost | Sort-Object -property Name | % {
	$server = $_
	($_ | Get-View).config.storagedevice.multipathinfo.Lun | % {
		$preferred = $_.Policy.Prefer
		$_.Path | where {$_.PathState -eq "dead"} | % {
			$row = New-Object System.Object
			$row | Add-Member -memberType NoteProperty -name Server -value $server.Name
			$row | Add-Member -memberType NoteProperty -name LunPath -value $_.Name
			$row | Add-Member -memberType NoteProperty -name State -value $_.PathState
                      $IsPreferred = $false
			if($_.Name -eq $preferred){
				$IsPreferred = $true
			}
			$row | Add-Member -memberType NoteProperty -name Preferred -value $IsPreferred
			$sanId = ""
			if($_.Transport.NodeWorldWideName -ne $null){
				$sanId = "{0:x}" -f $_.Transport.NodeWorldWideName
				for($i = $sanId.length-2 ;$i -ge 2; $i -= 2){$sanId = $sanId.insert($i,":")}
			}
			$row | Add-Member -memberType NoteProperty -name SanId -value $sanId
			$report += $row
		}

	}
}
$report | ConvertTo-Html -head "Dead Path's Found" | Out-File "C:\pathreport1.html"


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

0 Kudos
esarakaitis
Enthusiast
Enthusiast
Jump to solution

0 Kudos
rlongley
Contributor
Contributor
Jump to solution

Thanks LucD.

0 Kudos