This topic contains 1 reply, has 2 voices, and was last updated by  Jason D July 5, 2016.

Using PSCredentials with the REST API?

  • I’ve written a script to pull VPGs and Protected VMs from the REST API using Powershell and the next step would be to write the scripts so that I can schedule them to run through Task Scheduler.

    At the moment I’ve got me credentials entered into the Test script but obviously would like to use PSCredential to securely store them before going live with any scripts. Has anybody got this working? How are people passing credentials from Powershell (if you are using Powershell to call the REST API)?

    Thanks,

    Lee

    I also wanted to do this, and try to keep the password out of clear text as much as possible.  I was able to do a one liner for the password, so the password isn’t stored in clear text in a variable.  That doesn’t mean you can’t find the password in the code, but you can use get-credentials instead of storing it in a clear text string.

     

    Original $authInfo variable with password stored as the $ZertoPassword variable and converted to Base64

    $authInfo = (“{0}:{1}” -f $ZertoUser,$ZertoPassword)
    $authInfo = [System.Text.Encoding]::UTF8.GetBytes($authInfo)
    $authInfo = [System.Convert]::ToBase64String($authInfo)

    Updated $authInfo populated by Get-Credentials instead of the $ZertoPassword string

    $creds = Get-Credential
    $ZertoUser = $creds.UserName.ToString()
    #Converts the get-credential username and password to the Base64 string required by the Zerto REST API

    #####Note: This is one line
    $authInfo = [System.Text.Encoding]::UTF8.GetBytes((“{0}:{1}” -f $ZertoUser,([Runtime.InteropServices.Marshal]::PtrToStringBSTR([Runtime.InteropServices.Marshal]::SecureStringToBSTR($creds.Password)))))

    #####
    $authInfo = [System.Convert]::ToBase64String($authInfo)

You must be logged in to create new topics. Click here to login