Reply to Message

View discussion in a popup

Replying to:
po-temkin
Contributor
Contributor

VMware View API - Change recovery password

Hello to everyone
I want to edit Recovery Password for my Horizon Connection Server. Below, you can see a couple of functions that should do this. But instead of a successful result, I see an error:

PS C:\Windows\system32> $Password = '12345678'
PS C:\Windows\system32> $Password = ConvertTo-SecureString -String $Password -AsPlainText -Force
PS C:\Windows\system32> Set-HvRecoveryPasswordExt -Password $Password
Exception calling "GlobalSettings_Update" with "1" argument(s): "ExceptionType : VMware.Hv.InvalidArgument
ErrorMessage : Invalid recovery password hash.
ParameterName : dataRecoveryPasswordData"
At C:\Program Files\WindowsPowerShell\Modules\VMware.Hv.Helper.Ext\VMware.Hv.Helper.Ext.psm1:1627 char:5
+     $services.GlobalSettings.GlobalSettings_Update($updates)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : VimException


RecoveryPasswordSHA256 RecoveryPasswordHint
---------------------- --------------------
{123, 35, 45, 65...}

 

What's wrong with my approach? P.S: Documentation page https://developer.vmware.com/apis/1696/view

function Set-HvRecoveryPasswordExt {
  param(
    [Parameter(Mandatory = $true)]
    [securestring]
    $Password,
    [Parameter(Mandatory = $false)]
    [string]
    $Hint
  )
  begin {
    $services = Get-ViewAPIService -hvServer $hvServer
    if ($null -eq $services) {
      Write-Error "Could not retrieve ViewApi services from connection object"
      break
    }
  }
  process {
    $tempPassword = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password)
    $plainePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($tempPassword)
    $Stream = [IO.MemoryStream]::new([byte[]][char[]]$plainePassword)
    $SHA256 = (Get-FileHash -InputStream $Stream -Algorithm SHA256).Hash
    $SHA256Bytes = [System.Text.Encoding]::UTF8.GetBytes($SHA256)
    $GlobalSettingsDataRecoveryPasswordData = New-Object VMware.Hv.GlobalSettingsDataRecoveryPasswordData -Property @{
      recoveryPasswordSHA256 = $SHA256Bytes
      recoveryPasswordHint = $Hint
    }
    $updates = Get-MapEntryExt -Key dataRecoveryPasswordData -Value $GlobalSettingsDataRecoveryPasswordData
    $services.GlobalSettings.GlobalSettings_Update($updates)
  }
  end {
    return $updates
    [System.gc]::collect()
  }
}

 

Reply
0 Kudos