-1

I have to write a migration script for Active Directory using PowerShell. This script will update the password of the local admin of server stored in Pleasant Password Server for KeePass.

First I log into the KeePass using my Windows login credentials and then search the server for which the password needs to be updated.

So, skipping the script for above said functionalities I will start from generating the password:

function Generate-Password {
    $alphabets = "abcdefghijklmnopqstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#%^*"

    $char = for ($i = 0; $i -lt $alphabets.Length; $i++) { $alphabets[$i] }

    for ($i = 1; $i -le 16; $i++) {
        Write-Host -NoNewline $(Get-Random $char)
        if ($i -eq 16) { Write-Host `n }
    }
}
$pass = Generate-Password 

After a password is generated from the above script I want to update the password for the server which I searched.

For example: Find the attachment for Server Details

I want to change the password for the above searched server by the function Generate-Password.

For this, I used the REST API method of PPS:

function UpdatePassword {
    $update = Invoke-RestMethod -Uri “$KeepassURL/api/v4/rest/credential/$CredentialID/password/$pass” -Headers $headers -Method Put -ContentType ‘application/json’
}

I think I am making some mistakes with the syntax here. How do I pass the generated password ie. $pass to the invoked REST method?

Pranjal Kaushik
  • 13
  • 1
  • 10

1 Answers1

0

The documentation you referenced says:

PUT credential/:id        ** Update a credential **
Method                    PUT
Requires Authentication?  Yes
Parameters id GUID for credential
Input type Credential Result type None

and the documentation of the Credential input type shows this example JSON document:

Example Credential (JSON)
{ Id: "2b45438a-2f4a-4d96-9ba9-058ea54252fb" Name: "Credential 0" Username: "Credential 0" Password: null Url: "" Notes: "" GroupId: "cfb2c08e-e990-43b7-99d1-c8e23e0ae00e" Created: "2013-11-18T10:14:27.8218898-07:00" Modified: "2015-06-01T13:26:12.336084-06:00" Expires: null ... }

So you probably need to do something like this:

$CredentialID = '2b45438a-2f4a-4d96-9ba9-058ea54252fb'
$uri  = "$KeepassURL/api/v4/rest/credential/$CredentialID"
$type = 'application/json'
$body = @{
    'Id'       = $CredentialID
    'Name'     = 'Credential 0'
    'Username' = 'Credential 0'
    'Password' = $pass
    ...
}

Invoke-RestMethod -Uri $uri -Method Put -Headers $headers -Body $body -ContentType $type

assuming that you're passing an already acquired authorization token via $headers.

Untested, though, and I'm not quite sure if you need all key/value pairs in the body or just the relevant ones, so you'll need to play around with it a little.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Thank you for your answer. The variable '$CredentialID' needs GUID to proceed the script further. But the client ( KeePass ) shows only the 'UUID' of the machine and not the GUID. Would I need another script to access the GUID first for the server? – Pranjal Kaushik Dec 20 '17 at 09:45
  • https://stackoverflow.com/questions/246930/is-there-any-difference-between-a-guid-and-a-uuid – Ansgar Wiechers Dec 20 '17 at 10:14
  • @PranjalKaushik Any particular reason why you unaccepted this answer? – Ansgar Wiechers Jan 03 '18 at 09:23