Uninstall.ps1
<#
.SYNOPSIS This function uninstalls the virtual appliance. .DESCRIPTION This function uninstalls the virtual appliance. .EXAMPLE UnInstall-NetAppCBSAppliance Description --------------------------------------- Uninstalls the virtual appliance #> function uninstall-netapp-cbs-appliance { BEGIN { Write-Verbose -Message "In begin block of UnInstall-NetAppCBSAppliance" Write-DebugLog -Message "In begin block of UnInstall-NetAppCBSAppliance" $SCDPRole = "SCDP Administrator" $userName = "NetAppSCDPAdmin" $domain = "VSPHERE.LOCAL" $tagName = "AVS_ANF_CLOUD_ADMIN_VM_TAG" $tagCategoryName = "AVS_ANF_CLOUD_ADMIN_VM_TAG_CATEGORY" } PROCESS { Invoke-PreflightNetAppCBSAVSCheck Write-Verbose -Message "UnInstall-NetAppCBSAppliance execution is started" Write-DebugLog -Message "UnInstall-NetAppCBSAppliance execution is started" Write-Host "Uninstallation is started." $ApplianceVirtualMachineArray = [System.Collections.ArrayList]@() Write-Verbose -Message "Checking if the VM exists" $tagAssignment = $null try { $tagAssignment = Get-TagAssignment -Tag $tagName -Category $tagCategoryName -ErrorAction SilentlyContinue } catch { if ($tagAssignment) { Write-DebugLog "Inside catch block but tagAssignment is not empty" if ($tagAssignment.Entity.Name) { Write-DebugLog "Appliance name "$tagAssignment.Entity.Name | Out-String } } Write-DebugLog $_.exception | Out-String } if ($tagAssignment) { Write-Verbose "Tag Present" Write-DebugLog "Tag Present" if ($tagAssignment.Entity.Name -is [array]) { Write-DebugLog "More than one VM present with tag AVS_ANF_CLOUD_ADMIN_VM_TAG" $noOfVM = $tagAssignment.Entity.Name.Count For ($i = 0; $i -lt $noOfVM; $i++) { $ApplianceVirtualMachineArray.Add($tagAssignment.Entity.Name[$i]) | Out-Null } } else { Write-DebugLog "One VM present with tag AVS_ANF_CLOUD_ADMIN_VM_TAG" $ApplianceVirtualMachineArray.Add($tagAssignment.Entity.Name) | Out-Null Write-DebugLog "First element inside ApplianceVirtualMachineArray: "$ApplianceVirtualMachineArray[0] Write-DebugLog "No of VMs are: "$ApplianceVirtualMachineArray.Count } if ($ApplianceVirtualMachineArray.Count -lt 1) { Write-DebugLog "VM with the tag $tagName does not exist" Write-Verbose "VM with the tag $tagName does not exist" } else { $noOfVM = $ApplianceVirtualMachineArray.Count For ($i = 0; $i -lt $noOfVM; $i++) { $ApplianceVirtualMachine = $ApplianceVirtualMachineArray[$i] try { $VMObject = Get-VM -Name $ApplianceVirtualMachine -ErrorAction Stop if ($VMObject) { Write-Verbose "Virtual machine exists" Write-DebugLog "Virtual machine exists" try { Write-Verbose "Tag Present" Write-DebugLog "Tag Present" Remove-VirtualMachine -ApplianceVirtualMachine $ApplianceVirtualMachine } catch { Write-Error -ErrorId "UNINSTALL_ERROR" -Message "No permission to delete the virtual machine $ApplianceVirtualMachine, contact NetApp." Throw $_.exception.Message } } else { Write-Host -Message "VM with name $ApplianceVirtualMachine does not exist" Write-DebugLog -Message "VM with name $ApplianceVirtualMachine does not exist" Write-Verbose -Message "VM with name $ApplianceVirtualMachine does not exist" Throw "VM with name $ApplianceVirtualMachine does not exist" } } catch { if ($_.CategoryInfo.Category -eq "ObjectNotFound") { Write-DebugLog -Message "VM with name $ApplianceVirtualMachine does not exist" Write-Error -ErrorId "UNINSTALL-NO-SUCH-VM-ERROR" -Message "VM with name $ApplianceVirtualMachine does not exist" Throw $_.exception.Message } else { Write-DebugLog -Message $_.exception | Out-String Throw $_.exception.Message } } } Remove-User -Domain $domain -UserName $userName Remove-Role -RoleName $SCDPRole Remove-Extensions Write-Host "Uninstallation is completed." } } else { Write-DebugLog -Message "No appliance found" Write-Verbose -Message "No appliance found" Remove-User -Domain $domain -UserName $userName Remove-Role -RoleName $SCDPRole Remove-Extensions Write-Host "Uninstallation is completed." Throw "No virtual machine appliance found" } } END { Write-Verbose -Message "UnInstall-NetAppCBSAppliance execution is completed" Write-DebugLog -Message "UnInstall-NetAppCBSAppliance execution is completed" } } function Remove-VirtualMachine { <# .SYNOPSIS This function removes the virtual machine user from the vCenter. .DESCRIPTION This function removes the uvirtual machine ser from the vCenter. .PARAMETER ApplianceVirtualMachine Specify the virtual machine name. .EXAMPLE Remove-VirtualMachine ApplianceVirtualMachine 'vm1' Description --------------------------------------- Removes the virtual machine 'vm1'. #> param( [Parameter( Mandatory = $true, HelpMessage = 'Specify the virtual machine for the virtual appliance.')] [string] $ApplianceVirtualMachine ) PROCESS { Write-Verbose -Message "Remove-VirtualMachine execution is started" Write-DebugLog -Message "Remove-VirtualMachine execution is started" Write-Verbose "Checking state of virtual machine appliance" Write-DebugLog "Checking state of virtual machine appliance" $VMObject = Get-VM -Name $ApplianceVirtualMachine -ErrorAction SilentlyContinue -ErrorVariable VMObjectError $VMPowerState = $VMObject.PowerState if ($VMPowerState -eq "PoweredOn") { Write-DebugLog -Message "Stopping virtual machine appliance" Write-Verbose "Stopping virtual machine appliance" $Task = Stop-VM -VM $ApplianceVirtualMachine -Confirm:$false #Show-Progress -TotalNoOfTimes 100 -SleepTime 1 -ActivityText 'Stopping' -ProgressText "percent complete" Write-DebugLog -Message "Deleting virtual machine appliance" Write-Verbose "Deleting virtual machine appliance" if ($Task.PowerState -eq "PoweredOff") { try { Remove-VM -VM $ApplianceVirtualMachine -DeletePermanently -Confirm:$false Write-Host "Virtual machine $ApplianceVirtualMachine is deleted." } catch { Write-DebugLog -Message $_.exception | Out-String Write-Error -ErrorId -Message "Unable to delete virtual machine" Throw $_.exception.message } } else { Write-Host "Virtual machine $ApplianceVirtualMachine could not be stopped." } } else { Write-DebugLog -Message "Deleting virtual machine appliance" Write-Verbose "Deleting virtual machine appliance" try { Remove-VM -VM $ApplianceVirtualMachine -DeletePermanently -Confirm:$false Write-Host "Virtual machine $ApplianceVirtualMachine is deleted." } catch { Write-DebugLog -Message $_.exception | Out-String Write-Error -ErrorId -Message "Unable to delete virtual machine" Throw $_.exception.message } } } END { Write-Verbose -Message "Remove-VirtualMachine execution is completed" Write-DebugLog -Message "Remove-VirtualMachine execution is completed" } } function Remove-User { <# .SYNOPSIS This function removes the user from the vCenter. .DESCRIPTION This function removes the user from the vCenter. .PARAMETER Domain Specify the domain in which user should be deleted. .PARAMETER UserName Specify the user name to be deleted. .EXAMPLE Remove-User -Domain 'domain1' -UserName "user1" Description --------------------------------------- Removes the user 'user1' in the domain 'domain1' #> param( [Parameter( Mandatory = $true, HelpMessage = 'Specify the virtual machine for the virtual appliance.')] [string] $Domain, [Parameter( Mandatory = $true, HelpMessage = 'Specify the virtual machine for the virtual appliance.')] [string] $UserName ) PROCESS { Write-Verbose -Message "Remove-User execution is started" Write-DebugLog -Message "Remove-User execution is started" $SCDPAdminObject = Get-SsoPersonUser -Domain $Domain -Name $UserName if ($null -ne $SCDPAdminObject) { Write-DebugLog -Message "Removing SSO user $UserName" Write-Verbose -Message "Removing SSO user $UserName" Remove-SsoPersonUser -User (Get-SsoPersonUser -Name $UserName -Domain vsphere.local) } else { Write-Verbose -Message "User $UserName is not present in the domain $Domain" | Out-String Write-DebugLog -Message "User $UserName is not present in the domain $Domain" | Out-String } } END { Write-Verbose -Message "Remove-User execution is completed" Write-DebugLog -Message "Remove-User execution is completed" } } function Remove-Role { <# .SYNOPSIS This function removes the role from the vCenter. .DESCRIPTION This function removes the role from the vCenter. .PARAMETER RoleName Specify the role name to be deleted. .EXAMPLE Remove-Role -RoleName "role1" Description --------------------------------------- Removes the role 'role1' #> param( [Parameter( Mandatory = $true, HelpMessage = 'Specify the virtual machine for the virtual appliance.')] [string] $RoleName ) PROCESS { Write-Verbose -Message "Remove-Role execution is started" Write-DebugLog -Message "Remove-Role execution is started" $SCDPRoleObject = Get-VIRole -Name $RoleName -ErrorAction silentlycontinue if ($null -ne $SCDPRoleObject) { Write-Verbose -Message "Role $RoleName is present " Write-DebugLog -Message "Role $RoleName is present " Remove-VIRole -Role (Get-VIRole -Name $RoleName) -Confirm:$false -Force } else { Write-Verbose -Message "Role with the name $RoleName is not present" Write-DebugLog -Message "Role with the name $RoleName is not present" } } END { Write-Verbose -Message "Remove-Role execution is completed" Write-DebugLog -Message "Remove-Role execution is completed" } } function Remove-Extensions { <# .DESCRIPTION This function removes the extension. .EXAMPLE Remove-Extensions #> BEGIN { $scvExtension1 = "com.netapp.aegis" $scvExtension2 = "com.netapp.scvm.webclient" } PROCESS { Write-Verbose -Message "Remove-Extensions execution is started" Write-DebugLog -Message "Remove-Extensions execution is started" ## unregistering from GUI $em = Get-View ExtensionManager $extensionList = $em.ExtensionList.Key if ($extensionList -contains $scvExtension1) { $em.UnregisterExtension($scvExtension1) Write-DebugLog -Message "Extension $scvExtension1 is removed" Write-Verbose -Message "Extension $scvExtension1 is removed" } else { Write-DebugLog -Message "Extension $scvExtension1 is not found" } if ($extensionList -contains $scvExtension2) { $em.UnregisterExtension($scvExtension2) Write-DebugLog -Message "Extensions $scvExtension2 is removed" Write-Verbose -Message "Extensions $scvExtension2 is removed" } else { Write-DebugLog -Message "Extension $scvExtension2 is not found" Write-Verbose -Message "Extensions $scvExtension2 is removed" } $em.UpdateViewData() } END { Write-Verbose -Message "Remove-Extensions execution is completed" Write-DebugLog -Message "Remove-Extensions execution is completed" } } |