我正在尝试使用LastPass Enterprise来使用Powershell自动在我们的系统中创建新用户。我似乎无法在代码中正确地调用API。我几乎可以肯定,这与“数据”对象有关。这是我正在通过身体的批处理对象。
$lastPassObject = @{
cid = "G1TUROWN";
provhash = "N0TM!NE";
cmd = "batchadd";
data = @(
{
username = $email;
fullname = $firstName + " " + $lastName;
password = "Toys4Trucks22!";
password_reset_required = "true";
}
)
}
这是我的API调用
Invoke-RestMethod -Uri "https://lastpass.com/enterpriseapi.php" -Method Post -Body $lastPassObject -ContentType "application/json"
然后是我收到的错误
引用API:https://support.lastpass.com/help/add-new-users-via-lastpass-api
发布于 2022-06-24 13:53:55
你需要在发送之前把你的身体转换成json。另外,还可以将Data
部分放在数组中的scriptblock中。这需要是一个哈希表,而不是scriptblock。
$lastPassObject = @{
cid = "G1TUROWN";
provhash = "N0TM!NE";
cmd = "batchadd";
data = @(
# This need to be an hashtable. You were missing the @
@{
username = $email;
fullname = $firstName + " " + $lastName;
password = "Toys4Trucks22!";
password_reset_required = $true;
}
)
}
$Body = $lastPassObject | ConvertTo-Json
Invoke-RestMethod -Uri "https://lastpass.com/enterpriseapi.php" -Method Post -Body $Body -ContentType "application/json"
如果在此之后仍然存在问题,请确保检查json的实际外观(在ConvertTo-Json
之后),以便准确地知道您要发送什么,并且能够更容易地发现差异。例如,当我第一次这样做的时候,我立即发现数据部分都是错误的,格式化是明智的,并因此发现了缺少的@
。
此外,通过查看转换后的json和它们的文档中的示例,您仍然可以看到password_reset_required
是一个布尔值。我将您的"true"
更改为$true
,以便相关的json能够达到预期的效果。
https://stackoverflow.com/questions/72744980
复制相似问题