ChangePassword.ps1

<#
.SYNOPSIS
This function modifies the password of the virtual appliance.
 
.DESCRIPTION
This function modifies the password of the virtual appliance.
 
.EXAMPLE
set-netapp-cbs-appliance-password
 
Description
---------------------------------------
Modify the password of the virtual appliance.
 
#>

function set-netapp-cbs-appliance-password {

  <#
   High level task of this cmdlets
   1. Check if tag AVS_ANF_CLOUD_ADMIN_VM_TAG exists.
   2. If the tag does not exist, throw error.
   3. If tag exist, get teh VM detail.
   4. Check if VM assosicated with Tag present.
   5. If VM not present, throw eror
   6. IF VM present
      A. Change password for teh user 'NetAppSCDPAdmin'
      B. Update vApp options for the users new password.
      C. Poweroff/on
      D. Restart VMGuest
  #>


  BEGIN {
    Write-DebugLog -Message "In begin block of Set-NetAppCBSAppliancePassword"
    $userName = "NetAppSCDPAdmin"
  }

  PROCESS {
    Write-DebugLog -Message "Set-NetAppCBSAppliancePassword execution is started"
  
    Invoke-PreflightNetAppCBSAVSCheck

    $ApplianceVirtualMachine = Get-ApplianceVirtualMachineUsingTag
    
    $VMObject = Test-ApplianceExistOrNot -ApplianceVirtualMachine  $ApplianceVirtualMachine
    
    if ($VMObject) {    
  
      $mobObject = (Get-View ExtensionManager | Select-Object -ExpandProperty ExtensionList | Select-Object Key, @{N = 'Server'; E = { $_.Server.url } } |  Where-Object { $_.Key -eq "com.netapp.aegis" })
      Write-DebugLog $mobObject | out-string
  
      if ($mobObject) {
        Write-DebugLog "Cloud Backup Service is registered"
        $serverUrl = $mobObject.Server
      }
      else {
        Write-DebugLog "Cloud Backup Service is not registered"
        Write-Error "Cloud Backup Service is not registered"  -ErrorAction Stop
      }
      
      $serverUrlWithouthHttps = $serverUrl.replace("https://", "")
      Write-DebugLog $serverUrlWithouthHttps
      $ApplianceIPAddress = $serverUrlWithouthHttps.substring(0, $serverUrlWithouthHttps.indexof(":"))
      Write-DebugLog "ApplianceIPAddress"

      try {
       
        Write-DebugLog "Fetching tag for virtual machine..."      

        $newUserPassword = Get-Password
        $vcSecurePass = ($newUserPassword | ConvertTo-SecureString -AsPlainText -Force)
        $vcUserCredential = New-Object System.Management.Automation.PSCredential ($userName, $vcSecurePass)
        
        Write-DebugLog -Message "Changing user's password..."
        # Modify NetAppSCDPAdmin users password
        Set-Password -VcUserCredential $vcUserCredential
        
        $ovfPasswdChanges = @{
          "vcenter.register.passwd" = $newUserPassword
        }
        
        # Modify VM's Ovf Property for NetAppSCDPAdmin user's password change.
        Set-VMOvfProperty -ApplianceVirtualMachine $ApplianceVirtualMachine -ovfChanges $ovfPasswdChanges
        #Changing OVF properties, may take some time to reflect so adding a sleep of 15 second.
        Start-Sleep 15
        
        PowerOffOnVM -ApplianceVirtualMachine $ApplianceVirtualMachine
        #Post poweroff/on , even if status is running, still some operations continues for some time. So as there is no right way to find out if operations is completly done or not,
        #a sleep is required. Here 2 minute sleep is added .
        Start-Sleep 120
        
        Write-DebugLog "Restart-VMwareVM execution started." 
        Restart-VMwareVM -ApplianceVirtualMachine $ApplianceVirtualMachine
        Write-DebugLog -Message "Waiting 10 minutes for service to be started."
        #Post Restart-VMwareVMGuest , even if status is running, still some operations continues for some time. So as there is no right way to find out if operations is completly
        # done or not a sleep is required. Here 10 minute sleep is added .
        Start-Sleep 600 
        Write-DebugLog "Restart-VMwareVMGuest execution completed." 
    
        Test-Configuration -ApplianceIPAddress "" -ApplianceVirtualMachine $ApplianceVirtualMachine
        
        Write-DebugLog "Password is changed." 
        Clear-PasswordInOVFProperties -ApplianceVirtualMachine $ApplianceVirtualMachine        
      }
      catch {
        Write-Error -ErrorId "CHANGE_PASSWD_ERROR" -Message "Operation failed"
        write-error $_.exception.Message -ErrorAction Stop
      }
    }
    else {
      Write-DebugLog -Message "VM with name $ApplianceVirtualMachine does not exist"
      write-error "VM with name $ApplianceVirtualMachine does not exist" -ErrorAction Stop
    }
  }

  END {
    Write-DebugLog -Message "Set-NetAppCBSAppliancePassword execution is completed"
  }  
}