Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
5040 lines (4819 sloc) 237 KB
<#
***************************************************************************************************
VMWARE POWERCLI AND PURE STORAGE POWERSHELL SDK MUST BE INSTALLED ON THE MACHINE THIS IS RUNNING ON
***************************************************************************************************
For info, refer to www.codyhosterman.com
*******Disclaimer:******************************************************
This scripts are offered "as is" with no warranty. While this
scripts is tested and working in my environment, it is recommended that you test
this script in a test lab before using in a production environment. Everyone can
use the scripts/commands provided here without any written permission but I
will not be liable for any damage or loss to the system.
************************************************************************
This can be run directly from PowerCLI or from a standard PowerShell prompt. PowerCLI must be installed on the local host regardless.
Supports:
-PowerShell 3.0 or later
-Pure Storage PowerShell SDK 1.7 or later
-PowerCLI 6.3 Release 1+
-Purity 4.7 and later
-FlashArray 400 Series and //m
-vCenter 5.5 and later
'Pure Storage FlashArray VMware Snapshot Recovery Tool v2.8.0'
#>
#Import PowerCLI. Requires PowerCLI version 6.3 or later. Will fail here if PowerCLI is not installed
#Will try to install PowerCLI with PowerShellGet if PowerCLI is not present.
if ((!(Get-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) -and (!(get-Module -Name VMware.PowerCLI -ListAvailable))) {
if (Test-Path “C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1”)
{
. “C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1|out-null
}
elseif (Test-Path “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1”)
{
. “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1|out-null
}
elseif (!(get-Module -Name VMware.PowerCLI -ListAvailable))
{
if (get-Module -name PowerShellGet -ListAvailable)
{
try
{
Get-PackageProvider -name NuGet -ListAvailable -ErrorAction stop
}
catch
{
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Scope CurrentUser -Confirm:$false
}
Install-Module -Name VMware.PowerCLI –Scope CurrentUser -Confirm:$false -Force
}
else
{
write-host ("PowerCLI could not automatically be installed because PowerShellGet is not present. Please install PowerShellGet or PowerCLI") -BackgroundColor Red
write-host "PowerShellGet can be found here https://www.microsoft.com/en-us/download/details.aspx?id=51451 or is included with PowerShell version 5"
write-host "Terminating Script" -BackgroundColor Red
return
}
}
if ((!(Get-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) -and (!(get-Module -Name VMware.PowerCLI -ListAvailable)))
{
write-host ("PowerCLI not found. Please verify installation and retry.") -BackgroundColor Red
write-host "Terminating Script" -BackgroundColor Red
return
}
}
set-powercliconfiguration -invalidcertificateaction "ignore" -confirm:$false |out-null
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false -confirm:$false|out-null
#Set
$EndPoint = $null
$Endpoints = @()
$ErrorActionPreference = "Stop"
#Connection Functions
function connectServer{
try
{
$connect = Connect-VIServer -Server $serverTextBox.Text -User $usernameTextBox.Text -Password $passwordTextBox.Text -ErrorAction stop
$buttonConnect.Enabled = $false #Disable controls once connected
$serverTextBox.Enabled = $false
$usernameTextBox.Enabled = $false
$passwordTextBox.Enabled = $false
$buttonDisconnect.Enabled = $true #Enable Disconnect button
$outputTextBox.text = ((get-Date -Format G) + " Successfully connected to vCenter $($serverTextBox.Text)`r`n$($outputTextBox.text)")
if (($endpoints.count -ge 1) -and ($buttonDisconnect.Enabled = $true))
{
getClusters
enableObjects
$TabControl.Enabled = $true
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function disconnectServer{
try
{
$disconnect = Disconnect-VIServer -Server $serverTextBox.Text -Confirm:$false -Force:$true -ErrorAction stop
$buttonConnect.Enabled = $true #Enable login controls once disconnected
$serverTextBox.Enabled = $true
$usernameTextBox.Enabled = $true
$passwordTextBox.Enabled = $true
$buttonDisconnect.Enabled = $false #Disable Disconnect button
$outputTextBox.text = (get-Date -Format G) + " Successfully disconnected from vCenter $($serverTextBox.Text)`r`n" + $outputTextBox.text
disableObjects
$TabControl.Enabled = $false
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function connectFlashArray{
try
{
$FApassword = convertto-securestring $flasharrayPasswordTextBox.Text -asplaintext -force
$script:EndPoints += New-PfaArray -EndPoint $flasharrayTextBox.Text -Username $flasharrayUsernameTextBox.Text -Password $FApassword -IgnoreCertificateError -ErrorAction stop
$outputTextBox.text = (get-Date -Format G) + " Successfully connected to FlashArray $($flasharrayTextBox.Text)" + ("`r`n") + $outputTextBox.text
$newArray = Get-PfaArrayAttributes -Array $endpoints[-1]
$FAisValid = $true
for ($i=0;$i -lt ($endpoints.count - 1);$i++)
{
$currentArray = Get-PfaArrayAttributes -Array $endpoints[$i]
if ($currentArray.id -eq $newArray.id)
{
$outputTextBox.text = (get-Date -Format G) + " ERROR: Will not connect this FlashArray. This FlashArray is already registered under the IP/FQDN of $($endpoints[$i].endpoint)" + ("`r`n") + $outputTextBox.text
$FAisValid = $false
}
}
if ($FAisValid -eq $false)
{
disconnectFlashArray
}
else
{
$flasharrayButtonConnect.enabled = $false
$flasharrayButtonDisconnect.enabled = $true
$flasharrayTextBox.Enabled = $false
$flasharrayUsernameTextBox.Enabled = $false
$flasharrayPasswordTextBox.Enabled = $false
$FlashArrayDropDownBox.Items.Add($flasharrayTextBox.Text)
$flasharrayPasswordTextBox.Text = ""
$flasharrayUsernameTextBox.Text = ""
$FlashArrayDropDownBox.Enabled=$true
$FlashArrayDropDownBox.SelectedIndex = $endpoints.count
if (($endpoints.count -eq 1) -and ($buttonDisconnect.Enabled -eq $true))
{
getClusters
enableObjects
$TabControl.Enabled = $true
}
elseif (($endpoints.count -gt 1) -and ($buttonDisconnect.Enabled -eq $true))
{
listFlashArrays
}
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " Connection to FlashArray " + $flasharrayTextBox.Text + " failed. Please check credentials or IP/FQDN") + ("`r`n") + $outputTextBox.text
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function disconnectFlashArray{
try
{
foreach ($EndPoint in $Endpoints)
{
if ($endpoint.endpoint-eq $flasharrayTextBox.Text)
{
break
}
}
Disconnect-PfaArray -Array $EndPoint -ErrorAction stop
$script:endpoints = $endpoints -ne $endpoint
$outputTextBox.text = ((get-Date -Format G) + " Successfully disconnected from FlashArray " + $flasharrayTextBox.Text) + ("`r`n") + $outputTextBox.text
$FlashArrayDropDownBox.Items.Remove($flasharrayTextBox.Text)
$FlashArrayDropDownBox.SelectedIndex = $endpoints.count
if ($Endpoints.count -eq 0)
{
$RadioButtonVM.Enabled = $false
$RadioButtonVMDK.Enabled = $false
$RadioButtonRDM.Enabled = $false
}
if (($endpoints.count -ge 1) -and ($buttonDisconnect.Enabled = $true))
{
listFlashArrays
}
if (($endpoints.count -eq 0))
{
disableObjects
$TabControl.Enabled = $false
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " Disconnection from FlashArray " + $flasharrayTextBox.Text + " failed.") + ("`r`n") + $outputTextBox.text
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
#Inventory Functions
function getDatastores{
try
{
$DatastoreDropDownBox.Items.Clear()
if ($ClusterDropDownBox.SelectedItem.ToString() -eq "<All Clusters>")
{
if ($nameFilterTextBox.Text -ne "")
{
$datastores = get-datastore -Name ("*" + $nameFilterTextBox.Text + "*") | where-object {$_.Type -eq "VMFS"}
}
else
{
$datastores = get-datastore | where-object {$_.Type -eq "VMFS"}
}
}
else
{
if ($nameFilterTextBox.Text -ne "")
{
$datastores = get-cluster -Name $ClusterDropDownBox.SelectedItem.ToString() |get-datastore -Name ("*" + $nameFilterTextBox.Text + "*") | where-object {$_.Type -eq "VMFS"}
}
else
{
$datastores = get-cluster -Name $ClusterDropDownBox.SelectedItem.ToString() |get-datastore | where-object {$_.Type -eq "VMFS"}
}
}
if ($datastores.count -ge 1)
{
$DatastoreDropDownBox.Enabled=$true
$DatastoreDropDownBox.Items.Add("Choose VMFS...")
foreach ($datastore in $datastores)
{
$DatastoreDropDownBox.Items.Add($datastore.Name) #Add Datastores to DropDown List
}
$DatastoreDropDownBox.SelectedIndex = 0
}
elseif ($datastores.count -eq 0)
{
$DatastoreDropDownBox.Enabled=$false
$DatastoreDropDownBox.Items.Add("<No Datastores Found>") #Add Datastores to DropDown List
$DatastoreDropDownBox.SelectedIndex = 0
$buttonSnapshots.Enabled = $false
$newSnapshotTextBox.Enabled = $false
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function getClusters{
try
{
$clusters = Get-Cluster #Returns all clusters
if ($TabControl.SelectedIndex -eq 3)
{
$PgroupClusterDropDownBox.Items.Clear()
$PgroupClusterDropDownBox.Items.Add("Choose a Recovery Cluster...")
foreach ($cluster in $clusters)
{
$PgroupClusterDropDownBox.Items.Add($cluster.Name) #Add Clusters to DropDown List
}
$PgroupClusterDropDownBox.Enabled = $true
$PgroupClusterDropDownBox.SelectedIndex = 0
}
elseif ($TabControl.SelectedIndex -eq 2)
{
$HostClusterDropDownBox.Items.Clear()
$HostClusterDropDownBox.Items.Add("Choose a Cluster...")
foreach ($cluster in $clusters)
{
$HostClusterDropDownBox.Items.Add($cluster.Name) #Add Clusters to DropDown List
}
$HostClusterDropDownBox.Enabled = $true
$HostClusterDropDownBox.SelectedIndex = 0
}
elseif (($TabControl.SelectedIndex -eq 0) -or ($TabControl.SelectedIndex -eq 1))
{
$ClusterDropDownBox.Items.Clear()
$ClusterDropDownBox.Items.Add("<All Clusters>")
foreach ($cluster in $clusters)
{
$ClusterDropDownBox.Items.Add($cluster.Name) #Add Clusters to DropDown List
}
$ClusterDropDownBox.Enabled = $true
$ClusterDropDownBox.SelectedIndex = 0
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function getRecoveryClusters{
try
{
$clusters = Get-Cluster -ErrorAction stop #Returns all clusters
if ($getVMFSCluster -eq $true)
{
$CreateVMFSClusterDropDownBox.Items.Clear()
$CreateVMFSClusterDropDownBox.Items.Add("Choose a Cluster...")
foreach ($cluster in $clusters)
{
$CreateVMFSClusterDropDownBox.Items.Add($cluster.Name) #Add Clusters to DropDown List
$CreateVMFSClusterDropDownBox.Enabled = $true
}
$CreateVMFSClusterDropDownBox.SelectedIndex = 0
}
elseif ($RecoveryClusterDropDownBox.Enabled -eq $true)
{
$RecoveryClusterDropDownBox.Items.Clear()
$RecoveryClusterDropDownBox.Items.Add("Choose a Cluster...")
foreach ($cluster in $clusters)
{
$RecoveryClusterDropDownBox.Items.Add($cluster.Name) #Add Clusters to DropDown List
$RecoveryClusterDropDownBox.Enabled = $true
}
$RecoveryClusterDropDownBox.SelectedIndex = 0
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function getSnapshots{
try
{
$SnapshotDropDownBox.Items.Clear()
$SnapshotDropDownBox.Enabled = $false
if ($TabControl.SelectedIndex -eq 0)
{
$datastore = get-datastore $DatastoreDropDownBox.SelectedItem.ToString() -ErrorAction stop
$script:lun = $datastore.ExtensionData.Info.Vmfs.Extent.DiskName |select-object -unique
}
getFlashArray
if ($script:lun -like 'naa.624a9370*')
{
$volumes = Get-PfaVolumes -Array $EndPoint
$volserial = ($lun.ToUpper()).substring(12)
$script:purevol = $volumes | where-object { $_.serial -eq $volserial }
if ($purevol -eq $null)
{
$outputTextBox.text = (get-Date -Format G) + " ERROR: Volume not found on a connected FlashArray." + ("`r`n") + $outputTextBox.text
$SnapshotDropDownBox.Items.Clear()
$buttonSnapshots.Enabled = $false
$newSnapshotTextBox.Enabled = $false
$buttonDeleteVMFS.Enabled = $false
$newSnapshotTextBox.Text = ""
$LabelNewSnapError.Text = ""
$SnapshotDropDownBox.Enabled = $false
$buttonDelete.Enabled = $false
}
else
{
$script:snapshots = $null
$script:snapshots = Get-PfaVolumeSnapshots -array $endpoint -VolumeName $purevol.name
$buttonDeleteVMFS.Enabled = $true
if ($snapshots -ne $null)
{
$SnapshotDropDownBox.Items.Add("Choose a Snapshot...")
foreach ($snapshot in $snapshots)
{
$SnapshotDropDownBox.Items.Add("$($snapshot.Name) ($($snapshot.Created))") #Add snapshots to drop down List
}
$SnapshotDropDownBox.Enabled=$true
$newSnapshotTextBox.Enabled = $true
$SnapshotDropDownBox.SelectedIndex = 0
}
else
{
$SnapshotDropDownBox.Items.Add("No snapshots found")
$SnapshotDropDownBox.SelectedIndex = 0
$newSnapshotTextBox.Enabled = $true
$SnapshotDropDownBox.Enabled=$false
}
}
}
else
{
$outputTextBox.text = (get-Date -Format G) + " Selected datastore is not a FlashArray volume." + ("`r`n") + $outputTextBox.text
$SnapshotDropDownBox.Items.Clear()
$buttonSnapshots.Enabled = $false
$newSnapshotTextBox.Enabled = $false
$buttonDeleteVMFS.Enabled = $false
$newSnapshotTextBox.Text = ""
$LabelNewSnapError.Text = ""
$SnapshotDropDownBox.Enabled = $false
$buttonDelete.Enabled = $false
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function getVMSnapshots{
try
{
$VMSnapshotDropDownBox.Items.Clear()
$VMSnapshotDropDownBox.Enabled = $false
if ($script:RadioButtonVMDK.Checked -eq $true)
{
$datastore = get-datastore (($VMDKDropDownBox.SelectedItem.ToString() |foreach { $_.Split("]")[0] }).substring(1)) -ErrorAction stop
$script:lun = $datastore.ExtensionData.Info.Vmfs.Extent.DiskName |select-object -unique
}
elseif ($script:RadioButtonRDM.Checked -eq $true)
{
$script:lun = ($RDMDropDownBox.SelectedItem.ToString()).substring(0,36)
}
else
{
$datastore = get-vm -Name $VMDropDownBox.SelectedItem.ToString() |Get-Datastore -ErrorAction stop
$script:lun = $datastore.ExtensionData.Info.Vmfs.Extent.DiskName |select-object -unique
if ($datastore.count -gt 1)
{
$CheckBoxDeleteVMObjectSnapshot.Enabled = $false
$CheckBoxDeleteVMObjectSnapshot.Checked = $false
$buttonGetVMSnapshots.Enabled = $false
$newVMSnapshotTextBox.Enabled = $false
$buttonNewVMSnapshot.Enabled = $false
$newVMSnapshotTextBox.Text = ""
disableRecoveryItems
throw "This VM uses more than one datastore and is not currently supported in this tool for full recovery. Use per-VMDK or per-RDM recovery instead."
}
}
getFlashArray
if ($script:lun -like 'naa.624a9370*')
{
$volumes = Get-PfaVolumes -Array $EndPoint
$volserial = ($lun.ToUpper()).substring(12)
$script:purevol = $volumes | where-object { $_.serial -eq $volserial }
if ($purevol -eq $null)
{
disableRecoveryItems
$outputTextBox.text = (get-Date -Format G) + " ERROR: Volume not found on a connected FlashArray." + ("`r`n") + $outputTextBox.text
$VMSnapshotDropDownBox.Items.Clear()
$buttonGetVMSnapshots.Enabled = $false
$VMSnapshotDropDownBox.Enabled = $false
$CheckBoxDeleteVMObjectSnapshot.Enabled = $false
$CheckBoxDeleteVMObjectSnapshot.Checked = $false
$newVMSnapshotTextBox.Enabled = $false
$buttonNewVMSnapshot.Enabled = $false
$newVMSnapshotTextBox.Text = ""
}
else
{
$script:snapshots = $null
$script:snapshots = Get-PfaVolumeSnapshots -array $endpoint -VolumeName $purevol.name
if ($snapshots -ne $null)
{
$VMSnapshotDropDownBox.Items.Add("Choose a Snapshot...")
foreach ($snapshot in $snapshots)
{
$VMSnapshotDropDownBox.Items.Add("$($snapshot.Name) ($($snapshot.Created))") #Add snapshots to drop down List
}
$VMSnapshotDropDownBox.Enabled=$true
$buttonGetVMSnapshots.Enabled = $true
$VMSnapshotDropDownBox.SelectedIndex = 0
$newVMSnapshotTextBox.Enabled = $true
}
else
{
$VMSnapshotDropDownBox.Items.Add("No snapshots found")
$VMSnapshotDropDownBox.SelectedIndex = 0
$VMSnapshotDropDownBox.Enabled=$false
$buttonGetVMSnapshots.Enabled = $false
$CheckBoxDeleteVMObjectSnapshot.Enabled = $false
$CheckBoxDeleteVMObjectSnapshot.Checked = $false
$newVMSnapshotTextBox.Enabled = $true
}
}
}
else
{
$outputTextBox.text = (get-Date -Format G) + " Selected datastore is not a FlashArray volume." + ("`r`n") + $outputTextBox.text
$VMSnapshotDropDownBox.Items.Clear()
$newSnapshotTextBox.Enabled = $false
$newSnapshotTextBox.Text = ""
$LabelNewSnapError.Text = ""
$VMSnapshotDropDownBox.Enabled = $false
$CheckBoxDeleteVMObjectSnapshot.Enabled = $false
$CheckBoxDeleteVMObjectSnapshot.Checked = $false
$buttonGetVMSnapshots.Enabled = $false
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function getHostGroup{
try
{
$fcinitiators = @()
$iscsiinitiators = @()
if ($TabControl.SelectedIndex -eq 0)
{
if ($script:createVMFS -eq $true)
{
$recoveryobject = get-cluster -Name $CreateVMFSClusterDropDownBox.SelectedItem.ToString()
$fatemp = $ChooseFlashArrayDropDownBox.SelectedItem.ToString()
foreach ($faArray in $endpoints)
{
if ($faArray.endpoint -eq $fatemp)
{
$script:endpoint = $faArray
break
}
}
}
else
{
$recoveryobject = get-cluster -Name $RecoveryClusterDropDownBox.SelectedItem.ToString()
}
}
elseif ($TabControl.SelectedIndex -eq 2)
{
$recoveryobject = get-cluster -name $HostClusterDropDownBox.SelectedItem.ToString()
}
elseif ($TabControl.SelectedIndex -eq 3)
{
$script:endpoint = $endpoints[$PgroupFADropDownBox.SelectedIndex-1]
$recoveryobject = get-cluster -Name $PgroupClusterDropDownBox.SelectedItem.ToString()
}
else
{
if (($script:RadioButtonVM.Checked -eq $true) -and ($cloneObject -eq $true))
{
$recoveryobject = get-cluster -Name $TargetClusterDropDownBox.SelectedItem.ToString()
}
elseif((($script:RadioButtonVMDK.Checked -eq $true) -or ($script:RadioButtonRDM.Checked -eq $true)) -and ($cloneObject -eq $true))
{
$recoveryobject = get-vm -Name $TargetVMDropDownBox.SelectedItem.ToString()
}
else
{
$recoveryobject = get-vm -Name $VMDropDownBox.SelectedItem.ToString()
}
}
$iscsiadapters = $recoveryobject |Get-VMHost | Get-VMHostHBA -Type iscsi | Where {$_.Model -eq "iSCSI Software Adapter"}
$fcadapters = $recoveryobject |Get-VMHost | Get-VMHostHBA -Type FibreChannel | Select VMHost,Device,@{N="WWN";E={"{0:X}" -f $_.PortWorldWideName}} | Format-table -Property WWN -HideTableHeaders |out-string
foreach ($iscsiadapter in $iscsiadapters)
{
$iqn = $iscsiadapter.ExtensionData.IScsiName
$iscsiinitiators += $iqn.ToLower()
}
$fcadapters = (($fcadapters.Replace("`n","")).Replace("`r","")).Replace(" ","")
$fcadapters = &{for ($i = 0;$i -lt $fcadapters.length;$i += 16)
{
$fcadapters.substring($i,16)
}}
foreach ($fcadapter in $fcadapters)
{
$fcinitiators += $fcadapter.ToLower()
}
$fahosts = Get-PfaHosts -array $endpoint
$script:hostgroup = $null
foreach ($fahost in $fahosts)
{
foreach ($iscsiinitiator in $iscsiinitiators)
{
if ($fahost.iqn -contains $iscsiinitiator)
{
$script:hostgroup = $fahost.hgroup
break
}
}
if ($hostgroup -ne $null)
{
break
}
foreach ($fcinitiator in $fcinitiators)
{
if ($fahost.wwn -contains $fcinitiator)
{
$script:hostgroup = $fahost.hgroup
break
}
}
if ($hostgroup -ne $null)
{
break
}
}
if ($hostgroup -eq $null)
{
$outputTextBox.text = ((get-Date -Format G) + " No matching host group on $($endpoint.endpoint) could be found") + ("`r`n") + $outputTextBox.text
}
else
{
$outputTextBox.text = ((get-Date -Format G) + " The host group identified on FlashArray $($endpoint.endpoint) is named $($hostgroup)`r`n$($outputTextBox.text)")
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function getVMs{
try
{
$VMDropDownBox.Items.Clear()
if ($ClusterDropDownBox.SelectedItem.ToString() -eq "<All Clusters>")
{
if ($nameFilterTextBox.Text -ne "")
{
$vms = get-vm -Name ("*" + $nameFilterTextBox.Text + "*") -ErrorAction stop
}
else
{
$vms = get-vm -ErrorAction stop
}
}
else
{
if ($nameFilterTextBox.Text -ne "")
{
$vms = get-cluster -Name $ClusterDropDownBox.SelectedItem.ToString() -ErrorAction stop|get-vm -Name ("*" + $nameFilterTextBox.Text + "*") -ErrorAction stop
}
else
{
$vms = get-cluster -Name $ClusterDropDownBox.SelectedItem.ToString() -ErrorAction stop |get-vm -ErrorAction stop
}
}
if ($vms.count -eq 0)
{
$VMDropDownBox.Items.Add("No VMs found")
$VMDropDownBox.SelectedIndex = 0
$VMDropDownBox.Enabled=$false
$VMSnapshotDropDownBox.Items.Clear()
$VMSnapshotDropDownBox.Enabled = $false
}
else
{
$VMDropDownBox.Items.Add("Choose VM...")
foreach ($vm in $vms)
{
$VMDropDownBox.Items.Add($vm.Name) #Add VMs to DropDown List
}
$VMDropDownBox.Enabled=$true
$VMDropDownBox.SelectedIndex = 0
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function getDisks{
try
{
if (($VMDropDownBox.text -ne "No VMs found") -and ($script:RadioButtonVM.Checked -ne $true))
{
if ($script:RadioButtonVMDK.Checked -eq $true)
{
$VMDKDropDownBox.Items.Clear()
$atLeastOnevdisk = $false
$vm = get-vm -Name $VMDropDownBox.SelectedItem.ToString() -ErrorAction stop
$vmdevices = $vm.ExtensionData.Config.Hardware.Device
foreach ($vmdevice in $vmdevices)
{
if ($vmdevice.gettype().Name -eq "VirtualDisk")
{
if ( $vmdevice.Backing.gettype().Name -eq "VirtualDiskFlatVer2BackingInfo")
{
if ($atLeastOnevdisk -eq $false)
{
$VMDKDropDownBox.Items.Add("Choose a VMDK...")
}
$atLeastOnevdisk = $true
$VMDKDropDownBox.Items.Add(("$($vmdevice.Backing.fileName) ($($vmdevice.CapacityInKB/1024/1024) GB)"))
}
}
}
if ($atLeastOnevdisk -eq $true)
{
$VMDKDropDownBox.Enabled = $true
$VMDKDropDownBox.SelectedIndex = 0
}
else
{
$VMDKDropDownBox.Items.Add("No virtual disks found")
$VMDKDropDownBox.SelectedIndex = 0
$VMSnapshotDropDownBox.Items.Clear()
$VMSnapshotDropDownBox.Enabled = $false
}
}
elseif ($script:RadioButtonRDM.Checked -eq $true)
{
$RDMDropDownBox.Items.Clear()
$atLeastOnerdm = $false
$vm = get-vm -Name $VMDropDownBox.SelectedItem.ToString() -ErrorAction stop
$vmdevices = $vm.ExtensionData.Config.Hardware.Device
foreach ($vmdevice in $vmdevices)
{
if ($vmdevice.gettype().Name -eq "VirtualDisk")
{
if ( $vmdevice.Backing.gettype().Name -eq "VirtualDiskRawDiskMappingVer1BackingInfo")
{
if ($atLeastOnerdm -eq $false)
{
$RDMDropDownBox.Items.Add("Choose a RDM...")
}
$atLeastOnerdm = $true
$RDMname = ("naa.$($vmdevice.Backing.DeviceName.substring(14,32)) ($($vmdevice.CapacityInKB/1024/1024) GB)")
$RDMDropDownBox.Items.Add($RDMname)
}
}
}
if ($atLeastOnerdm -eq $true)
{
$RDMDropDownBox.Enabled = $true
$RDMDropDownBox.SelectedIndex = 0
}
else
{
$RDMDropDownBox.Items.Add("No raw device mappings found")
$RDMDropDownBox.SelectedIndex = 0
$RDMDropDownBox.Enabled = $false
$VMSnapshotDropDownBox.Items.Clear()
$VMSnapshotDropDownBox.Enabled = $false
}
}
}
else
{
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function getFlashArray{
try
{
$volArraySN = $script:lun.substring(12,16)
foreach ($FAarray in $endpoints)
{
$arraySN = Get-PfaArrayAttributes -Array $FAarray
$arraySN = $arraySN.id.substring(0,18)
$arraySN = $arraySN -replace '-',''
if ($arraySN -ieq $volArraySN)
{
$script:endpoint = $FAarray
break
}
else
{
$script:endpoint = $FAarray
}
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function listFlashArrays{
try
{
if (($TabControl.SelectedIndex -eq 0) -or ($TabControl.SelectedIndex -eq 1))
{
$ChooseFlashArrayDropDownBox.Items.Clear()
$ChooseFlashArrayDropDownBox.Items.Add("Choose a FlashArray...")
foreach ($fa in $endpoints)
{
$ChooseFlashArrayDropDownBox.Items.Add($fa.endpoint)
}
$ChooseFlashArrayDropDownBox.SelectedIndex = 0
}
elseif ($TabControl.SelectedIndex -eq 2)
{
$HostFlashArrayDropDownBox.Enabled = $false
$HostFlashArrayDropDownBox.Items.Clear()
$HostFlashArrayDropDownBox.Items.Add("Choose a FlashArray...")
$HostFlashArrayDropDownBox.Items.Add("Select All FlashArrays")
foreach ($fa in $endpoints)
{
$HostFlashArrayDropDownBox.Items.Add($fa.endpoint)
$HostFlashArrayDropDownBox.Enabled = $true
}
$HostFlashArrayDropDownBox.SelectedIndex = 0
}
elseif ($TabControl.SelectedIndex -eq 3)
{
$PgroupFADropDownBox.Enabled = $false
$PgroupFADropDownBox.Items.Clear()
$PgroupFADropDownBox.Items.Add("Choose a FlashArray...")
foreach ($fa in $endpoints)
{
$PgroupFADropDownBox.Items.Add($fa.endpoint)
$PgroupFADropDownBox.Enabled = $true
}
if ($endpoints.count -eq 1)
{
$PgroupFADropDownBox.SelectedIndex = 1
}
else
{
$PgroupFADropDownBox.SelectedIndex = 0
$AddToPgroupCheckedListBox.Items.Clear()
$AddToPgroupCheckedListBox.Enabled=$false
}
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function getTargetVMs{
try
{
$targetVMs = Get-VM #Returns all VMs
$TargetVMDropDownBox.Items.Clear()
$TargetVMDropDownBox.Items.Add("Choose Target VM...")
foreach ($targetVM in $targetVMs)
{
$TargetVMDropDownBox.Items.Add($targetVM.Name) #Add VMs to DropDown List
}
$TargetVMDropDownBox.Enabled = $true
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
$TargetVMDropDownBox.Enabled = $true
$TargetVMDropDownBox.SelectedIndex = 0
}
function getTargetClusters{
try
{
if ($TargetDatastoreDropDownBox.SelectedItem.ToString() -eq "<Keep on a New Recovery Datastore>")
{
$targetClusters = get-cluster #Returns all clusters
}
else
{
$targetClusters = get-datastore $TargetDatastoreDropDownBox.SelectedItem.ToString() |get-vmhost |get-cluster #Returns all clusters
}
$TargetClusterDropDownBox.Items.Clear()
$TargetClusterDropDownBox.Items.Add("Choose Target Cluster...")
foreach ($targetCluster in $targetClusters)
{
$TargetClusterDropDownBox.Items.Add($targetCluster.Name) #Add Clusters to DropDown List
}
$TargetClusterDropDownBox.Enabled = $true
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
$TargetClusterDropDownBox.Enabled = $true
$TargetClusterDropDownBox.SelectedIndex = 0
}
function getTargetDatastores{
try
{
$targetDatastores = Get-Datastore |Where-Object {$_.Type -eq "VMFS"} #Returns all datastores
$TargetDatastoreDropDownBox.Items.Clear()
$TargetDatastoreDropDownBox.Items.Add("Choose Target Datastore...")
$TargetDatastoreDropDownBox.Items.Add("<Keep on a New Recovery Datastore>")
foreach ($targetDatastore in $targetDatastores)
{
$TargetDatastoreDropDownBox.Items.Add($targetDatastore.Name) #Add Datastores to DropDown List
}
$TargetDatastoreDropDownBox.Enabled = $true
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
$TargetDatastoreDropDownBox.Enabled = $true
$TargetDatastoreDropDownBox.SelectedIndex = 0
}
function getProtectionGroups{
try
{
$script:pgroups = Get-PfaProtectionGroups -Array $endpoints[$PgroupFADropDownBox.SelectedIndex-1]
if ($TabControl.SelectedIndex -eq 3)
{
$PgroupPGDropDownBox.Enabled = $false
$PgroupPGDropDownBox.Items.Clear()
if ($pgroups -ne $null)
{
$PgroupPGDropDownBox.Items.Add("Choose a Protection Group...")
foreach ($pg in $script:pgroups)
{
$PgroupPGDropDownBox.Items.Add($pg.name)
$PgroupPGDropDownBox.Enabled = $true
}
}
else
{
$PgroupPGDropDownBox.Items.Add("No Protection Groups found")
}
$PgroupPGDropDownBox.SelectedIndex = 0
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function getProtectionGroupSnapshots{
try
{
if ($TabControl.SelectedIndex -eq 3)
{
$selectedPG = $PgroupPGDropDownBox.SelectedItem.ToString()
if ($selectedPG -ne "Choose a Protection Group...")
{
if ($PgroupPGDropDownBox.SelectedItem.ToString() -like "*:*")
{
if ($script:currentPGisRemote -eq $true)
{
$script:pgFaChanged = $false
}
else
{
$script:pgFaChanged = $true
}
$script:currentPGisRemote = $true
}
else
{
if ($script:currentPGisRemote -eq $true)
{
$script:pgFaChanged = $true
}
else
{
$script:pgFaChanged = $false
}
$script:currentPGisRemote = $false
}
$script:pgroupsnapshots = Get-PfaProtectionGroupSnapshots -Array $endpoints[$PgroupFADropDownBox.SelectedIndex-1] -Name $selectedPG
$buttonCreatePgroupSnap.Enabled = $true
$PgroupSnapDropDownBox.Enabled = $false
$PgroupSnapDropDownBox.Items.Clear()
if ($script:pgroupsnapshots -ne $null)
{
$PgroupSnapDropDownBox.Items.Add("Choose a Snapshot Group...")
foreach ($pgsnapgroup in $script:pgroupsnapshots)
{
$dateconvert = get-date $pgsnapgroup.created
$PgroupSnapDropDownBox.Items.Add("$($dateconvert) $($pgsnapgroup.name)")
$PgroupSnapDropDownBox.Enabled = $true
}
}
else
{
$PgroupSnapDropDownBox.Items.Add("No Snapshot Groups found")
}
$PgroupSnapDropDownBox.SelectedIndex = 0
getPgroupDatastores
$script:atChoosePgroup = $false
}
else
{
$script:atChoosePgroup = $true
$PgroupSnapDropDownBox.Enabled = $false
$PgroupSnapDropDownBox.Items.Clear()
$SnapshotCheckedListBox.Items.Clear()
$SnapshotCheckedListBox.Enabled=$false
$PgroupSnapDropDownBox.Enabled = $false
$PgroupSnapDropDownBox.Items.Clear()
$PgroupClusterDropDownBox.Items.Clear()
$buttonRecoverPgroup.Enabled = $false
$PgroupClusterDropDownBox.Enabled=$false
$SnapshotCheckedListBox.Items.Clear()
$SnapshotCheckedListBox.Enabled=$false
$registerVMs.Enabled = $false
$registerVMs.Checked = $false
$buttonCreatePgroupSnap.Enabled = $false
$buttonDeletePgroupSnap.Enabled = $false
$AddToPgroupCheckedListBox.Items.Clear()
$AddToPgroupCheckedListBox.Enabled=$false
}
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function getPiTSnapshots{
try
{
if (($PgroupSnapDropDownBox.SelectedItem.ToString() -ne "Choose a Snapshot Group...") -and ($PgroupSnapDropDownBox.SelectedItem.ToString() -ne "No Snapshot Groups found"))
{
$selectedPiT = $script:pgroupsnapshots[$PgroupSnapDropDownBox.SelectedIndex-1].name
$script:volumeSnapshots = Get-PfaVolumeSnapshots -Array $endpoints[$PgroupFADropDownBox.SelectedIndex-1] -VolumeName * |where-object {$_.name -like "$($selectedPiT).*"}
$SnapshotCheckedListBox.Items.Clear()
$SnapshotCheckedListBox.Enabled=$false
$buttonDeletePgroupSnap.Enabled = $true
$SnapshotCheckedListBox.Items.Add("Select All")
foreach ($volumeSnapshot in $script:volumeSnapshots)
{
$SnapshotCheckedListBox.Items.Add($volumeSnapshot.name)
}
$SnapshotCheckedListBox.Enabled = $true
$SnapshotCheckedListBox.SetItemchecked(0,$true)
for ($i=1;$i -lt $SnapshotCheckedListBox.Items.count;$i++)
{
$SnapshotCheckedListBox.SetItemchecked($i,$true)
}
getClusters
}
else
{
$SnapshotCheckedListBox.Items.Clear()
$SnapshotCheckedListBox.Enabled=$false
$PgroupClusterDropDownBox.Items.Clear()
$PgroupClusterDropDownBox.Enabled=$false
$SnapshotCheckedListBox.Items.Clear()
$SnapshotCheckedListBox.Enabled=$false
$registerVMs.Enabled = $false
$registerVMs.Checked = $false
$buttonRecoverPgroup.Enabled = $false
$buttonDeletePgroupSnap.Enabled = $false
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function getPgroupDatastores{
try
{
if ($PgroupPGDropDownBox.SelectedItem.ToString() -like "*:*")
{
$AddToPgroupCheckedListBox.Items.Clear()
$AddToPgroupCheckedListBox.Enabled=$false
}
elseif (($script:pgFaChanged -eq $true) -or ($script:atChoosePgroup -eq $true))
{
$arraySN = Get-PfaArrayAttributes -Array $endpoints[$PgroupFADropDownBox.SelectedIndex-1]
$arraySN = $arraySN.id.substring(0,18)
$arraySN = $arraySN -replace '-',''
$script:datastoresOnFA = get-datastore | where-object {($_.ExtensionData.Info.Vmfs.Extent.DiskName |select-object -unique) -like "*$($arraySN)*"}
$AddToPgroupCheckedListBox.Items.Clear()
$AddToPgroupCheckedListBox.Enabled=$false
$AddToPgroupCheckedListBox.Items.Add("Select All")
if ($datastoresOnFA -ne $null)
{
foreach ($datastoreOnFA in $datastoresOnFA)
{
$AddToPgroupCheckedListBox.Items.Add($datastoreOnFA.name)
}
$AddToPgroupCheckedListBox.Enabled = $true
}
else
{
$AddToPgroupCheckedListBox.Items.Add("No datastores found from this FA.")
}
$script:pgFaChanged = $false
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function getHosts{
try
{
$AddHostDropDownBox.Items.Clear()
$esxihosts = get-cluster -name $HostClusterDropDownBox.SelectedItem.ToString() |get-vmhost
foreach ($esxihost in $esxihosts)
{
$AddHostDropDownBox.Items.Add($esxihost.Name) #Add hosts to DropDown List
}
$AddHostDropDownBox.Enabled = $true
$AddHostDropDownBox.SelectedIndex = 0
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
#Context change functions
function radioSelectChanged{
disableRecoveryItems
$CheckBoxDeleteVMObjectSnapshot.Enabled = $false
$CheckBoxDeleteVMObject.Checked = $false
$CheckBoxDeleteVMObjectSnapshot.Checked = $false
if ($script:RadioButtonVM.Checked -eq $true)
{
if ($groupBoxRecoverVM.text -eq "Restore/Clone Virtual Disk:")
{
$script:groupBoxCloneVM.Controls.Remove($TargetDatastoreDropDownBox)
$script:groupBoxCloneVM.Controls.Remove($TargetVMDropDownBox)
$script:groupBoxCloneVM.Controls.Remove($LabelTargetVM)
$script:groupBoxCloneVM.Controls.Remove($LabelTargetDatastore)
}
elseif($groupBoxRecoverVM.text -eq "Restore/Clone Raw Device Mapping:")
{
$script:groupBoxCloneVM.Controls.Remove($TargetVMDropDownBox)
$script:groupBoxCloneVM.Controls.Remove($LabelTargetVM)
$groupBoxRecoverVM.Controls.Remove($groupBoxRestoreRDM)
$groupBoxRecoverVM.Controls.Add($groupBoxRestoreVM)
}
$script:groupBoxRecoverVM.text = "Restore/Clone Virtual Machine:"
$buttonDeleteVM.Text = "Delete VM"
$script:LabelTargetDatastore.Location = New-Object System.Drawing.Point(5, 20)
$script:LabelTargetCluster.Location = New-Object System.Drawing.Point(5, 45)
$script:TargetDatastoreDropDownBox.Location = New-Object System.Drawing.Size(90,20)
$script:TargetClusterDropDownBox.Location = New-Object System.Drawing.Size(90,45)
$script:groupBoxCloneVM.Controls.Add($TargetDatastoreDropDownBox)
$script:groupBoxCloneVM.Controls.Add($TargetClusterDropDownBox)
$script:groupBoxCloneVM.Controls.Add($LabelTargetCluster)
$script:groupBoxCloneVM.Controls.Add($LabelTargetDatastore)
}
elseif ($script:RadioButtonVMDK.Checked -eq $true)
{
if ($groupBoxRecoverVM.text -eq "Restore/Clone Virtual Machine:")
{
$script:groupBoxCloneVM.Controls.Remove($TargetDatastoreDropDownBox)
$script:groupBoxCloneVM.Controls.Remove($TargetClusterDropDownBox)
$script:groupBoxCloneVM.Controls.Remove($LabelTargetDatastore)
$script:groupBoxCloneVM.Controls.Remove($LabelTargetCluster)
}
elseif($groupBoxRecoverVM.text -eq "Restore/Clone Raw Device Mapping:")
{
$script:groupBoxCloneVM.Controls.Remove($TargetVMDropDownBox)
$script:groupBoxCloneVM.Controls.Remove($LabelTargetVM)
$groupBoxRecoverVM.Controls.Remove($groupBoxRestoreRDM)
$groupBoxRecoverVM.Controls.Add($groupBoxRestoreVM)
}
$script:groupBoxRecoverVM.text = "Restore/Clone Virtual Disk:"
$buttonDeleteVM.Text = "Delete VMDK"
$script:LabelTargetVM.Location = New-Object System.Drawing.Point(5, 20)
$script:LabelTargetDatastore.Location = New-Object System.Drawing.Point(5, 45)
$script:TargetVMDropDownBox.Location = New-Object System.Drawing.Size(90,20)
$script:TargetDatastoreDropDownBox.Location = New-Object System.Drawing.Size(90,45)
$script:groupBoxCloneVM.Controls.Add($TargetDatastoreDropDownBox)
$script:groupBoxCloneVM.Controls.Add($TargetVMDropDownBox)
$script:groupBoxCloneVM.Controls.Add($LabelTargetVM)
$script:groupBoxCloneVM.Controls.Add($LabelTargetDatastore)
}
elseif ($script:RadioButtonRDM.Checked -eq $true)
{
if ($groupBoxRecoverVM.text -eq "Restore/Clone Virtual Machine:")
{
$script:groupBoxCloneVM.Controls.Remove($TargetDatastoreDropDownBox)
$script:groupBoxCloneVM.Controls.Remove($TargetClusterDropDownBox)
$script:groupBoxCloneVM.Controls.Remove($LabelTargetDatastore)
$script:groupBoxCloneVM.Controls.Remove($LabelTargetCluster)
}
elseif ($groupBoxRecoverVM.text -eq "Restore/Clone Virtual Disk:")
{
$script:groupBoxCloneVM.Controls.Remove($TargetDatastoreDropDownBox)
$script:groupBoxCloneVM.Controls.Remove($TargetVMDropDownBox)
$script:groupBoxCloneVM.Controls.Remove($LabelTargetVM)
$script:groupBoxCloneVM.Controls.Remove($LabelTargetDatastore)
}
$groupBoxRecoverVM.Controls.Remove($groupBoxRestoreVM)
$groupBoxRecoverVM.Controls.Add($groupBoxRestoreRDM)
$script:groupBoxRecoverVM.text = "Restore/Clone Raw Device Mapping:"
$buttonDeleteVM.Text = "Delete RDM"
$script:LabelTargetVM.Location = New-Object System.Drawing.Point(5, 30)
$script:TargetVMDropDownBox.Location = New-Object System.Drawing.Size(90,30)
$script:groupBoxCloneVM.Controls.Add($TargetVMDropDownBox)
$script:groupBoxCloneVM.Controls.Add($LabelTargetVM)
}
if ($script:RadioButtonVM.Checked -eq $true)
{
if (($VMDropDownBox.SelectedItem.ToString() -ne "Choose VM...") -and ($VMDropDownBox.SelectedItem.ToString() -ne "No VMs found"))
{
getVMSnapshots
}
if ($script:vmdkChosen -eq $true)
{
$script:groupBoxVM.Controls.Remove($groupBoxVMDK)
$script:vmdkChosen = $false
}
if ($script:rdmChosen -eq $true)
{
$script:groupBoxVM.Controls.Remove($groupBoxRDM)
$script:rdmChosen = $false
}
$CheckBoxDeleteVMObject.Text = "I confirm that I want to delete this ENTIRE virtual machine"
$groupBoxDeleteVMObject.text = "Delete Virtual Machine:"
}
elseif ($script:RadioButtonVMDK.Checked -eq $true)
{
if ($VMDropDownBox.SelectedItem.ToString() -ne "Choose VM...")
{
getDisks
}
if ($script:rdmChosen -eq $true)
{
$script:groupBoxVM.Controls.Remove($groupBoxRDM)
$script:rdmChosen = $false
}
$script:vmdkChosen = $true
$script:groupBoxVM.Controls.Add($groupBoxVMDK)
$CheckBoxDeleteVMObject.Text = "I confirm that I want to delete this virtual disk"
$groupBoxDeleteVMObject.text = "Delete Virtual Disk:"
}
elseif ($script:RadioButtonRDM.Checked -eq $true)
{
if ($VMDropDownBox.SelectedItem.ToString() -ne "Choose VM...")
{
getDisks
}
if ($script:vmdkChosen -eq $true)
{
$script:groupBoxVM.Controls.Remove($groupBoxVMDK)
$script:vmdkChosen = $false
}
$script:rdmChosen = $true
$script:groupBoxVM.Controls.Add($groupBoxRDM)
$CheckBoxDeleteVMObject.Text = "I confirm that I want to delete this raw device mapping"
$groupBoxDeleteVMObject.text = "Delete Raw Device Mapping:"
}
enableVMDetails
}
function vmDiskSelectionChanged{
if (($script:RadioButtonVMDK.Checked -eq $true) -and ($VMDKDropDownBox.SelectedItem.ToString() -ne "Choose a VMDK...") -and ($VMDKDropDownBox.SelectedItem.ToString() -ne "No virtual disks found") )
{
$CheckBoxDeleteVMObject.Enabled = $true
getVMSnapshots
disableRecoveryItems
}
elseif (($script:RadioButtonRDM.Checked -eq $true) -and ($RDMDropDownBox.SelectedItem.ToString() -ne "Choose a RDM...") -and ($RDMDropDownBox.SelectedItem.ToString() -ne "No raw device mappings found") )
{
$CheckBoxDeleteVMObject.Enabled = $true
getVMSnapshots
disableRecoveryItems
}
else
{
$CheckBoxDeleteVMObject.Enabled = $false
$CheckBoxDeleteVMObject.Enabled = $false
$VMSnapshotDropDownBox.Enabled = $false
$VMSnapshotDropDownBox.Items.Clear()
$buttonGetVMSnapshots.Enabled = $false
disableRecoveryItems
}
if (($script:RadioButtonVMDK.Checked -eq $true) -and ($VMDKDropDownBox.SelectedItem.ToString() -eq "Choose a VMDK..."))
{
$newVMSnapshotTextBox.Enabled = $false
disableRecoveryItems
}
elseif (($script:RadioButtonRDM.Checked -eq $true) -and ($RDMDropDownBox.SelectedItem.ToString() -eq "Choose a RDM..."))
{
$newVMSnapshotTextBox.Enabled = $false
disableRecoveryItems
}
$CheckBoxDeleteVMObject.Checked = $false
}
function isSnapshotTextChanged{
if ($TabControl.SelectedIndex -eq 0)
{
$LabelNewSnapError.Text = ""
if (($newSnapshotTextBox.Text -notmatch "^[\w\-]+$") -and ($newSnapshotTextBox.Text -ne ""))
{
$LabelNewSnapError.ForeColor = "Red"
$LabelNewSnapError.Text = "The snapshot name must only be letters, numbers or dashes"
$buttonNewSnapshot.Enabled = $false
}
elseif($newVMSnapshotTextBox.Text -eq "")
{
$buttonNewSnapshot.Enabled = $false
}
else
{
$buttonNewSnapshot.Enabled = $true
}
}
elseif ($TabControl.SelectedIndex -eq 1)
{
$LabelNewVMSnapError.Text = ""
if (($newVMSnapshotTextBox.Text -notmatch "^[\w\-]+$") -and ($newVMSnapshotTextBox.Text -ne ""))
{
$LabelNewVMSnapError.ForeColor = "Red"
$LabelNewVMSnapError.Text = "The snapshot name must only be letters, numbers or dashes"
$buttonNewVMSnapshot.Enabled = $false
}
elseif($newVMSnapshotTextBox.Text -eq "")
{
$buttonNewVMSnapshot.Enabled = $false
}
else
{
$buttonNewVMSnapshot.Enabled = $true
}
}
}
function datastoreSelectionChanged{
if (($DatastoreDropDownBox.Enabled -eq $true) -and ($DatastoreDropDownBox.SelectedItem.ToString() -ne "<No Datastores Found>") -and ($DatastoreDropDownBox.SelectedItem.ToString() -ne "Choose VMFS..."))
{
getSnapshots
$buttonSnapshots.Enabled = $true
}
else
{
$SnapshotDropDownBox.Items.Clear()
$buttonSnapshots.Enabled = $false
$newSnapshotTextBox.Enabled = $false
$buttonDeleteVMFS.Enabled = $false
$newSnapshotTextBox.Text = ""
$SnapshotDropDownBox.Enabled = $false
}
}
function vmSelectionChanged{
if (($VMDropDownBox.Enabled -eq $true) -and ($VMDropDownBox.SelectedItem.ToString() -ne "<No Virtual Machines Found>") -and ($VMDropDownBox.SelectedItem.ToString() -ne "Choose VM..."))
{
$CheckBoxDeleteVMObject.Enabled = $false
if ($script:RadioButtonVM.Checked -eq $true)
{
if (($VMDropDownBox.SelectedItem.ToString() -ne "Choose VM...") -and ($VMDropDownBox.SelectedItem.ToString() -ne "No VMs found"))
{
$CheckBoxDeleteVMObject.Enabled = $true
}
}
$CheckBoxDeleteVMObject.Checked = $false
$CheckBoxDeleteVMObjectSnapshot.Checked = $false
$CheckBoxDeleteVMObjectSnapshot.Enabled = $false
getDisks
if ($script:RadioButtonVM.Checked -eq $true)
{
if (($VMDropDownBox.SelectedItem.ToString() -ne "Choose VM...") -and ($VMDropDownBox.SelectedItem.ToString() -ne "No VMs found"))
{
getVMSnapshots
}
}
}
else
{
$CheckBoxDeleteVMObject.Enabled = $false
$CheckBoxDeleteVMObjectSnapshot.Enabled = $false
$CheckBoxDeleteVMObject.Checked = $false
$CheckBoxDeleteVMObjectSnapshot.Checked = $false
$VMSnapshotDropDownBox.Items.Clear()
$VMSnapshotDropDownBox.Enabled = $false
$buttonGetVMSnapshots.Enabled = $false
disableRecoveryItems
if ($script:RadioButtonVMDK.Checked -eq $true)
{
$script:VMDKDropDownBox.Items.Clear()
$script:VMDKDropDownBox.Enabled = $false
$CheckBoxDeleteVMObject.Enabled = $false
}
elseif ($script:RadioButtonRDM.Checked -eq $true)
{
$script:RDMDropDownBox.Items.Clear()
$script:RDMDropDownBox.Enabled = $false
$CheckBoxDeleteVMObject.Enabled = $false
}
}
}
function isVMFSTextChanged{
$LabelNewVMFSError.Text = ""
if (($newVMFSTextBox.Text -notmatch "^[\w\-]+$") -and ($newVMFSTextBox.Text -ne ""))
{
$LabelNewVMFSError.ForeColor = "Red"
$LabelNewVMFSError.Text = "The VMFS name must only be letters, numbers or dashes"
$buttonNewVMFS.Enabled = $false
$script:nameIsValid = $false
}
elseif ($newVMFSTextBox.Text -eq "")
{
$script:nameIsValid = $false
enableCreateVMFS
}
else
{
$script:nameIsValid = $true
enableCreateVMFS
}
}
function isVCTextChanged{
if (($passwordTextBox.Text.Length -gt 0) -and ($usernameTextBox.Text.Length -gt 0) -and ($ServerTextBox.Text.Length -gt 0) )
{
$buttonConnect.enabled = $true
}
else
{
$buttonConnect.enabled = $false
}
}
function isFATextChanged{
if (($flasharrayPasswordTextBox.Text.Length -gt 0) -and ($flasharrayUsernameTextBox.Text.Length -gt 0) -and ($flasharrayTextBox.Text.Length -gt 0) )
{
$flasharrayButtonConnect.enabled = $true
}
else
{
$flasharrayButtonConnect.enabled = $false
}
}
function snapshotChanged{
try
{
if (($SnapshotDropDownBox.SelectedItem.ToString() -ne "Choose a Snapshot...") -and ($SnapshotDropDownBox.Enabled -eq $true))
{
$buttonDelete.Enabled = $true
$newSnapshotTextBox.Text = ""
$LabelNewSnapError.Text = ""
$RecoveryClusterDropDownBox.Enabled=$true
getRecoveryClusters
}
else
{
$buttonDelete.Enabled = $false
$RecoveryClusterDropDownBox.Enabled=$false
$RecoveryClusterDropDownBox.Items.Clear()
$newSnapshotTextBox.Text = ""
$LabelNewSnapError.Text = ""
$buttonRecover.Enabled = $false
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function vmSnapshotChanged{
try
{
if (($VMSnapshotDropDownBox.SelectedItem.ToString() -ne "Choose a Snapshot...") -and ($VMSnapshotDropDownBox.Enabled -eq $true))
{
$CheckBoxDeleteVMObjectSnapshot.Enabled = $true
$buttonRestoreVM.Enabled = $true
$buttonRestoreRDM.Enabled = $true
$migrateVMCheckBox.Enabled = $true
if ($script:RadioButtonVM.Checked -eq $true)
{
getTargetDatastores
}
elseif ($script:RadioButtonVMDK.Checked -eq $true)
{
getTargetVMs
}
elseif ($script:RadioButtonRDM.Checked -eq $true)
{
getTargetVMs
}
}
else
{
$CheckBoxDeleteVMObjectSnapshot.Enabled = $false
$CheckBoxDeleteVMObjectSnapshot.Checked = $false
$buttonRestoreVM.Enabled = $false
$buttonRestoreRDM.Enabled = $false
$migrateVMCheckBox.Enabled = $false
disableRecoveryItems
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function FlashArrayChanged{
if ($FlashArrayDropDownBox.SelectedItem.ToString() -eq "Add new FlashArray...")
{
if ($endpoints.count -eq 0)
{
$FlashArrayDropDownBox.Enabled = $false
}
$flasharrayTextBox.Text = ""
$flasharrayPasswordTextBox.Text = ""
$flasharrayUsernameTextBox.Text = ""
$flasharrayTextBox.Enabled = $true
$flasharrayPasswordTextBox.Enabled = $true
$flasharrayUsernameTextBox.Enabled = $true
$flasharrayButtonDisconnect.enabled = $false
$flasharrayButtonConnect.enabled = $false
}
else
{
$flasharrayTextBox.Text = $FlashArrayDropDownBox.SelectedItem.ToString()
$flasharrayPasswordTextBox.Text = ""
$flasharrayUsernameTextBox.Text = ""
$flasharrayTextBox.Enabled = $false
$flasharrayPasswordTextBox.Enabled = $false
$flasharrayUsernameTextBox.Enabled = $false
$flasharrayButtonDisconnect.enabled = $true
$flasharrayButtonConnect.enabled = $false
}
}
function sizeChanged{
$LabelNewVMFSSizeError.Text = ""
if (($newVMFSSizeTextBox.Text -notmatch "^[\d]+$") -and ($newVMFSSizeTextBox.Text -ne ""))
{
$LabelNewVMFSSizeError.ForeColor = "Red"
$LabelNewVMFSSizeError.Text = " The size must be a whole integer greater than zero"
$buttonNewVMFS.Enabled = $false
$script:sizeIsValid = $false
}
elseif (($newVMFSSizeTextBox.Text -eq 0) -and ($newVMFSSizeTextBox.Text -ne ""))
{
$LabelNewVMFSSizeError.ForeColor = "Red"
$LabelNewVMFSSizeError.Text = " The size must be greater than zero"
$buttonNewVMFS.Enabled = $false
$script:sizeIsValid = $false
}
elseif ($newVMFSSizeTextBox.Text -eq "")
{
$script:sizeIsValid = $false
enableCreateVMFS
}
else
{
$script:sizeIsValid = $true
enableCreateVMFS
}
}
function vmDeleteCheckedChanged{
if (($CheckBoxDeleteVMObject.Checked -eq $false) -or ($CheckBoxDeleteVMObject.Enabled -eq $false))
{
$buttonDeleteVM.Enabled = $false
}
elseif ($CheckBoxDeleteVMObject.Checked -eq $true)
{
$buttonDeleteVM.Enabled = $true
}
}
function vmDeleteSnapshotCheckedChanged{
if (($CheckBoxDeleteVMObjectSnapshot.Checked -eq $false) -or ($CheckBoxDeleteVMObjectSnapshot.Enabled -eq $false))
{
$buttonDeleteVMSnapshot.Enabled = $false
}
elseif ($CheckBoxDeleteVMObjectSnapshot.Checked -eq $true)
{
$buttonDeleteVMSnapshot.Enabled = $true
}
}
function targetClusterSelectionChanged{
if ($script:RadioButtonVM.Checked -eq $true)
{
if (($targetClusterDropDownBox.SelectedItem.ToString() -ne "Choose Target Cluster...") -and ($targetClusterDropDownBox.Enabled -eq $true))
{
$buttonCloneVM.Enabled = $true
}
elseif (($targetClusterDropDownBox.SelectedItem.ToString() -eq "Choose Target Cluster...") -or ($targetClusterDropDownBox.Enabled -eq $false))
{
$buttonCloneVM.Enabled = $false
}
}
}
function targetVMSelectionChanged{
if ($script:RadioButtonVMDK.Checked -eq $true)
{
if (($targetVMDropDownBox.SelectedItem.ToString() -ne "Choose Target VM...") -and ($targetVMDropDownBox.Enabled -eq $true))
{
getTargetDatastores
}
elseif (($targetVMDropDownBox.SelectedItem.ToString() -eq "Choose Target VM...") -or ($targetVMDropDownBox.Enabled -eq $false))
{
$TargetDatastoreDropDownBox.Items.Clear()
$TargetDatastoreDropDownBox.Enabled = $false
$buttonCloneVM.Enabled = $false
}
}
if ($script:RadioButtonRDM.Checked -eq $true)
{
if (($targetVMDropDownBox.SelectedItem.ToString() -ne "Choose Target VM...") -and ($targetVMDropDownBox.Enabled -eq $true))
{
$buttonCloneVM.Enabled = $true
}
elseif (($targetVMDropDownBox.SelectedItem.ToString() -eq "Choose Target VM...") -or ($targetVMDropDownBox.Enabled -eq $false))
{
$buttonCloneVM.Enabled = $false
}
}
}
function targetDatastoreSelectionChanged{
if ($script:RadioButtonVM.Checked -eq $true)
{
if (($targetDatastoreDropDownBox.SelectedItem.ToString() -ne "Choose Target Datastore...") -and ($targetDatastoreDropDownBox.Enabled -eq $true))
{
getTargetClusters
}
elseif (($targetDatastoreDropDownBox.SelectedItem.ToString() -eq "Choose Target Datastore...") -or ($targetDatastoreDropDownBox.Enabled -eq $false))
{
$TargetClusterDropDownBox.Items.Clear()
$TargetClusterDropDownBox.Enabled = $false
$buttonCloneVM.Enabled = $false
}
}
elseif ($script:RadioButtonVMDK.Checked -eq $true)
{
if (($targetDatastoreDropDownBox.SelectedItem.ToString() -ne "Choose Target Datastore...") -and ($targetDatastoreDropDownBox.Enabled -eq $true))
{
$buttonCloneVM.Enabled = $true
}
elseif (($targetDatastoreDropDownBox.SelectedItem.ToString() -eq "Choose Target Datastore...") -or ($targetDatastoreDropDownBox.Enabled -eq $false))
{
$buttonCloneVM.Enabled = $false
}
}
}
function clusterConfigSelectionChanged{
try
{
if (($HostClusterDropDownBox.SelectedItem.ToString() -ne "Choose a Cluster...") -and ($HostFlashArrayDropDownBox.SelectedItem.ToString() -ne "Choose a FlashArray..."))
{
$script:RadioButtoniSCSI.Enabled = $true
$script:RadioButtoniSCSI.Checked = $true
$script:RadioButtonFC.Enabled = $true
$buttonCreateHostGroup.Enabled = $true
$script:RadioButtonHostiSCSI.Enabled = $true
$script:RadioButtonHostiSCSI.Checked = $true
$script:RadioButtonHostFC.Enabled = $true
$buttonAddHosts.Enabled = $true
$buttonConfigureiSCSI.Enabled = $true
$buttonConfigureSATP.Enabled = $true
getHosts
}
else
{
$script:RadioButtoniSCSI.Enabled = $false
$script:RadioButtoniSCSI.Checked = $false
$script:RadioButtonFC.Checked = $false
$script:RadioButtonFC.Enabled = $false
$buttonCreateHostGroup.Enabled = $false
$buttonConfigureiSCSI.Enabled = $false
$buttonConfigureSATP.Enabled = $false
$script:RadioButtonHostiSCSI.Enabled = $false
$script:RadioButtonHostiSCSI.Checked = $false
$script:RadioButtonHostFC.Enabled = $false
$buttonAddHosts.Enabled = $false
$AddHostDropDownBox.Enabled = $false
$AddHostDropDownBox.Items.Clear()
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function pgroupFAChanged{
if ($PgroupFADropDownBox.SelectedItem.ToString() -ne "Choose a FlashArray...")
{
getProtectionGroups
$script:pgFaChanged = $true
}
else
{
$PgroupPGDropDownBox.Enabled = $false
$PgroupPGDropDownBox.Items.Clear()
$PgroupSnapDropDownBox.Enabled = $false
$PgroupSnapDropDownBox.Items.Clear()
$PgroupClusterDropDownBox.Items.Clear()
$PgroupClusterDropDownBox.Enabled=$false
$SnapshotCheckedListBox.Items.Clear()
$SnapshotCheckedListBox.Enabled=$false
$registerVMs.Enabled = $false
$buttonRecoverPgroup.Enabled = $false
$registerVMs.Checked = $false
$buttonCreatePgroupSnap.Enabled = $false
$buttonDeletePgroupSnap.Enabled = $false
$AddToPgroupCheckedListBox.Items.Clear()
$AddToPgroupCheckedListBox.Enabled=$false
}
}
function snapshotSelectAll{
if ($This.SelectedItem -eq 'Select All')
{
If ($This.GetItemCheckState(0) -ne 'Checked')
{
for ($i=1;$i -lt $SnapshotCheckedListBox.Items.count;$i++)
{
$SnapshotCheckedListBox.SetItemchecked($i,$true)
}
if ($PgroupClusterDropDownBox.Enabled -eq $false)
{
getClusters
}
}
else
{
for ($i=1;$i -lt $SnapshotCheckedListBox.Items.count;$i++)
{
$SnapshotCheckedListBox.SetItemchecked($i,$false)
}
$PgroupClusterDropDownBox.Items.Clear()
$PgroupClusterDropDownBox.Enabled=$false
$registerVMs.Enabled = $false
$buttonRecoverPgroup.Enabled = $false
$registerVMs.Checked = $false
}
}
else
{
if ($This.GetItemCheckState($This.SelectedIndex) -ne 'Checked')
{
if ($PgroupClusterDropDownBox.Enabled -eq $false)
{
getClusters
}
}
elseif($SnapshotCheckedListBox.CheckedItems.count -eq 1)
{
$PgroupClusterDropDownBox.Items.Clear()
$PgroupClusterDropDownBox.Enabled=$false
$registerVMs.Enabled = $false
$buttonRecoverPgroup.Enabled = $false
$registerVMs.Checked = $false
}
elseif ($This.GetItemCheckState($This.SelectedIndex) -eq 'Checked')
{
if ($SnapshotCheckedListBox.GetItemCheckState(0) -eq 'Checked')
{
$SnapshotCheckedListBox.SetItemchecked(0,$false)
}
}
}
}
function enableAddtoPG{
if ($This.SelectedItem -eq 'Select All')
{
If ($This.GetItemCheckState(0) -ne 'Checked')
{
for ($i=1;$i -lt $AddToPgroupCheckedListBox.Items.count;$i++)
{
$AddToPgroupCheckedListBox.SetItemchecked($i,$true)
}
$buttonAddVMFStoPgroup.Enabled = $true
}
else
{
for ($i=1;$i -lt $AddToPgroupCheckedListBox.Items.count;$i++)
{
$AddToPgroupCheckedListBox.SetItemchecked($i,$false)
}
$buttonAddVMFStoPgroup.Enabled = $false
}
}
else
{
if ($This.GetItemCheckState($This.SelectedIndex) -ne 'Checked')
{
$buttonAddVMFStoPgroup.Enabled = $true
}
elseif($AddToPgroupCheckedListBox.CheckedItems.count -eq 1)
{
$buttonAddVMFStoPgroup.Enabled = $false
}
elseif ($This.GetItemCheckState($This.SelectedIndex) -eq 'Checked')
{
if ($AddToPgroupCheckedListBox.GetItemCheckState(0) -eq 'Checked')
{
$AddToPgroupCheckedListBox.SetItemchecked(0,$false)
}
}
}
}
function clusterSelectionChanged{
enableObjects
}
function registerVMchanged{
if ($registerVMs.Checked -eq $false)
{
$powerOnVMs.Enabled = $false
$powerOnVMs.Checked = $false
}
else
{
$powerOnVMs.Enabled = $true
}
}
#Enable functions
function enableCreateVMFS{
if (($nameIsValid -eq $true) -and ($sizeIsValid -eq $true) -and ($ChooseFlashArrayDropDownBox.SelectedItem.ToString() -ne "Choose a FlashArray...") -and ($CreateVMFSClusterDropDownBox.SelectedItem.ToString() -ne "Choose a Cluster..."))
{
$buttonNewVMFS.Enabled = $true
}
else
{
$buttonNewVMFS.Enabled = $false
}
}
function enableVMDetails{
if (($VMDropDownBox.Enabled -eq $true) -and ($VMDropDownBox.SelectedItem.ToString() -ne "<No Virtual Machines Found>") -and ($VMDropDownBox.SelectedItem.ToString() -ne "Choose VM..."))
{
if ($script:RadioButtonVM.Checked -eq $true)
{
}
elseif ($script:RadioButtonVMDK.Checked -eq $true)
{
$VMDKDropDownBox.Enabled=$true
$CheckBoxDeleteVMObject.Enabled=$false
}
elseif ($script:RadioButtonRDM.Checked -eq $true)
{
$RDMDropDownBox.Enabled=$true
$CheckBoxDeleteVMObject.Enabled=$false
}
getDisks
}
elseif (($VMDropDownBox.SelectedItem.ToString() -eq "<No Virtual Machines Found>") -and ($VMDropDownBox.SelectedItem.ToString() -eq "Choose VM..."))
{
$VMDKDropDownBox.Enabled=$false
$RDMDropDownBox.Enabled=$false
$CheckBoxDeleteVMObject.Enabled=$false
$CheckBoxDeleteVMObject.Enabled=$false
}
}
function enableRecovery{
if (($RecoveryClusterDropDownBox.SelectedItem.ToString() -eq "Choose a Cluster...") -or ($RecoveryClusterDropDownBox.Enabled -eq $false))
{
$buttonRecover.Enabled = $false
}
else
{
$buttonRecover.Enabled = $true
}
}
function enableObjects{
disableObjects
if ($TabControl.SelectedIndex -eq 0)
{
if ($script:lastTab -ieq "host")
{
$script:main_form.Controls.Remove($groupBoxChooseHost)
$script:main_form.Controls.Add($groupBoxRadio)
}
elseif ($script:lastTab -ieq "pgroup")
{
$script:main_form.Controls.Remove($groupBoxFilterPgroup)
$script:main_form.Controls.Add($groupBoxRadio)
}
$script:lastTab = "vmfs"
$ChooseFlashArrayDropDownBox.Enabled=$true
$CreateVMFSClusterDropDownBox.Enabled=$true
$newVMFSTextBox.Enabled = $true
$newVMFSSizeTextBox.Enabled = $true
$UnitDropDownBox.Enabled=$true
$buttonDatastores.Enabled = $true
$ClusterDropDownBox.Enabled=$true
$nameFilterTextBox.Enabled = $true
listFlashArrays
getDatastores
$getVMFSCluster = $true
getRecoveryClusters
$getVMFSCluster = $false
}
elseif($TabControl.SelectedIndex -eq 1)
{
if ($script:lastTab -ieq "host")
{
$script:main_form.Controls.Remove($groupBoxChooseHost)
$script:main_form.Controls.Add($groupBoxRadio)
}
elseif ($script:lastTab -ieq "pgroup")
{
$script:main_form.Controls.Remove($groupBoxFilterPgroup)
$script:main_form.Controls.Add($groupBoxRadio)
}
$script:lastTab = "vm"
$VMDropDownBox.Enabled = $true
$buttonVMs.Enabled = $true
$ClusterDropDownBox.Enabled=$true
$nameFilterTextBox.Enabled = $true
$RadioButtonVM.Enabled = $true
$RadioButtonVMDK.Enabled = $true
$RadioButtonRDM.Enabled = $true
getVMs
$script:RadioButtonVM.Checked = $true
}
elseif($TabControl.SelectedIndex -eq 2)
{
if (($script:lastTab -ieq "vm") -or ($script:lastTab -ieq "vmfs"))
{
$script:main_form.Controls.Remove($groupBoxRadio)
$script:main_form.Controls.Add($groupBoxChooseHost)
}
elseif ($script:lastTab -ieq "pgroup")
{
$script:main_form.Controls.Remove($groupBoxFilterPgroup)
$script:main_form.Controls.Add($groupBoxChooseHost)
}
$script:lastTab = "host"
getclusters
listFlashArrays
}
elseif($TabControl.SelectedIndex -eq 3)
{
if ($script:lastTab -ieq "host")
{
$script:main_form.Controls.Remove($groupBoxChooseHost)
$script:main_form.Controls.Add($groupBoxFilterPgroup)
}
elseif (($script:lastTab -ieq "vm") -or ($script:lastTab -ieq "vmfs"))
{
$script:main_form.Controls.Remove($groupBoxRadio)
$script:main_form.Controls.Add($groupBoxFilterPgroup)
}
$script:lastTab = "pgroup"
listFlashArrays
}
}
function disableObjects{
if ($TabControl.SelectedIndex -eq 0)
{
$buttonDatastores.Enabled = $false
$buttonDeleteVMFS.Enabled = $false
$buttonSnapshots.Enabled = $false
$buttonNewSnapshot.Enabled = $false
$buttonNewVMFS.Enabled = $false
$buttonRecover.Enabled = $false
$buttonDelete.Enabled = $false
$nameFilterTextBox.Enabled = $false
$newSnapshotTextBox.Enabled = $false
$newVMFSTextBox.Enabled = $false
$newVMFSSizeTextBox.Enabled = $false
$ClusterDropDownBox.Enabled=$false
$DatastoreDropDownBox.Enabled=$false
$UnitDropDownBox.Enabled=$false
$CreateVMFSClusterDropDownBox.Enabled=$false
$ChooseFlashArrayDropDownBox.Enabled=$false
$RecoveryClusterDropDownBox.Enabled=$false
$SnapshotDropDownBox.Enabled=$false
}
elseif($TabControl.SelectedIndex -eq 1)
{
$VMDropDownBox.Enabled = $false
$buttonVMs.Enabled = $false
$ClusterDropDownBox.Enabled=$false
$nameFilterTextBox.Enabled = $false
$RadioButtonVM.Enabled = $false
$RadioButtonVMDK.Enabled = $false
$RadioButtonRDM.Enabled = $false
}
elseif($TabControl.SelectedIndex -eq 2)
{
$HostFlashArrayDropDownBox.Enabled = $false
$HostFlashArrayDropDownBox.Items.Clear()
$HostClusterDropDownBox.Items.Clear()
$HostClusterDropDownBox.Enabled = $false
}
elseif($TabControl.SelectedIndex -eq 3)
{
$PgroupPGDropDownBox.Enabled = $false
$PgroupPGDropDownBox.Items.Clear()
$PgroupSnapDropDownBox.Enabled = $false
$PgroupSnapDropDownBox.Items.Clear()
$SnapshotCheckedListBox.Items.Clear()
$SnapshotCheckedListBox.Enabled=$false
$buttonRecoverPgroup.Enabled = $false
$PgroupSnapDropDownBox.Enabled = $false
$PgroupSnapDropDownBox.Items.Clear()
$PgroupClusterDropDownBox.Items.Clear()
$PgroupClusterDropDownBox.Enabled=$false
$SnapshotCheckedListBox.Items.Clear()
$SnapshotCheckedListBox.Enabled=$false
$registerVMs.Enabled = $false
$buttonCreatePgroupSnap.Enabled = $false
$registerVMs.Checked = $false
$buttonDeletePgroupSnap.Enabled = $false
$AddToPgroupCheckedListBox.Items.Clear()
$AddToPgroupCheckedListBox.Enabled=$false
}
}
function disableRecoveryItems{
$script:TargetDatastoreDropDownBox.Items.Clear()
$script:TargetDatastoreDropDownBox.Enabled = $false
$script:buttonCloneVM.Enabled = $false
$script:TargetClusterDropDownBox.Items.Clear()
$script:TargetClusterDropDownBox.Enabled = $false
$script:TargetVMDropDownBox.Items.Clear()
$script:TargetVMDropDownBox.Enabled = $false
$script:buttonRestoreVM.Enabled = $false
$script:buttonRestoreRDM.Enabled = $false
$migrateVMCheckBox.Enabled = $false
}
function pgroupcheckboxes{
if (($PgroupClusterDropDownBox.SelectedItem.ToString() -ne "Choose a Recovery Cluster...") -and ($SnapshotCheckedListBox.CheckedItems.count -ge 1))
{
$registerVMs.Enabled = $true
$buttonRecoverPgroup.Enabled = $true
}
else
{
$registerVMs.Enabled = $false
$registerVMs.Checked = $false
$powerOnVMs.Enabled = $false
$powerOnVMs.Checked = $false
$buttonRecoverPgroup.Enabled = $false
}
}
#Operation Functions
function addHost{
try
{
if ($HostFlashArrayDropDownBox.SelectedItem.ToString() -eq "Select All FlashArrays")
{
foreach ($FAendpoint in $endpoints)
{
$script:endpoint = $FAendpoint
getHostGroup
createHost
}
}
else
{
foreach ($FAendpoint in $endpoints)
{
if ($HostFlashArrayDropDownBox.SelectedItem.ToString() -eq $FAendpoint.endpoint)
{
$script:endpoint = $FAendpoint
break
}
}
getHostGroup
createHost
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function deleteVMObject{
if ($script:RadioButtonVM.Checked -eq $true)
{
deleteVM
}
elseif ($script:RadioButtonVMDK.Checked -eq $true)
{
deleteVMDK
}
elseif ($script:RadioButtonRDM.Checked -eq $true)
{
deleteRDM
}
}
function deleteSnapshot{
try
{
$deletedSnap = Remove-PfaVolumeOrSnapshot -Array $endpoint -Name $snapshots[$SnapshotDropDownBox.SelectedIndex-1].name
$outputTextBox.text = ((get-Date -Format G) + " Deleted snapshot $($deletedSnap.name)`r`n$($outputTextBox.text)")
getSnapshots
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function deleteVMSnapshot{
try
{
$deletedSnap = Remove-PfaVolumeOrSnapshot -Array $endpoint -Name $snapshots[$VMSnapshotDropDownBox.SelectedIndex-1].name
$outputTextBox.text = ((get-Date -Format G) + " Deleted snapshot $($deletedSnap.name)`r`n$($outputTextBox.text)")
getVMSnapshots
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function deleteVMFS{
$outputTextBox.text = ((get-Date -Format G) + " Deleting datastore $($DatastoreDropDownBox.SelectedItem.ToString())...`r`n$($outputTextBox.text)")
try
{
$deleteVmfs = get-datastore $DatastoreDropDownBox.SelectedItem.ToString() -ErrorAction stop
$vms = $deleteVmfs |get-vm -ErrorAction stop
$templates = $deleteVmfs |get-template -ErrorAction stop
if (($vms.count -eq 0) -and ($templates.count -eq 0))
{
$lun = $deleteVmfs.ExtensionData.Info.Vmfs.Extent.DiskName |select-object -unique
$volumes = Get-PfaVolumes -Array $EndPoint
$volserial = ($lun.ToUpper()).substring(12)
$deletevol = $volumes | where-object { $_.serial -eq $volserial }
if ($deletevol -eq $null)
{
$outputTextBox.text = "ERROR: Volume not found on connected FlashArray." + ("`r`n") + $outputTextBox.text
return
}
$FAhostgroups = Get-PfaVolumeHostGroupConnections -Array $endpoint -VolumeName $deletevol.name
$FAhosts = Get-PfaVolumeHostConnections -Array $endpoint -VolumeName $deletevol.name
$esxihosts = $deleteVmfs |get-vmhost -ErrorAction stop
$FAhostgroups = $FAhostgroups.hgroup |Select-Object -unique
$FAhosts = $FAhosts.host |Select-Object -unique
$outputTextBox.text = ((get-Date -Format G) + " Unmounting and detaching volume from each ESXi host...`r`n$($outputTextBox.text)")
foreach ($esxihost in $esxihosts)
{
$storageSystem = Get-View $esxihost.Extensiondata.ConfigManager.StorageSystem -ErrorAction stop
$StorageSystem.UnmountVmfsVolume($deleteVmfs.ExtensionData.Info.vmfs.uuid)
$storageSystem.DetachScsiLun((Get-ScsiLun -VmHost $esxihost -ErrorAction stop| where {$_.CanonicalName -eq $deleteVmfs.ExtensionData.Info.Vmfs.Extent.DiskName}).ExtensionData.Uuid)
}
foreach ($FAhost in $FAhosts)
{
Remove-PfaHostVolumeConnection -Array $endpoint -VolumeName $deletevol.name -HostName $FAhost
}
foreach ($FAhostgroup in $FAhostgroups)
{
Remove-PfaHostGroupVolumeConnection -Array $endpoint -VolumeName $deletevol.name -HostGroupName $FAhostgroup
}
Remove-PfaVolumeOrSnapshot -Array $endpoint -Name $deletevol.name
$outputTextBox.text = ((get-Date -Format G) + " The FlashArray volume $($deletevol.name) has been deleted`r`n$($outputTextBox.text)")
$outputTextBox.text = ((get-Date -Format G) + " Rescanning cluster...`r`n$($outputTextBox.text)")
$esxihosts | Get-VMHostStorage -RescanAllHba -RescanVMFS -ErrorAction stop
$outputTextBox.text = ((get-Date -Format G) + " COMPLETED: The datastore and FlashArray volume has been deleted`r`n$($outputTextBox.text)")
getDatastores
}
else
{
$outputTextBox.text = ((get-Date -Format G) + " ERROR: Cannot delete this VMFS as there are virtual machines and/or templates using it.`r`n$($outputTextBox.text)")
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function deleteVM{
try
{
$vm = get-vm -Name $VMDropDownBox.SelectedItem.ToString() -ErrorAction stop
$vm |remove-vm -DeletePermanently -Confirm:$false -ErrorAction stop
$outputTextBox.text = ((get-Date -Format G) + " COMPLETE: Deleted VM $($VMDropDownBox.SelectedItem.ToString())`r`n$($outputTextBox.text)")
getVMs
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function deleteVMDK{
try
{
$vm = get-vm -Name $VMDropDownBox.SelectedItem.ToString() -ErrorAction stop
$filepath = ($VMDKDropDownBox.SelectedItem.ToString().Split("(")[0])
$filepath = $filepath.Substring(0,$filepath.Length-1)
$disk = $vm | get-harddisk |where-object { $_.Filename -eq $filepath } -ErrorAction stop
if ($disk.ExtensionData.Backing.Sharing -eq "sharingMultiWriter")
{
$VMDKinUse = $false
$outputTextBox.text = ((get-Date -Format G) + " WARNING. VMDK is detected in Shared Multi-Writer Mode. Could be in use by another VM. Checking...`r`n$($outputTextBox.text)")
$vms = $disk |Get-Datastore| get-vm
foreach ($vmdkVM in $vms)
{
$tempDisk = $vmdkVM |get-harddisk |where-object { $_.Filename -eq $filepath } -ErrorAction stop
if (($tempDisk -ne $null) -and ($vmdkVM.name -ne $vm.name))
{
$outputTextBox.text = ((get-Date -Format G) + " RDM is also in use by VM $($vmdkVM.Name).`r`n$($outputTextBox.text)")
$VMDKinUse = $true
}
}
if ($VMDKinUse -eq $true)
{
$outputTextBox.text = ((get-Date -Format G) + " VMDK is detected as in-use by other VMs. Only removing VMDK from this VM, but not deleting it.`r`n$($outputTextBox.text)")
disk |remove-harddisk -Confirm:$false -ErrorAction stop
$outputTextBox.text = ((get-Date -Format G) + " COMPLETE: VMDK removed.`r`n$($outputTextBox.text)")
}
else
{
$outputTextBox.text = ((get-Date -Format G) + " No other VMs have been detected using this VMDK. Will continue with removing and deleting it.`r`n$($outputTextBox.text)")
}
}
if (($disk.ExtensionData.Backing.Sharing -ne "sharingMultiWriter") -or ($VMDKinUse -eq $false) )
{
$outputTextBox.text = ((get-Date -Format G) + " Removing VMDK from VM...`r`n$($outputTextBox.text)")
$disk | remove-harddisk -DeletePermanently -Confirm:$false -ErrorAction stop
$outputTextBox.text = ((get-Date -Format G) + " COMPLETE: VMDK removed and deleted.`r`n$($outputTextBox.text)")
}
getDisks
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function deleteRDM{
try
{
$outputTextBox.text = ((get-Date -Format G) + " Removing RDM and destroying the FlashArray volume...`r`n$($outputTextBox.text)")
$vm = get-vm -Name $VMDropDownBox.SelectedItem.ToString() -ErrorAction stop
$naa = (($RDMDropDownBox.SelectedItem.ToString()).substring(0,36))
$disk = $vm | get-harddisk |where-object { $_.ScsiCanonicalName -eq $naa } -ErrorAction stop
if ($disk.ExtensionData.Backing.Sharing -eq "sharingMultiWriter")
{
$RDMinUse = $false
$outputTextBox.text = ((get-Date -Format G) + " WARNING. RDM is detected in Shared Multi-Writer Mode. Could be in use by another VM. Checking...`r`n$($outputTextBox.text)")
$vms = $disk |Get-Datastore| get-vm
foreach ($rdmVM in $vms)
{
$tempDisk = $rdmVM |get-harddisk |where-object { $_.ScsiCanonicalName -eq $naa } -ErrorAction stop
if (($tempDisk -ne $null) -and ($rdmVM.name -ne $vm.Name))
{
$outputTextBox.text = ((get-Date -Format G) + " RDM is also in use by VM $($rdmVM.Name).`r`n$($outputTextBox.text)")
$RDMinUse = $true
}
}
if ($RDMinUse -eq $true)
{
$outputTextBox.text = ((get-Date -Format G) + " RDM is detected as in-use by other VMs. Only removing RDM from this VM, but not deleting it.`r`n$($outputTextBox.text)")
$disk |remove-harddisk -Confirm:$false -ErrorAction stop
$outputTextBox.text = ((get-Date -Format G) + " COMPLETE: RDM removed.`r`n$($outputTextBox.text)")
}
else
{
$outputTextBox.text = ((get-Date -Format G) + " No other VMs have been detected using this RDM. Will continue with removing and deleting it.`r`n$($outputTextBox.text)")
}
}
if (($disk.ExtensionData.Backing.Sharing -ne "sharingMultiWriter") -or ($RDMinUse -eq $false) )
{
$disk |remove-harddisk -Confirm:$false -DeletePermanently -ErrorAction stop
$outputTextBox.text = ((get-Date -Format G) + " Removed RDM from VM. Now detaching the volume`r`n$($outputTextBox.text)")
$lun = $naa
$volumes = Get-PfaVolumes -Array $EndPoint
$volserial = ($lun.ToUpper()).substring(12)
$deletevol = $volumes | where-object { $_.serial -eq $volserial }
if ($deletevol -eq $null)
{
$outputTextBox.text = "ERROR: Volume not found on connected FlashArray." + ("`r`n") + $outputTextBox.text
return
}
$esxihosts = get-vmhost
$hostsToDetach = @()
foreach ($esxihost in $esxihosts)
{
$scsilun = $esxihost | get-scsilun |where-object { $_.CanonicalName -eq $naa }
if ($scsilun.count -ne 0)
{
$hostsToDetach += $esxihost
}
}
$outputTextBox.text = ((get-Date -Format G) + " Detaching device from hosts...`r`n$($outputTextBox.text)")
foreach ($esxihost in $hostsToDetach)
{
$storageSystem = Get-View $esxihost.Extensiondata.ConfigManager.StorageSystem -ErrorAction stop
$storageSystem.DetachScsiLun($naa)
}
$FAhostgroups = Get-PfaVolumeHostGroupConnections -Array $endpoint -VolumeName $deletevol.name | select -ExpandProperty hgroup |select-object -unique
$FAhosts = Get-PfaVolumeHostConnections -Array $endpoint -VolumeName $deletevol.name | select -ExpandProperty hgroup |select-object -unique
foreach ($FAhost in $FAhosts)
{
Remove-PfaHostVolumeConnection -Array $endpoint -VolumeName $deletevol.name -HostName $FAhost
}
foreach ($FAhostgroup in $FAhostgroups)
{
Remove-PfaHostGroupVolumeConnection -Array $endpoint -VolumeName $deletevol.name -HostGroupName $FAhostgroup
}
Remove-PfaVolumeOrSnapshot -Array $endpoint -Name $deletevol.name
$outputTextBox.text = ((get-Date -Format G) + " The FlashArray volume $($deletevol.name) has been deleted`r`n$($outputTextBox.text)")
$outputTextBox.text = ((get-Date -Format G) + " Rescanning cluster...`r`n$($outputTextBox.text)")
$hostsToDetach | Get-VMHostStorage -RescanAllHba -RescanVMFS -ErrorAction stop
$outputTextBox.text = ((get-Date -Format G) + " COMPLETE: The RDM volume has been removed and destroyed.`r`n$($outputTextBox.text)")
getDisks
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function createHostGroup{
if ($HostFlashArrayDropDownBox.SelectedItem.ToString() -eq "Select All FlashArrays")
{
$flasharrays = $endpoints
}
else
{
foreach ($endpoint in $endpoints)
{
if ($HostFlashArrayDropDownBox.SelectedItem.ToString() -eq $endpoint.endpoint)
{
$flasharrays = $endpoint
}
}
}
$esxihosts = get-cluster -name $HostClusterDropDownBox.SelectedItem.ToString() |get-vmhost
$clustername = $HostClusterDropDownBox.SelectedItem.ToString()
$outputTextBox.text = ((get-Date -Format G) + " Creating host groups for the cluster $($clustername) on $($flasharrays.count) FlashArrays`r`n$($outputTextBox.text)")
foreach ($flasharray in $flasharrays)
{
try
{
$fahosts = Get-PFAHosts -array $flasharray -ErrorAction Stop
$fahostgroups = Get-PFAHostGroups -array $flasharray -ErrorAction Stop
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " ERROR: Could not obtain host information from the FlashArray. Check FlashArray for errors. Skipping the FlashArray named $($flasharray.endpoint)`r`n$($outputTextBox.text)")
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
return
}
$iqnexist = $false
$wwnsexist = $false
foreach ($esxihost in $esxihosts)
{
if ($RadioButtoniSCSI.Checked -eq $true)
{
$iscsiadapter = $esxihost | Get-VMHostHBA -Type iscsi | Where {$_.Model -eq "iSCSI Software Adapter"}
if ($iscsiadapter -eq $null)
{
$outputTextBox.text = ((get-Date -Format G) + " ERROR: No Software iSCSI adapter found on host " + $esxihost.NetworkInfo.HostName + ". No changes were made.`r`n$($outputTextBox.text)")
return
}
else
{
$iqn = $iscsiadapter.ExtensionData.IScsiName
foreach ($esxi in $fahosts)
{
if ($esxi.iqn.count -ge 1)
{
foreach($fahostiqn in $esxi.iqn)
{
if ($iqn.ToLower() -eq $fahostiqn.ToLower())
{
$hostgroupfailed = $true
$iqnexist = $true
}
}
}
}
}
}
elseif ($RadioButtonFC.Checked -eq $true)
{
$wwns = $esxihost | Get-VMHostHBA -Type FibreChannel | Select VMHost,Device,@{N="WWN";E={"{0:X}" -f $_.PortWorldWideName}} | Format-table -Property WWN -HideTableHeaders |out-string
$wwns = (($wwns.Replace("`n","")).Replace("`r","")).Replace(" ","")
$wwns = &{for ($i = 0;$i -lt $wwns.length;$i += 16)
{
$wwns.substring($i,16)
}}
if ($wwns -eq $null)
{
$outputTextBox.text = ((get-Date -Format G) + " No FC WWNs found on host $($esxihost.NetworkInfo.HostName). No changes were made.`r`n$($outputTextBox.text)")
return
}
else
{
foreach ($wwn in $wwns)
{
foreach ($esxi in $fahosts)
{
if ($esxi.wwn.count -ge 1)
{
foreach($fahostwwn in $esxi.wwn)
{
if ($wwn.ToLower() -eq $fahostwwn.ToLower())
{
$outputTextBox.text = ((get-Date -Format G) + " ERROR: The ESXi WWN $($wwn) already exists on the FlashArray.`r`n$($outputTextBox.text)")
$wwnsexist = $true
}
}
}
}
}
}
}
}
if (($iqnexist -eq $true) -or ($wwnsexist -eq $true))
{
$outputTextBox.text = ((get-Date -Format G) + " ERROR: An ESXi host's initiators have been found on this FlashArray. Skipping the FlashArray named $($flasharray.endpoint)`r`n$($outputTextBox.text)")
continue
}
else
{
$outputTextBox.text = ((get-Date -Format G) + " Creating hosts on the FlashArray $($flasharray.endpoint)`r`n$($outputTextBox.text)")
$createhostfail = $false
$newfahosts = @()
if ($RadioButtoniSCSI.Checked -eq $true)
{
foreach ($esxihost in $esxihosts)
{
if ($createhostfail -eq $false)
{
$iscsiadapter = $esxihost | Get-VMHostHBA -Type iscsi | Where {$_.Model -eq "iSCSI Software Adapter"}
$iqn = $iscsiadapter.ExtensionData.IScsiName
try
{
$newfahosts += New-PfaHost -Array $flasharray -Name $esxihost.NetworkInfo.HostName -IqnList $iqn -ErrorAction stop
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " ERROR: The host $($esxihost.NetworkInfo.HostName) failed to create. Review error. Cleaning up this FlashArray and moving on.`r`n$($outputTextBox.text)")
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
$hostgroupfailed = $true
$createhostfail = $true
if ($newfahosts.count -ge 1)
{
$outputTextBox.text = ((get-Date -Format G) + " Deleting the $($newfahosts.count) hosts on this FlashArray that were just created by this script..`r`n$($outputTextBox.text)")
foreach ($removehost in $newfahosts)
{
Remove-PfaHost -Array $flasharray -Name $removehost.Name |out-null
}
}
}
}
}
}
elseif ($RadioButtonFC.Checked -eq $true)
{
foreach ($esxihost in $esxihosts)
{
if ($createhostfail -eq $false)
{
$wwns = $esxihost | Get-VMHostHBA -Type FibreChannel | Select VMHost,Device,@{N="WWN";E={"{0:X}" -f $_.PortWorldWideName}} | Format-table -Property WWN -HideTableHeaders |out-string
$wwns = (($wwns.Replace("`n","")).Replace("`r","")).Replace(" ","")
$wwns = &{for ($i = 0;$i -lt $wwns.length;$i += 16)
{
$wwns.substring($i,16)
}}
try
{
$newfahosts += New-PfaHost -Array $flasharray -Name $esxihost.NetworkInfo.HostName -ErrorAction stop
foreach ($wwn in $wwns)
{
if ($createhostfail -eq $false)
{
try
{
Add-PfaHostWwns -Array $flasharray -Name $esxihost.NetworkInfo.HostName -AddWwnList $wwn -ErrorAction stop |out-null
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " ERROR: The host $($esxihost.NetworkInfo.HostName) failed to create. Review error. Cleaning up this FlashArray and moving on.`r`n$($outputTextBox.text)")
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
$hostgroupfailed = $true
$createhostfail = $true
if ($newfahosts.count -ge 1)
{
$outputTextBox.text = ((get-Date -Format G) + " Deleting the $($newfahosts.count) hosts on this FlashArray that were just created by this script..`r`n$($outputTextBox.text)")
foreach ($removehost in $newfahosts)
{
Remove-PfaHost -Array $flasharray -Name $removehost.Name |out-null
}
}
}
}
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + "ERROR: The host $($esxihost.NetworkInfo.HostName) failed to create. Review error. Cleaning up this FlashArray and moving on.`r`n$($outputTextBox.text)")
$hostgroupfailed = $true
$outputTextBox.text = ((get-Date -Format G) + " $($error[0])`r`n$($outputTextBox.text)")
$createhostfail = $true
if ($newfahosts.count -ge 1)
{
$outputTextBox.text = ((get-Date -Format G) + " Deleting the $($newfahosts.count) hosts on this FlashArray that were created by this script`r`n$($outputTextBox.text)")
foreach ($removehost in $newfahosts)
{
Remove-PfaHost -Array $flasharray -Name $removehost.Name |out-null
$outputTextBox.text = ((get-Date -Format G) + " Removed host $($removehost.Name)`r`n$($outputTextBox.text)")
}
}
}
}
}
}
if ($createhostfail -eq $false)
{
#FlashArray only supports Alphanumeric or the dash - character in host group names. Checking for VMware cluster name compliance and removing invalid characters.
if ($clustername -notmatch "^[a-zA-Z0-9\-]+$")
{
$clustername = $clustername -replace "[^\w\-]", ""
$clustername = $clustername -replace "[_]", ""
}
$clustersuccess = $false
try
{
$outputTextBox.text = ((get-Date -Format G) + " Creating host group on the FlashArray $($flasharray.endpoint)`r`n$($outputTextBox.text)")
$newcluster = New-PfaHostGroup -Array $flasharray -Name $clustername -ErrorAction stop
$clustersuccess = $true
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
$outputTextBox.text = ((get-Date -Format G) + " ERROR: The host group $($clustername) failed to create. Review error below. Cleaning up this FlashArray and moving on.`r`n$($outputTextBox.text)")
$hostgroupfailed = $true
if ($newfahosts.count -ge 1)
{
$outputTextBox.text = ((get-Date -Format G) + " Deleting the $($newfahosts.count) hosts on this FlashArray that were created by this script`r`n$($outputTextBox.text)")
foreach ($removehost in $newfahosts)
{
Remove-PfaHost -Array $flasharray -Name $removehost.Name |out-null
}
}
}
if ($clustersuccess -eq $true)
{
$outputTextBox.text = ((get-Date -Format G) + " Adding the hosts to the host group`r`n$($outputTextBox.text)")
foreach ($newfahost in $newfahosts)
{
Add-PfaHosts -Array $flasharray -Name $clustername -hoststoadd $newfahost.name |Out-Null
}
$outputTextBox.text = ((get-Date -Format G) + " COMPLETE: Host Group created on FlashArray $($flasharray.endpoint)`r`n$($outputTextBox.text)")
}
}
}
}
}
function createHost{
$esxihost = get-vmhost -name $AddHostDropDownBox.SelectedItem.ToString()
$outputTextBox.text = ((get-Date -Format G) + " Creating host for $($esxihost.name) on FlashArray $($endpoint.endpoint) and adding to host group `r`n$($outputTextBox.text)")
try
{
$fahosts = Get-PFAHosts -array $endpoint -ErrorAction Stop
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " ERROR: Could not obtain host information from the FlashArray. Check FlashArray for errors. `r`n$($outputTextBox.text)")
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
return
}
$iqnexist = $false
$wwnsexist = $false
if ($RadioButtonHostiSCSI.Checked -eq $true)
{
$iscsiadapter = $esxihost | Get-VMHostHBA -Type iscsi | Where {$_.Model -eq "iSCSI Software Adapter"}
if ($iscsiadapter -eq $null)
{
$outputTextBox.text = ((get-Date -Format G) + " ERROR: No Software iSCSI adapter found on host " + $esxihost.NetworkInfo.HostName + ". No changes were made.`r`n$($outputTextBox.text)")
return
}
else
{
$iqn = $iscsiadapter.ExtensionData.IScsiName
foreach ($esxi in $fahosts)
{
if ($esxi.iqn.count -ge 1)
{
foreach($fahostiqn in $esxi.iqn)
{
if ($iqn.ToLower() -eq $fahostiqn.ToLower())
{
$hostgroupfailed = $true
$iqnexist = $true
return
}
}
}
}
}
}
elseif ($RadioButtonHostFC.Checked -eq $true)
{
$wwns = $esxihost | Get-VMHostHBA -Type FibreChannel | Select VMHost,Device,@{N="WWN";E={"{0:X}" -f $_.PortWorldWideName}} | Format-table -Property WWN -HideTableHeaders |out-string
$wwns = (($wwns.Replace("`n","")).Replace("`r","")).Replace(" ","")
$wwns = &{for ($i = 0;$i -lt $wwns.length;$i += 16)
{
$wwns.substring($i,16)
}}
if ($wwns -eq $null)
{
$outputTextBox.text = ((get-Date -Format G) + " No FC WWNs found on host $($esxihost.NetworkInfo.HostName). No changes were made.`r`n$($outputTextBox.text)")
return
}
else
{
foreach ($wwn in $wwns)
{
foreach ($esxi in $fahosts)
{
if ($esxi.wwn.count -ge 1)
{
foreach($fahostwwn in $esxi.wwn)
{
if ($wwn.ToLower() -eq $fahostwwn.ToLower())
{
$outputTextBox.text = ((get-Date -Format G) + " ERROR: The ESXi WWN $($wwn) already exists on the FlashArray.`r`n$($outputTextBox.text)")
$wwnsexist = $true
return
}
}
}
}
}
}
}
$outputTextBox.text = ((get-Date -Format G) + " Creating the host on the FlashArray $($endpoint.endpoint)`r`n$($outputTextBox.text)")
if ($RadioButtonHostiSCSI.Checked -eq $true)
{
$iscsiadapter = $esxihost | Get-VMHostHBA -Type iscsi | Where {$_.Model -eq "iSCSI Software Adapter"}
$iqn = $iscsiadapter.ExtensionData.IScsiName
try
{
$newfahost = New-PfaHost -Array $endpoint -Name $esxihost.NetworkInfo.HostName -IqnList $iqn -ErrorAction stop
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " ERROR: The host $($esxihost.NetworkInfo.HostName) failed to create. Review error.`r`n$($outputTextBox.text)")
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
return
}
}
elseif ($RadioButtonHostFC.Checked -eq $true)
{
$wwns = $esxihost | Get-VMHostHBA -Type FibreChannel | Select VMHost,Device,@{N="WWN";E={"{0:X}" -f $_.PortWorldWideName}} | Format-table -Property WWN -HideTableHeaders |out-string
$wwns = (($wwns.Replace("`n","")).Replace("`r","")).Replace(" ","")
$wwns = &{for ($i = 0;$i -lt $wwns.length;$i += 16)
{
$wwns.substring($i,16)
}}
try
{
$newfahost = New-PfaHost -Array $endpoint -Name $esxihost.NetworkInfo.HostName -ErrorAction stop
foreach ($wwn in $wwns)
{
Add-PfaHostWwns -Array $endpoint -Name $esxihost.NetworkInfo.HostName -AddWwnList $wwn -ErrorAction stop |out-null
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " ********ERROR********`r`n$($outputTextBox.text)")
$outputTextBox.text = ((get-Date -Format G) + " $($error[0])`r`n$($outputTextBox.text)")
$outputTextBox.text = ((get-Date -Format G) + " The host $($esxihost.NetworkInfo.HostName) failed to create. Review error.`r`n$($outputTextBox.text)")
return
}
}
try
{
Add-PfaHosts -Array $endpoint -Name $hostgroup -hoststoadd $newfahost.name |Out-Null
$outputTextBox.text = ((get-Date -Format G) + " COMPLETE: Host added to host group on FlashArray $($endpoint.endpoint)`r`n$($outputTextBox.text)")
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " ********ERROR********`r`n$($outputTextBox.text)")
$outputTextBox.text = ((get-Date -Format G) + " $($error[0])`r`n$($outputTextBox.text)")
$outputTextBox.text = ((get-Date -Format G) + " The host failed to add to the host group. Review error.`r`n$($outputTextBox.text)")
$outputTextBox.text = ((get-Date -Format G) + " Removing the earlier created host`r`n$($outputTextBox.text)")
Remove-PfaHost -Array $endpoint -Name $newfahost.name |out-null
return
}
}
function newSnapshot{
try
{
if ($TabControl.SelectedIndex -eq 0)
{
$LabelNewSnapError.Text = ""
$datastore = get-datastore $DatastoreDropDownBox.SelectedItem.ToString()
$script:lun = $datastore.ExtensionData.Info.Vmfs.Extent.DiskName |select-object -unique
}
elseif ($TabControl.SelectedIndex -eq 1)
{
$LabelNewVMSnapError.Text = ""
if ($RadioButtonVM.Checked -eq $true)
{
$datastore = get-vm -Name $VMDropDownBox.SelectedItem.ToString() |Get-Datastore
$script:lun = $datastore.ExtensionData.Info.Vmfs.Extent.DiskName |select-object -unique
if ($datastore.count -gt 1)
{
throw "This VM uses more than one datastore and is not supported in this tool"
}
}
if ($RadioButtonVMDK.Checked -eq $true)
{
$vm = get-vm -Name $VMDropDownBox.SelectedItem.ToString()
$datastore = get-datastore (($script:VMDKDropDownBox.SelectedItem.ToString() |foreach { $_.Split("]")[0] }).substring(1))
$script:lun = $datastore.ExtensionData.Info.Vmfs.Extent.DiskName |select-object -unique
}
if ($RadioButtonRDM.Checked -eq $true)
{
$vm = get-vm -Name $VMDropDownBox.SelectedItem.ToString()
$script:lun = ($script:RDMDropDownBox.SelectedItem.ToString()).substring(0,36)
}
}
getFlashArray
if ($lun -like 'naa.624a9370*')
{
$volumes = Get-PfaVolumes -Array $EndPoint
$volserial = ($lun.ToUpper()).substring(12)
$script:purevol = $volumes | where-object { $_.serial -eq $volserial }
if ($purevol -eq $null)
{
$outputTextBox.text = (get-Date -Format G) + " ERROR: Volume not found on connected FlashArray." + ("`r`n") + $outputTextBox.text
}
else
{
if ($TabControl.SelectedIndex -eq 0)
{
$newSnapshot = New-PfaVolumeSnapshots -Array $EndPoint -Sources $purevol.name -Suffix $newSnapshotTextBox.Text -ErrorAction stop
$newSnapshotTextBox.Text = ""
$LabelNewSnapError.ForeColor = "Black"
$LabelNewSnapError.Text = "$($newSnapshot.Name) ($($newSnapshot.Created))"
$buttonNewSnapshot.Enabled = $false
getSnapshots
}
elseif ($TabControl.SelectedIndex -eq 1)
{
$newSnapshot = New-PfaVolumeSnapshots -Array $EndPoint -Sources $purevol.name -Suffix $newVMSnapshotTextBox.Text -ErrorAction stop
$newVMSnapshotTextBox.Text = ""
$LabelNewVMSnapError.ForeColor = "Black"
$LabelNewVMSnapError.Text = "$($newSnapshot.Name) ($($newSnapshot.Created))"
$buttonNewVMSnapshot.Enabled = $false
getVMSnapshots
}
$outputTextBox.text = ((get-Date -Format G) + " Created snapshot $($newSnapshot.Name)`r`n$($outputTextBox.text)")
}
}
else
{
$outputTextBox.text = (get-Date -Format G) + " Selected datastore is not a FlashArray volume." + ("`r`n") + $outputTextBox.text
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
}
}
function newVMFS{
$script:createVMFS = $true
getHostGroup
try
{
$newvol = New-PfaVolume -Array $endpoints[$ChooseFlashArrayDropDownBox.SelectedIndex-1] -VolumeName $newVMFSTextBox.text -Size $($newVMFSSizeTextBox.text) $($UnitDropDownBox.SelectedItem.ToString())
$outputTextBox.text = ((get-Date -Format G) + " New FlashArray volume is $($newvol.name)`r`n$($outputTextBox.text)")
New-PfaHostGroupVolumeConnection -Array $endpoints[$ChooseFlashArrayDropDownBox.SelectedIndex-1] -VolumeName $newvol.name -HostGroupName $hostgroup
$outputTextBox.text = ((get-Date -Format G) + " Connected volume to host group $($hostgroup)`r`n$($outputTextBox.text)")
$outputTextBox.text = ((get-Date -Format G) + " Rescanning cluster...`r`n$($outputTextBox.text)")
$esxi = get-cluster -Name $CreateVMFSClusterDropDownBox.SelectedItem.ToString() | Get-VMHost -ErrorAction stop
$esxi | Get-VMHostStorage -RescanAllHba -RescanVMFS -ErrorAction stop
$newNAA = "naa.624a9370" + $newvol.serial.toLower()
$ESXiApiVersion = $esxi[0].ExtensionData.Summary.Config.Product.ApiVersion
if ($ESXiApiVersion -eq "6.5")
{
$outputTextBox.text = ((get-Date -Format G) + " Creating a VMFS 6 datastore...`r`n$($outputTextBox.text)")
$newVMFS = $esxi[0] |new-datastore -name $newVMFSTextBox.text -vmfs -Path $newNAA -FileSystemVersion 6
}
else
{
$outputTextBox.text = ((get-Date -Format G) + " Creating a VMFS 5 datastore...`r`n$($outputTextBox.text)")
$newVMFS = $esxi[0] |new-datastore -name $newVMFSTextBox.text -vmfs -Path $newNAA -FileSystemVersion 5
}
$outputTextBox.text = ((get-Date -Format G) + " COMPLETE: VMFS datastore $($newVMFSTextBox.text) created.`r`n$($outputTextBox.text)")
$newVMFSTextBox.Text = ""
$newVMFSSizeTextBox.Text = ""
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " FAILED: Datastore creation failed.`r`n$($outputTextBox.text)")
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
if ($newvol -ne $null)
{
$outputTextBox.text = ((get-Date -Format G) + " Cleaning up volume... $($Error[0])`r`n$($outputTextBox.text)")
Remove-PfaHostGroupVolumeConnection -Array $endpoints[$ChooseFlashArrayDropDownBox.SelectedIndex-1] -VolumeName $newvol.name -HostGroupName $hostgroup
Remove-PfaVolumeOrSnapshot -Array $endpoints[$ChooseFlashArrayDropDownBox.SelectedIndex-1] -Name $newvol.name
Remove-PfaVolumeOrSnapshot -Array $endpoints[$ChooseFlashArrayDropDownBox.SelectedIndex-1] -Name $newvol.name -Eradicate
$outputTextBox.text = ((get-Date -Format G) + " Rescanning cluster...`r`n$($outputTextBox.text)")
$esxi | Get-VMHostStorage -RescanAllHba -RescanVMFS
$outputTextBox.text = ((get-Date -Format G) + " The recovery datastore has been deleted`r`n$($outputTextBox.text)")
}
}
$script:createVMFS = $false
}
function cloneVMFS{
getFlashArray
getHostGroup
try
{
$snapshotchoice = $SnapshotDropDownBox.SelectedIndex -1
$volumename = $purevol.name + "-snap-" + (Get-Random -Minimum 1000 -Maximum 9999)
$newvol =New-PfaVolume -Array $endpoint -Source $snapshots[$snapshotchoice].name -VolumeName $volumename
$outputTextBox.text = ((get-Date -Format G) + " New FlashArray volume is $($newvol.name)`r`n$($outputTextBox.text)")
New-PfaHostGroupVolumeConnection -Array $endpoint -VolumeName $newvol.name -HostGroupName $hostgroup
$outputTextBox.text = ((get-Date -Format G) + " Connected volume to host group $($hostgroup)`r`n$($outputTextBox.text)")
$outputTextBox.text = ((get-Date -Format G) + " Rescanning cluster...`r`n$($outputTextBox.text)")
$cluster = get-cluster -Name $RecoveryClusterDropDownBox.SelectedItem.ToString() -ErrorAction stop
$cluster | Get-VMHost | Get-VMHostStorage -RescanAllHba -RescanVMFS -ErrorAction stop
$esxi = $cluster | Get-VMHost -ErrorAction stop
$esxcli=get-esxcli -VMHost $esxi[0] -v2 -ErrorAction stop
$resigargs =$esxcli.storage.vmfs.snapshot.list.createargs()
$sourceds = get-datastore $DatastoreDropDownBox.SelectedItem.ToString()
$resigargs.volumelabel = $sourceds.Name
Start-sleep -s 10
$unresolvedvmfs = $esxcli.storage.vmfs.snapshot.list.invoke($resigargs)
if ($unresolvedvmfs.UnresolvedExtentCount -ge 2)
{
throw ("ERROR: There is more than one unresolved copy of the source VMFS named " + $sourceds.Name)
}
else
{
$resigOp = $esxcli.storage.vmfs.snapshot.resignature.createargs()
$resigOp.volumelabel = $resigargs.volumelabel
$outputTextBox.text = ((get-Date -Format G) + " Resignaturing the VMFS...`r`n$($outputTextBox.text)")
$esxcli.storage.vmfs.snapshot.resignature.invoke($resigOp)
Start-sleep -s 10
$cluster | Get-VMHost | Get-VMHostStorage -RescanVMFS -ErrorAction stop
$datastores = $esxi[0] | Get-Datastore -ErrorAction stop
$recoverylun = ("naa.624a9370" + $newvol.serial)
foreach ($ds in $datastores)
{
$naa = $ds.ExtensionData.Info.Vmfs.Extent.DiskName
if ($naa -eq $recoverylun.ToLower())
{
$resigds = $ds
}
}
$resigds = $resigds | Set-Datastore -Name $volumename -ErrorAction stop
$outputTextBox.text = (get-Date -Format G) + " Presented copied VMFS named " + $resigds.name + ("`r`n") + $outputTextBox.text
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
$outputTextBox.text = ((get-Date -Format G) + " Attempting to cleanup recovered datastore...`r`n$($outputTextBox.text)")
if ($unresolvedvmfs.UnresolvedExtentCount -eq 1)
{
$esxihosts = $resigds |get-vmhost
foreach ($esxihost in $esxihosts)
{
$storageSystem = Get-View $esxihost.Extensiondata.ConfigManager.StorageSystem -ErrorAction stop
$StorageSystem.UnmountVmfsVolume($resigds.ExtensionData.Info.vmfs.uuid)
$storageSystem.DetachScsiLun((Get-ScsiLun -VmHost $esxihost | where {$_.CanonicalName -eq $resigds.ExtensionData.Info.Vmfs.Extent.DiskName}).ExtensionData.Uuid)
}
}
Remove-PfaHostGroupVolumeConnection -Array $endpoint -VolumeName $newvol.name -HostGroupName $hostgroup
Remove-PfaVolumeOrSnapshot -Array $endpoint -Name $newvol.name
Remove-PfaVolumeOrSnapshot -Array $endpoint -Name $newvol.name -Eradicate
$outputTextBox.text = ((get-Date -Format G) + " Rescanning cluster...`r`n$($outputTextBox.text)")
$cluster | Get-VMHost | Get-VMHostStorage -RescanAllHba -RescanVMFS -ErrorAction stop
$outputTextBox.text = ((get-Date -Format G) + " The recovery datastore has been deleted`r`n$($outputTextBox.text)")
return
}
}
function restoreVMObject{
getFlashArray
$cloneObject = $false
if ($RadioButtonRDM.Checked -eq $false)
{
getHostGroup
try
{
$snapshotchoice = $VMSnapshotDropDownBox.SelectedIndex -1
$volumename = $purevol.name + "-snap-" + (Get-Random -Minimum 1000 -Maximum 9999)
$newvol =New-PfaVolume -Array $endpoint -Source $snapshots[$snapshotchoice].name -VolumeName $volumename
$outputTextBox.text = ((get-Date -Format G) + " New FlashArray volume is $($newvol.name)`r`n$($outputTextBox.text)")
New-PfaHostGroupVolumeConnection -Array $endpoint -VolumeName $newvol.name -HostGroupName $hostgroup
$outputTextBox.text = ((get-Date -Format G) + " Connected volume to host group $($hostgroup)`r`n$($outputTextBox.text)")
$outputTextBox.text = ((get-Date -Format G) + " Rescanning cluster...`r`n$($outputTextBox.text)")
$vm = get-vm -Name $VMDropDownBox.SelectedItem.ToString() -ErrorAction stop
$vm |get-cluster | Get-VMHost | Get-VMHostStorage -RescanAllHba -RescanVMFS -ErrorAction stop
$cluster = $vm |get-cluster
$esxi = $cluster | Get-VMHost -ErrorAction stop
$esxcli=get-esxcli -VMHost $esxi[0] -v2 -ErrorAction stop
$resigargs =$esxcli.storage.vmfs.snapshot.list.createargs()
$sourceds = $vm |get-datastore
if ($sourceds.count -eq 1)
{
$resigargs.volumelabel = $sourceds.Name
}
elseif ($RadioButtonVMDK.Checked -eq $true)
{
$vmdkpath = ($script:VMDKDropDownBox.SelectedItem.ToString().Split("(")[0])
$resigargs.volumelabel = ($vmdkpath.Split("]")[0]).substring(1)
}
elseif ($sourceds.count -gt 1)
{
throw "This VM has more than one datastore and is not a supported configuration for this tool"
}
Start-sleep -s 10
$unresolvedvmfs = $esxcli.storage.vmfs.snapshot.list.invoke($resigargs)
if ($unresolvedvmfs.UnresolvedExtentCount -ge 2)
{
throw ("ERROR: There is more than one unresolved copy of the source VMFS named " + $sourceds.Name)
}
else
{
$resigOp = $esxcli.storage.vmfs.snapshot.resignature.createargs()
$resigOp.volumelabel = $resigargs.volumelabel
$outputTextBox.text = ((get-Date -Format G) + " Resignaturing the VMFS...`r`n$($outputTextBox.text)")
$esxcli.storage.vmfs.snapshot.resignature.invoke($resigOp)
Start-sleep -s 10
$cluster | Get-VMHost | Get-VMHostStorage -RescanVMFS -ErrorAction stop
$datastores = $esxi[0] | Get-Datastore -ErrorAction stop
$recoverylun = ("naa.624a9370" + $newvol.serial)
foreach ($ds in $datastores)
{
$naa = $ds.ExtensionData.Info.Vmfs.Extent.DiskName
if ($naa -eq $recoverylun.ToLower())
{
$resigds = $ds
}
}
$resigds = $resigds | Set-Datastore -Name $volumename -ErrorAction stop
$outputTextBox.text = (get-Date -Format G) + " Presented copied VMFS named " + $resigds.name + ("`r`n") + $outputTextBox.text
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
if (($sourceds.count -eq 1) -or ($RadioButtonVMDK.Checked -eq $true))
{
$outputTextBox.text = ((get-Date -Format G) + " Attempting to cleanup recovered datastore...`r`n$($outputTextBox.text)")
if ($unresolvedvmfs.UnresolvedExtentCount -eq 1)
{
$esxihosts = $resigds |get-vmhost
foreach ($esxihost in $esxihosts)
{
$storageSystem = Get-View $esxihost.Extensiondata.ConfigManager.StorageSystem -ErrorAction stop
$StorageSystem.UnmountVmfsVolume($resigds.ExtensionData.Info.vmfs.uuid)
$storageSystem.DetachScsiLun((Get-ScsiLun -VmHost $esxihost | where {$_.CanonicalName -eq $resigds.ExtensionData.Info.Vmfs.Extent.DiskName}).ExtensionData.Uuid)
}
}
Remove-PfaHostGroupVolumeConnection -Array $endpoint -VolumeName $newvol.name -HostGroupName $hostgroup
Remove-PfaVolumeOrSnapshot -Array $endpoint -Name $newvol.name
Remove-PfaVolumeOrSnapshot -Array $endpoint -Name $newvol.name -Eradicate
$outputTextBox.text = ((get-Date -Format G) + " Rescanning cluster...`r`n$($outputTextBox.text)")
$cluster | Get-VMHost | Get-VMHostStorage -RescanAllHba -RescanVMFS -ErrorAction stop
$outputTextBox.text = ((get-Date -Format G) + " The recovery datastore has been deleted`r`n$($outputTextBox.text)")
}
return
}
if ($RadioButtonVM.Checked -eq $true)
{
try
{
$vmpath = $vm.extensiondata.Config.Files.VmPathName
$oldname = ($vmpath.Split("]")[0]).substring(1)
$vmpath = $vmpath -replace $oldname, $resigds.name
if ($vm.ExtensionData.summary.runtime.powerState -eq "poweredOn")
{
throw "The source VM is powered-on. Please shut it down prior to recovery"
}
else
{
$outputTextBox.text = ((get-Date -Format G) + " Registering VM from copied datastore...`r`n$($outputTextBox.text)")
$newvm = New-VM -VMHost ($vm |Get-VMHost) -VMFilePath $vmpath -Name ("$($vm.Name)-copy") -ErrorAction stop
}
$outputTextBox.text = ((get-Date -Format G) + " Powering on recovered VM...`r`n$($outputTextBox.text)")
$newvm | start-vm -runasync -ErrorAction stop
Start-Sleep -Seconds 20
$newvm | Get-VMQuestion | Set-VMQuestion -DefaultOption -confirm:$false
$outputTextBox.text = ((get-Date -Format G) + " Removing original VM permanently...`r`n$($outputTextBox.text)")
Start-Sleep -Seconds 4
$vm |remove-vm -DeletePermanently -Confirm:$false -ErrorAction stop
Start-Sleep -Seconds 4
$newvm = $newvm | set-vm -name $vm.name -Confirm:$false -ErrorAction stop
$outputTextBox.text = ((get-Date -Format G) + " COMPLETE: The VM has been recovered and the old VM has been deleted`r`n$($outputTextBox.text)")
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
$outputTextBox.text = ((get-Date -Format G) + " Attempting to cleanup copied datastore...`r`n$($outputTextBox.text)")
if ((!$newvm) -ne $true)
{
$cluster = $newvm |get-cluster
$newvm |remove-vm -DeletePermanently -Confirm:$false
}
if ($vms.count -eq 0)
{
$esxihosts = $resigds |get-vmhost
foreach ($esxihost in $esxihosts)
{
$storageSystem = Get-View $esxihost.Extensiondata.ConfigManager.StorageSystem -ErrorAction stop
$StorageSystem.UnmountVmfsVolume($resigds.ExtensionData.Info.vmfs.uuid)
$storageSystem.DetachScsiLun((Get-ScsiLun -VmHost $esxihost | where {$_.CanonicalName -eq $resigds.ExtensionData.Info.Vmfs.Extent.DiskName}).ExtensionData.Uuid)
}
Remove-PfaHostGroupVolumeConnection -Array $endpoint -VolumeName $newvol.name -HostGroupName $hostgroup
Remove-PfaVolumeOrSnapshot -Array $endpoint -Name $newvol.name
Remove-PfaVolumeOrSnapshot -Array $endpoint -Name $newvol.name -Eradicate
$outputTextBox.text = ((get-Date -Format G) + " Rescanning cluster...`r`n$($outputTextBox.text)")
$cluster | Get-VMHost | Get-VMHostStorage -RescanAllHba -RescanVMFS -ErrorAction stop
$outputTextBox.text = ((get-Date -Format G) + " The recovery datastore has been deleted`r`n$($outputTextBox.text)")
}
return
}
if ($migrateVMCheckBox.Checked -eq $true)
{
try
{
$outputTextBox.text = ((get-Date -Format G) + " Moving the VM to the original datastore...`r`n$($outputTextBox.text)")
Move-vm -vm $newvm -Datastore (get-datastore $oldname) -Confirm:$false -ErrorAction stop
$vms = $resigds |get-vm
if ($vms.count -eq 0)
{
$esxihosts = $resigds |get-vmhost
foreach ($esxihost in $esxihosts)
{
$storageSystem = Get-View $esxihost.Extensiondata.ConfigManager.StorageSystem -ErrorAction stop
$StorageSystem.UnmountVmfsVolume($resigds.ExtensionData.Info.vmfs.uuid)
$storageSystem.DetachScsiLun((Get-ScsiLun -VmHost $esxihost | where {$_.CanonicalName -eq $resigds.ExtensionData.Info.Vmfs.Extent.DiskName}).ExtensionData.Uuid)
}
Remove-PfaHostGroupVolumeConnection -Array $endpoint -VolumeName $newvol.name -HostGroupName $hostgroup
Remove-PfaVolumeOrSnapshot -Array $endpoint -Name $newvol.name
Remove-PfaVolumeOrSnapshot -Array $endpoint -Name $newvol.name -Eradicate
$vm = get-vm -Name $VMDropDownBox.SelectedItem.ToString() -ErrorAction stop
$outputTextBox.text = ((get-Date -Format G) + " Rescanning cluster...`r`n$($outputTextBox.text)")
$vm |get-cluster | Get-VMHost | Get-VMHostStorage -RescanAllHba -RescanVMFS -ErrorAction stop
$outputTextBox.text = ((get-Date -Format G) + " COMPLETE: The VM has been moved and the temporary datastore has been deleted`r`n$($outputTextBox.text)")
$migrateVMCheckBox.Checked = $false
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
return
}
}
}
if ($RadioButtonVMDK.Checked -eq $true)
{
try
{
$filepath = ($VMDKDropDownBox.SelectedItem.ToString().Split("(")[0])
$filepath = $filepath.Substring(0,$filepath.Length-1)
$disk = $vm | get-harddisk |where-object { $_.Filename -eq $filepath } -ErrorAction stop
$controller = $disk |Get-ScsiController -ErrorAction stop
$oldname = ($filepath.Split("]")[0]).substring(1)
$filepath = $filepath -replace $oldname, $resigds.name
$outputTextBox.text = ((get-Date -Format G) + " Removing old VMDK from VM...`r`n$($outputTextBox.text)")
$disk | remove-harddisk -DeletePermanently -Confirm:$false -ErrorAction stop
$outputTextBox.text = ((get-Date -Format G) + " Replacing VMDK from copied datastore...`r`n$($outputTextBox.text)")
$newDisk = $vm | new-harddisk -DiskPath $filepath -Controller $controller -ErrorAction stop
$outputTextBox.text = ((get-Date -Format G) + " COMPLETE: VMDK replaced and restored.`r`n$($outputTextBox.text)")
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
$outputTextBox.text = ((get-Date -Format G) + " Attempting to cleanup recovered datastore...`r`n$($outputTextBox.text)")
if ($vms.count -eq 0)
{
$esxihosts = $resigds |get-vmhost
foreach ($esxihost in $esxihosts)
{
$storageSystem = Get-View $esxihost.Extensiondata.ConfigManager.StorageSystem -ErrorAction stop
$StorageSystem.UnmountVmfsVolume($resigds.ExtensionData.Info.vmfs.uuid)
$storageSystem.DetachScsiLun((Get-ScsiLun -VmHost $esxihost | where {$_.CanonicalName -eq $resigds.ExtensionData.Info.Vmfs.Extent.DiskName}).ExtensionData.Uuid)
}
Remove-PfaHostGroupVolumeConnection -Array $endpoint -VolumeName $newvol.name -HostGroupName $hostgroup
Remove-PfaVolumeOrSnapshot -Array $endpoint -Name $newvol.name
Remove-PfaVolumeOrSnapshot -Array $endpoint -Name $newvol.name -Eradicate
$vm = get-vm -Name $VMDropDownBox.SelectedItem.ToString() -ErrorAction stop
$outputTextBox.text = ((get-Date -Format G) + " Rescanning cluster...`r`n$($outputTextBox.text)")
$vm |get-cluster | Get-VMHost | Get-VMHostStorage -RescanAllHba -RescanVMFS -ErrorAction stop
$outputTextBox.text = ((get-Date -Format G) + " The recovery datastore has been deleted`r`n$($outputTextBox.text)")
}
return
}
if ($migrateVMCheckBox.Checked -eq $true)
{
try
{
$outputTextBox.text = ((get-Date -Format G) + " Moving the VM to the original datastore...`r`n$($outputTextBox.text)")
Move-HardDisk -HardDisk $newDisk -Datastore (get-datastore $oldname) -Confirm:$false -ErrorAction stop
$vms = $resigds |get-vm
if ($vms.count -eq 0)
{
$esxihosts = $resigds |get-vmhost
foreach ($esxihost in $esxihosts)
{
$storageSystem = Get-View $esxihost.Extensiondata.ConfigManager.StorageSystem -ErrorAction stop
$StorageSystem.UnmountVmfsVolume($resigds.ExtensionData.Info.vmfs.uuid)
$storageSystem.DetachScsiLun((Get-ScsiLun -VmHost $esxihost | where {$_.CanonicalName -eq $resigds.ExtensionData.Info.Vmfs.Extent.DiskName}).ExtensionData.Uuid)
}
$outputTextBox.text = ((get-Date -Format G) + " Removing copied datastore...`r`n$($outputTextBox.text)")
Remove-PfaHostGroupVolumeConnection -Array $endpoint -VolumeName $newvol.name -HostGroupName $hostgroup
Remove-PfaVolumeOrSnapshot -Array $endpoint -Name $newvol.name
Remove-PfaVolumeOrSnapshot -Array $endpoint -Name $newvol.name -Eradicate
$vm = get-vm -Name $VMDropDownBox.SelectedItem.ToString() -ErrorAction stop
$outputTextBox.text = ((get-Date -Format G) + " Rescanning cluster...`r`n$($outputTextBox.text)")
$vm |get-cluster | Get-VMHost | Get-VMHostStorage -RescanAllHba -RescanVMFS -ErrorAction stop
$outputTextBox.text = ((get-Date -Format G) + " The recovery datastore has been deleted`r`n$($outputTextBox.text)")
$migrateVMCheckBox.Checked = $false
}
}
catch
{
$outputTextBox.text = ((get-Date -Format G) + " $($Error[0])`r`n$($outputTextBox.text)")
return
}
}
}
}
}
function restoreRDM{
getFlashArray
$cloneObject = $false
try
{
$outputTextBox.text = ((get-Date