Hi there,
Need your help to refine the below script please. The requirement is to mount the replicated volumes on multiple ESXi host the DR site:
Not sure what does point# 2 and #3 does and also, how and where do I use ForEach-Object loop in the script.
Thanks in advance
Mounting Replicated VMFS Volumes
Following the successful use of the storage management utility to break replication and make volumes
read/write (if required by the storage platform), virtual machines appear inactive in the vCenter Server
inventory. To rectify this, replicated VMFS volumes must be force mounted by the recovery hosts.
NOTE: It is essential that this process be conducted with caution to ensure that the mount process does not
result in volumes’ being resignatured. It is critical to the success of the approach described in this technical
paper that no MoRef or UUID changes occur. In addition, the process illustrated assumes that VMFS volumes
comprise a single extent.
Follow these steps to mount replicated VMFS volumes using vSphere PowerCLI:
1.Connect to the vCenter Server managing the resource cluster and initiate an HBA rescan on all
ESXi servers.
Get-Cluster Name | Get-VMHost | Get-VMHostStorage -RescanAllHba
2.Connect to an ESXi server and identify unresolved VMFS volumes arising from a UUID conflict.
$VMHost = Get-VMHost
$HstSys = Get-View $VMHost.id
$HstDsSys = Get-View $HstSys.ConfigManager.DatastoreSystem
$UnresVols = $HstDsSys.QueryUnresolvedVmfsVolumes()
3.Resolve the UUID conflict on the discovered VMFS volume(s) on the ESXi server.
$HstSSys = Get-view $VMHost.StorageInfo
$UnresVol = $UnresVols[Array Index]
$Extent = $UnresVol.Extent
$DevicePath = $UnresVol.Extent.DevicePath
$ResSpec = New-Object Vmware.Vim.HostUnresolvedVmfsResolutionSpec[](1)
$ResSpec[0].ExtentDevicePath = $DevicePath
$ResSpec[0].UuidResolution = “forceMount”
$HstSSys.ResolveMultipleUnresolvedVmfsVolumes($ResSpec)
4.Initiate a VMFS rescan on the ESXi servers.
$VMHost | Get-VMHostStorage -RescanVmfs
5.Repeat steps 3 and 4 for each of the unresolved VMFS volumes on each affected ESXi server within the
cluster. This should be performed using a direct connection to the associated ESXi server (as opposed to
vCenter Server).
NOTE: When a requirement exists to perform an action on multiple array objects, such as the unresolved VMFS
volumes, it is advisable to use a cmdlet such as ForEach-Object.
See my replies here.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD, let me see how can I test the script
Hi Luc D,
Can you please review the modified script, I guess the only thing missing is $UnresVol = $UnresVols["Array Index"]?
# load the VMware module
$VimAutoCore = "VMware.VimAutomation.Core"
if ( (Get-PSSnapin -Name $VimAutoCore -ErrorAction SilentlyContinue) -eq $null ) {
Write-Host "loading $VimAutoCore powershell module"
Add-PsSnapin $VimAutoCore
}
#connect to the vCenter Server
$vcenter = Read-Host "Please enter the vCenter Hostname or IP Address?"
#prompt for ClusterName
$cluster = Read-Host "Please enter the Cluster name"
#this will prompt the user to enter the user name and password to connect to the vCenter
Write-Host "Connecting to vCenter Server" -ForegroundColor Yellow
Connect-VIServer -Server $vcenter -ErrorAction Stop -Protocol https
Get-Cluster $cluster | Get-VMHost | Get-VMHostStorage -RescanAllHba
ForEach($esx in $cluster){
# Do step 2
$VMHost = Get-VMHost
$HstSys = Get-View $esx.id
$HstDsSys = Get-View $HstSys.ConfigManager.DatastoreSystem
$UnresVols = $HstDsSys.QueryUnresolvedVmfsVolumes()
# Do step 3
$HstSSys = Get-view $esx.StorageInfo
$UnresVol = $UnresVols[Array Index]
$Extent = $UnresVol.Extent
$DevicePath = $UnresVol.Extent.DevicePath
$ResSpec = New-Object Vmware.Vim.HostUnresolvedVmfsResolutionSpec[](1)
$ResSpec[0].ExtentDevicePath = $DevicePath
$ResSpec[0].UuidResolution = “forceMount”
$HstSSys.ResolveMultipleUnresolvedVmfsVolumes($ResSpec)
# Do step 4
$esx | Get-VMHostStorage -RescanVmfs
}
Disconnect-VIServer $vcenter -Confirm:$false
If there is more than 1 unresolved LUN, the $UNresVols will be an array.
That means that you would need to run the complete step 3 in another ForEach loop, in fact executing step 3 for each unresolved LUN.
Something like this
....
$UnresVols = @($HstDsSys.QueryUnresolvedVmfsVolumes())
# Do step 3
$HstSSys = Get-view $esx.StorageInfo
$UnresVols | %{
$Extent = $_.Extent
$DevicePath = $Extent.DevicePath
$ResSpec = New-Object Vmware.Vim.HostUnresolvedVmfsResolutionSpec[](1)
$ResSpec[0].ExtentDevicePath = $DevicePath
$ResSpec[0].UuidResolution = “forceMount”
$HstSSys.ResolveMultipleUnresolvedVmfsVolumes($ResSpec)
}
# Do step 4
$esx | Get-VMHostStorage -RescanVmfs
....
Note that the first line in this extract, forces $UnresVols to be an array, even if there is only 1 unresolved LUN
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Here's the updated script:
# load the VMware module
$VimAutoCore = "VMware.VimAutomation.Core"
if ( (Get-PSSnapin -Name $VimAutoCore -ErrorAction SilentlyContinue) -eq $null ) {
Write-Host "loading $VimAutoCore powershell module"
Add-PsSnapin $VimAutoCore
}
#connect to the vCenter Server
$vcenter = Read-Host "Please enter the vCenter Hostname or IP Address?"
#prompt for ClusterName
$cluster = Read-Host "Please enter the Cluster name"
#this will prompt the user to enter the user name and password to connect to the vCenter
Write-Host "Connecting to vCenter Server" -ForegroundColor Yellow
Connect-VIServer -Server $vcenter -ErrorAction Stop -Protocol https
Get-Cluster $cluster | Get-VMHost | Get-VMHostStorage -RescanAllHba
ForEach($esx in $cluster){
# Do step 2
$VMHost = Get-VMHost
$HstSys = Get-View $esx.id
$HstDsSys = Get-View $HstSys.ConfigManager.DatastoreSystem
$UnresVols = @($HstDsSys.QueryUnresolvedVmfsVolumes())
# Do step 3
$HstSSys = Get-view $esx.StorageInfo
$UnresVols | %{
$Extent = $_.Extent
$DevicePath = $Extent.DevicePath
$ResSpec = New-Object Vmware.Vim.HostUnresolvedVmfsResolutionSpec[](1)
$ResSpec[0].ExtentDevicePath = $DevicePath
$ResSpec[0].UuidResolution = “forceMount”
$HstSSys.ResolveMultipleUnresolvedVmfsVolumes($ResSpec)
}
# Do step 4
$esx | Get-VMHostStorage -RescanVmfs
}
Disconnect-VIServer $vcenter -Confirm:$false
Hi Luc D,
Can you please check if this part of the script looks good:
$ResSpec = New-Object Vmware.Vim.HostUnresolvedVmfsResolutionSpec[](1)
$ResSpec[0].ExtentDevicePath = $DevicePath
$ResSpec[0].UuidResolution = “forceMount”
$HstSSys.ResolveMultipleUnresolvedVmfsVolumes($ResSpec)
Seems to be ok to me.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Here's the updated script:
# load the VMware module
$VimAutoCore = "VMware.VimAutomation.Core"
if ( (Get-PSSnapin -Name $VimAutoCore -ErrorAction SilentlyContinue) -eq $null ) {
Write-Host "loading $VimAutoCore powershell module"
Add-PsSnapin $VimAutoCore
}
#connect to the vCenter Server
$vcenter = Read-Host "Please enter the vCenter Hostname or IP Address"
#this will prompt the user to enter the user name and password to connect to the vCenter
Write-Host "Connecting to vCenter Server $vcenter" -ForegroundColor Yellow
Connect-VIServer -Server $vcenter -ErrorAction Stop -Protocol https
#prompt for ClusterName
$clustername = Read-Host "Please enter the Cluster name"
$cluster = Get-Cluster -name $clustername
Get-Cluster $cluster | Get-VMHost | Get-VMHostStorage -RescanAllHba
$hostsincluster = Get-VMHost -Location $cluster
ForEach($esx in $hostsincluster){
# Check for unresolved UUID
$VMHost = $esx
$HstSys = Get-View $esx.id
$HstDsSys = Get-View $HstSys.ConfigManager.DatastoreSystem
$UnresVols = @($HstDsSys.QueryUnresolvedVmfsVolumes())
# Resolve UUID for each on each ESXi host in the cluster
$HstSSys = Get-view $esx.StorageInfo
$UnresVols | %{
$Extent = $_.Extent
$DevicePath = $Extent.DevicePath
$ResSpec = New-Object Vmware.Vim.HostUnresolvedVmfsResolutionSpec[](1)
$ResSpec[0].ExtentDevicePath = $DevicePath
$ResSpec[0].UuidResolution = “forceMount”
$HstSSys.ResolveMultipleUnresolvedVmfsVolumes($ResSpec)
}
# Rescan for VMFS volumes
$VMHost | Get-VMHostStorage -RescanVmfs
}
Disconnect-VIServer $vcenter -Confirm:$false