Is there anyway to improve the speed for this. I'm using the unmount and mount functions from the below script. I'm trying to create something that will unmount all attached datastores revert each of those datastores to a snapshot on the storage and remount everything. This works but its a bit slow.
Automating Datastore/Storage Device Detachment in vSphere 5 - VMware vSphere Blog
function revert_netapp_snapshot
{
param(
$virtualMachine,
$datastoreCluster
)
$currentSnapshot=''
$msgBoxInput = [System.Windows.MessageBox]::Show('Are you sure?This will disconnect all the Netapp Snapshot Dastores and restore them from the last snapshot','Snapshot Revert Confirmation','YesNoCancel','Error')
switch ($msgBoxInput) {
'Yes' {
$datastores=Get-Datastore -RelatedObject $datastoreCluster
$vmlocation=$(Get-VM $virtualMachine | select Name,@{E={$_.ExtensionData.Config.Files.VmPathName};L=”VM Path”}).'VM Path'
$esxi=$(Get-VM $virtualMachine).VMhost
foreach($datastore in $datastores)
{
$poweredOnVms=Get-VM -Datastore $datastore | Where-Object {$_.PowerState -eq "PoweredOn"}
foreach($vm in $poweredOnVms)
{
while($vm.PowerState -eq "PoweredOn")
{
try{
Shutdown-VMGuest -VM $vm -Confirm $false -ErrorAction Stop
sleep 15
}catch{
Stop-VM -VM $vm
sleep 15
}
}
}
}
Remove-VM -VM $virtualMachine
foreach($datastore in $datastores)
{
$volume=$datastore.Name+"_vol"
$snapshots=Get-NcSnapshot -SnapName $volume"*"
if($snapshots.Count -gt 0)
{
if($snapshots.Count -eq 1)
{
$currentSnapshot=$snapshots
}else{
$currentSnapshot=$snapshots[$snapshots.Count -1]
}
}
Restore-NcSnapshotVolume $volume $currentSnapshot.Name -Confirm:$false
}
foreach($datastore in $datastores)
{
Write-Host "Mounting " $datastore
Mount-Datastore $datastore
}
New-VM -VMHost $esxi -VMFilePath $vmlocation
break
}
'No' {
break
}
'Cancel'{
break
}
}
}
I would sooner investigate if the isn't a more general problem with the LUNs involved.
Do the datastore mounts from the Web Client faster?
Anything in the logs on path trashing, SCSI timeouts...?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The webclient is 4 times faster, about 10 minutes for 9 datstores to unmount and remount, vs 44 minutes to unmount and restore 9 datastores. I'll look at the logs but based on this I think its something in the code I wrote. I think I misunderstand the process block in powershell, I was hoping that would do the host unmounts asynchronously, but I think its doing the one at a time. I think I'll try adjust that unmount and mount functions to use jobs and then count the number of unmounting and mount events before reverting the lun snapshot. I'm pretty sure thats why its quicker in the webclient as I was firing the unmounts as fast as possible.
Afaik, there is no option to run the MountVmfsVolume method in a RunAsync modus.
I suspect the Mount-Datastore function you are using is trying to mount the datastore on each ESXi node on which the underlying LUN is visible.
That might be the difference between the function and the Web Client
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference