Invoke REST API method to update the local admin password

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?


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.

链接地址: http://www.djcxy.com/p/91460.html

上一篇: 假设一个GUID永远是唯一的是否安全?

下一篇: 调用REST API方法来更新本地管理员密码