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-Installation -ApplianceIPAddress "" -ApplianceVirtualMachine $ApplianceVirtualMachine Write-DebugLog "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 = "vcenter.register.username" $VMNetmask = "255.255.255.0" $VMGateway = "192.168.1.1" $ovfChanges = @{ "vcenter.register.username"=$VCRegisterUserName "vcenter.register.passwd"=$VCRegisterUserPasswd } 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-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 } |