使用 Powershell 通过 API 在 RabbitMQ 中创建用户
Using Powershell to create user in RabbitMQ via API
我正在尝试创建一个 powershell 脚本来设置用户 "George" 在新机器上安装 RabbitMQ 后。
我不明白为什么这个脚本不起作用。
最后一步给了我一个 404 {"error":"Object Not Found","reason":"\"Not Found\"\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
我也试过像这样格式化最后一步:
http://localhost:15672/api/permissions/george
同样的 404 错误。
我已经尝试了大约 20,000 种不同的方式来发送命令。从完美匹配其他示例到尝试一些抽象艺术和巫毒魔法。在查看 RabbitMQ 的管理工具时,我可以看到 George 已创建。他有一个空的虚拟主机。所以前 3 个步骤非常有效。
好的,伙计,你知道我爱你,因为今晚之前我从未听说过 RabbitMQ。在过去的一个小时里,我将它安装在我的 Windows 机器上,现在已经使用 this awesome guide here to the API 并且了解了它的工作原理。
因此,当我 运行 一步步执行与您相同的过程时,我看到一切都如您所说:
George 被创建:
由于您的第二步是列出 运行 调用 API 用户的当前权限,我接下来会看到具有完整权限的来宾帐户的输出。然后进入第 3 步,为 George 构建目标权限。
username : george
write : .*
read : .*
configure : .*
vhost : /
从这里开始,第 4 步。当我 运行 在上一步之后手动执行此步骤时...有效!但是,如果我 运行 这太快了,如果我一次 运行 整个脚本,我将得到 404 错误。似乎在 RabbitMQ 的幕后,需要稍微暂停一下才能使用新用户更新文件。当我删除用户并过快地再次尝试整个脚本时,我几乎每一步都得到 404。
但是,如果我添加一点 Start-Sleep 5
来暂停 5 秒...
整个过程完成。加停顿的关键地方在第1步之后,好像需要四五秒左右。
让它漂亮
当然,我不能就此停下来,所以我决定再添加一些小的停顿以提高输出的可读性并确保每个操作都完成。我在该步骤完成后添加了一些看起来很漂亮的 "OK" 消息,然后通过对当前用户进行最后一次 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
现在我只希望有机会再次使用 RabbitMQ...
终于偶然发现了其他人为与 RabbitMQ 交互而编写的 powershell 库 api。
我发现某些版本的 powershell 在将它发送给 rabbit 之前用 / 替换了 %2f。如果你使用这里找到的这些函数:
https://github.com/mariuszwojcik/RabbitMQTools
一切都运行得很漂亮。他把图书馆放在一起真的很棒。
我迟到了这个问题。我也喜欢 Powershell,但我使用 .bat 文件来 运行 原生 RabbitMQ 命令,如下所示。我需要添加一个用户、一个虚拟主机,并将这两者连接起来。我想要一个脚本,这样任何新开发人员都可以 运行 它。
cd /D c:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.14\sbin
echo cd completed
call rabbitmqctl add_user bts mypass
call rabbitmqctl set_user_tags bts administrator
call rabbitmqctl add_vhost base
call rabbitmqctl set_permissions -p base bts ".*" ".*" ".*"
Echo ----List Command to confirm results --------
call rabbitmqctl list_vhosts
call rabbitmqctl list_user_permissions bts
我想您也可以从 Powershell 中调用它们。我想这样做的缺点是您必须在 运行ning RabbitMQ 机器上。
我正在尝试创建一个 powershell 脚本来设置用户 "George" 在新机器上安装 RabbitMQ 后。
我不明白为什么这个脚本不起作用。
最后一步给了我一个 404 {"error":"Object Not Found","reason":"\"Not Found\"\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
我也试过像这样格式化最后一步:
http://localhost:15672/api/permissions/george
同样的 404 错误。
我已经尝试了大约 20,000 种不同的方式来发送命令。从完美匹配其他示例到尝试一些抽象艺术和巫毒魔法。在查看 RabbitMQ 的管理工具时,我可以看到 George 已创建。他有一个空的虚拟主机。所以前 3 个步骤非常有效。
好的,伙计,你知道我爱你,因为今晚之前我从未听说过 RabbitMQ。在过去的一个小时里,我将它安装在我的 Windows 机器上,现在已经使用 this awesome guide here to the API 并且了解了它的工作原理。
因此,当我 运行 一步步执行与您相同的过程时,我看到一切都如您所说:
George 被创建:
由于您的第二步是列出 运行 调用 API 用户的当前权限,我接下来会看到具有完整权限的来宾帐户的输出。然后进入第 3 步,为 George 构建目标权限。
username : george
write : .*
read : .*
configure : .*
vhost : /
从这里开始,第 4 步。当我 运行 在上一步之后手动执行此步骤时...有效!但是,如果我 运行 这太快了,如果我一次 运行 整个脚本,我将得到 404 错误。似乎在 RabbitMQ 的幕后,需要稍微暂停一下才能使用新用户更新文件。当我删除用户并过快地再次尝试整个脚本时,我几乎每一步都得到 404。
但是,如果我添加一点 Start-Sleep 5
来暂停 5 秒...
整个过程完成。加停顿的关键地方在第1步之后,好像需要四五秒左右。
让它漂亮
当然,我不能就此停下来,所以我决定再添加一些小的停顿以提高输出的可读性并确保每个操作都完成。我在该步骤完成后添加了一些看起来很漂亮的 "OK" 消息,然后通过对当前用户进行最后一次 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
现在我只希望有机会再次使用 RabbitMQ...
终于偶然发现了其他人为与 RabbitMQ 交互而编写的 powershell 库 api。
我发现某些版本的 powershell 在将它发送给 rabbit 之前用 / 替换了 %2f。如果你使用这里找到的这些函数:
https://github.com/mariuszwojcik/RabbitMQTools
一切都运行得很漂亮。他把图书馆放在一起真的很棒。
我迟到了这个问题。我也喜欢 Powershell,但我使用 .bat 文件来 运行 原生 RabbitMQ 命令,如下所示。我需要添加一个用户、一个虚拟主机,并将这两者连接起来。我想要一个脚本,这样任何新开发人员都可以 运行 它。
cd /D c:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.14\sbin
echo cd completed
call rabbitmqctl add_user bts mypass
call rabbitmqctl set_user_tags bts administrator
call rabbitmqctl add_vhost base
call rabbitmqctl set_permissions -p base bts ".*" ".*" ".*"
Echo ----List Command to confirm results --------
call rabbitmqctl list_vhosts
call rabbitmqctl list_user_permissions bts
我想您也可以从 Powershell 中调用它们。我想这样做的缺点是您必须在 运行ning RabbitMQ 机器上。