Lup*_*Lup 0 api powershell rabbitmq
我正在尝试创建一个powershell脚本,在新机器上安装RabbitMQ后设置用户"George".
我无法弄清楚为什么这个脚本不起作用.
最后一步给了我404 {"错误":"找不到对象","原因":"\"未找到\"\n"}
$secpasswd = ConvertTo-SecureString 'guest' -AsPlainText -Force
$credGuest = New-Object System.Management.Automation.PSCredential ('guest', $secpasswd)
$secpasswd2 = ConvertTo-SecureString 'george' -AsPlainText -Force
$credAdmin2 = New-Object System.Management.Automation.PSCredential ('george', $secpasswd2)
$body = @{
'password' = 'george'
'tags' = 'administrator'
} | ConvertTo-Json
$vhosts1 = ''
$vhosts1 = Invoke-RestMethod 'http://localhost:15672/api/users/george' -credential $credGuest -Method Put -ContentType "application/json" -Body $body
write '1:' $vhosts1
$vhosts2 = Invoke-RestMethod 'http://localhost:15672/api/permissions/%2f/' -Method get -credential $credAdmin2
write '2:' $vhosts2
$body2 = @{
'username' = 'george'
'vhost' = '/'
'configure' = '.*'
'write' = '.*'
'read' = '.*'
} | ConvertTo-Json
write '3:' $body2
$vhosts3 = Invoke-RestMethod 'http://localhost:15672/api/permissions/%2f/george' -credential $credGuest -Method Put -ContentType "application/json" -Body $body2
write '4:' $vhosts3
Run Code Online (Sandbox Code Playgroud)
我也试过像这样格式化最后一步:
http://localhost:15672/api/permissions/george
Run Code Online (Sandbox Code Playgroud)
相同的404错误.
我已经尝试了大约20,000种不同的方式发送命令.从完美匹配其他例子到尝试一些抽象艺术和伏都教魔法.在观看RabbitMQ的管理工具时,我可以看到George已经创建.而且他有一个空虚的幽灵.所以前3个步骤完美无缺.
好吧,伙计,你知道我爱你,因为我在今晚之前从未听说过RabbitMQ.在过去的一个小时里,我已经将它安装在我的Windows机器上,现在已经在这里使用了这个很棒的指南API,并了解它是如何工作的.
所以,当我一步一步地运行相同的过程时,我看到一切都在你说的时候发生:
乔治被创造出来:

由于您的第二步是列出运行API调用的用户的当前权限,因此我接下来会看到来宾帐户的输出,该帐户具有完整的权限.然后转到步骤3,它为George构建目标权限.
username : george
write : .*
read : .*
configure : .*
vhost : /
Run Code Online (Sandbox Code Playgroud)
从这里开始,步骤4.当我在上一步之后手动运行此步骤...它可以工作!但是,如果我运行得太快,如果我立即运行整个脚本,我将收到404错误.似乎在RabbitMQ的幕后,需要稍微暂停以便用新用户更新文件.当我删除用户并再次尝试整个脚本时,我的每一步都得到了404,非常多.
但是,如果我添加一点Start-Sleep 5暂停5秒......

整个过程完成了.添加暂停的关键位置是在步骤1之后,似乎需要大约四到五秒钟.
让它漂亮
当然,我无法阻止,所以我决定添加一些小的暂停以提高输出可读性并确保每个操作完成.我在步骤完成后添加了一些看待"OK"消息的purty,然后通过为当前用户执行最后一次API调用来添加权限的完成确认.
这是完成的输出

完成的脚本
$secpasswd = ConvertTo-SecureString 'guest' -AsPlainText -Force
$credGuest = New-Object System.Management.Automation.PSCredential ('guest', $secpasswd)
$secpasswd2 = ConvertTo-SecureString 'stephen' -AsPlainText -Force
$credAdmin2 = New-Object System.Management.Automation.PSCredential ('stephen', $secpasswd2)
$body = @{
'password' = 'stephen'
'tags' = 'administrator'
} | ConvertTo-Json
Write-host "About to create new user $(($body | ConvertFrom-Json).Password)..." -NoNewline
$vhosts1 = Invoke-RestMethod 'http://localhost:15672/api/users/stephen' -credential $credGuest -Method Put -ContentType "application/json" -Body $body
start-sleep 5
Write-host "OK" -ForegroundColor Green
Start-Sleep -Milliseconds 400
Write-Host '1: Results:' $vhosts1
$body2 = @{
'username' = 'stephen'
'vhost' = '/'
'configure' = '.*'
'write' = '.*'
'read' = '.*'
} | ConvertTo-Json
Write-Output "Desired perms for new user $(($body | ConvertFrom-Json).Password)" $body2
Write-host "Setting perms for new user..." -NoNewline
$vhosts3 = Invoke-RestMethod 'http://localhost:15672/api/permissions/%2f/stephen' -credential $credGuest -Method Put -ContentType "application/json" -Body $body2
Start-sleep 5
Write-host "OK" -ForegroundColor Green
Start-Sleep -Milliseconds 400
write '4:' $vhosts3
'Retrieiving perms for new user to confirm...'
Invoke-RestMethod 'http://localhost:15672/api/permissions/%2f/stephen' -Method get -credential $credAdmin2
Run Code Online (Sandbox Code Playgroud)
现在我希望我再次有机会再次使用RabbitMQ ......
| 归档时间: |
|
| 查看次数: |
2734 次 |
| 最近记录: |