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.
  #>


  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) {    
      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-Host "Changing user's password..."
        # Modify NetAppSCDPAdmin users password
        Set-Password -VcUserCredential $vcUserCredential
        
        $ovfPasswdChanges = @{
          "vplatform.vcenter.register.passwd.Vendor_Provider_Appliance" = $newUserPassword
        }
        
        # Modify VM's Ovf Property for NetAppSCDPAdmin user's password change.
        Set-VMOvfProperty -ApplianceVirtualMachine $ApplianceVirtualMachine -ovfChanges $ovfPasswdChanges
        Write-Output "Password is changed and OVF property for the appliance is updated" 
      }
      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"
  }  
}

Function Set-VMOvfProperty {
  <#
       
        .DESCRIPTION
            This function updates the OVF properties (vAppConfig property) for a virtual machine.
        .PARAMETER ApplianceVirtualMachine
            Name of the virtual machine appliance.
        .PARAMETER ovfChanges
            ishtable mapping OVF property ID to Value
        .EXAMPLE
            $VCRegisterUserPasswd = "password"
            $VCRegisterUserName = "vplatform.vcenter.register.username.Vendor_Provider_Appliance"
            $VMNetmask = "255.255.255.0"
            $VMGateway = "192.168.1.1"
             
            $ovfChanges = @{
                "vplatform.vcenter.register.username.Vendor_Provider_Appliance"=$VCRegisterUserName
                "vplatform.vcenter.register.passwd.Vendor_Provider_Appliance"=$VCRegisterUserPasswd
                "vplatform.gateway.Vendor_Provider_Appliance"=$VMGateway
                "vplatform.netMask.Vendor_Provider_Appliance"=$VMNetmask
            }
            Set-VMOvfProperty -ApplianceVirtualMachine "CloudAdminVM1" -ovfChanges $ovfChanges
    #>

  param(
    [Parameter(Mandatory = $true)]
    $ApplianceVirtualMachine,
    [Parameter(Mandatory = $true)]
    $ovfChanges
  )
    
  Write-DebugLog -Message "Set-VMOvfProperty execution is started"
  
  $VMObject = Get-VM -Name $ApplianceVirtualMachine 

  # Retrieve existing OVF properties from VM
  $vappProperties = $VMObject.ExtensionData.Config.VAppConfig.Property
    
  # Create a new Update spec based on the # of OVF properties to update
  $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
  $spec.vAppConfig = New-Object VMware.Vim.VmConfigSpec
  $propertySpec = New-Object VMware.Vim.VAppPropertySpec[]($ovfChanges.count)
    
  
  # Find OVF property Id and update the Update Spec
  foreach ($vappProperty in $vappProperties) {
    if ($ovfChanges.ContainsKey($vappProperty.Id)) {
      $tmp = New-Object VMware.Vim.VAppPropertySpec
      $tmp.Operation = "edit"
      $tmp.Info = New-Object VMware.Vim.VAppPropertyInfo
      $tmp.Info.Key = $vappProperty.Key
      $tmp.Info.value = $ovfChanges[$vappProperty.Id]
      $propertySpec += ($tmp)
    }
  }

  $spec.VAppConfig.Property = $propertySpec
  
  Write-Host -Message "Updating OVF Properties ..."
  Write-DebugLog -Message "Updating OVF Properties ..."

  Get-Task -Id ("Task-$(($VMObject.ExtensionData.ReconfigVM_Task($spec)).value)") | Wait-Task | Out-Null

  Write-DebugLog -Message "Set-VMOvfProperty execution is completed"
}
    
Function Get-VMOvfProperty {
  <#
        .DESCRIPTION
            Retrieves the OVF Properties (vAppConfig Property) for a VM
        .PARAMETER ApplianceVirtualMachine
            Name of the virtual machine appliance.
        .EXAMPLE
            #Get-VMOvfProperty -ApplianceVirtualMachine "CloudAdminVM1"
    #>

  param(
    [Parameter(Mandatory = $true)]$ApplianceVirtualMachine
  )

  $VMObject = Get-VM -Name $ApplianceVirtualMachine

  $vappProperties = $VMObject.ExtensionData.Config.VAppConfig.Property
    
  $results = @()
  foreach ($vappProperty in $vappProperties | Sort-Object -Property Id) {
    $tmp = [pscustomobject] @{
      Id          = $vappProperty.Id;
      Label       = $vappProperty.Label;
      Value       = $vappProperty.Value;
      Description = $vappProperty.Description;
    }
    $results += $tmp
  }
  $results
}