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
}
}
}