2015-04-02 109 views
0

我想在新机器上安装RabbitMQ之后创建一个设置用户“George”的powershell脚本。使用Powershell通过API在RabbitMQ中创建用户

我不明白为什么这个脚本不起作用。

的最后一步给了我一个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 

我也试过格式化最后一步是这样的:

http://localhost:15672/api/permissions/george 

相同的404错误。

我尝试了大约20,000种不同的发送命令的方式。从完全匹配其他例子到尝试一些抽象艺术和巫术魔法。在观看RabbitMQ的管理工具时,我可以看到George已经创建。他有一个空虚的鬼魂。所以前3个步骤完美地工作。

回答

5

好的男人,你知道我爱你,因为我今晚之前从来没有听说过RabbitMQ。在最后一小时,我已经将它安装在我的Windows机器上,现在已经使用this awesome guide here to the API并了解它的工作原理。

所以,当我跑一步你相同的工序,我看到的一切发生,因为你的状态:

乔治被创建: enter image description here

因为你的第二个步骤是列出用户的当前权限运行API调用,我接下来会看到具有完整权限的guest帐户的输出。然后进入步骤3,为乔治建立目标权限。

username : george 
write  : .* 
read  : .* 
configure : .* 
vhost  :/

从这里,第4步。当我在上一步后手动运行此步骤...它的工作原理!但是,如果我运行得太快,如果我一次运行整个脚本,则会出现404错误。看起来,在RabbitMQ的幕后,需要稍微暂停才能让新用户更新文件。当我删除用户并且再次尝试整个脚本时,我几乎每步都得到了404。

但是,如果我加一点Start-Sleep 5暂停5秒...

enter image description here

整个过程完成。添加暂停的关键在于步骤1之后,似乎需要四到五秒钟。

使其成为非常

现在当然,我不能停在那里,所以我决定添加一些更小的停顿,以提高输出的可读性,同时确保每个操作完成。在步骤完成后,我添加了一些purty,查看“OK”消息,然后通过为当前用户进行最后一次API调用来添加权限确认。

下面是完成输出

enter image description here

完成的脚本

$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 ...

+0

我想我刚刚发现了与RabbitMQ的Windows 2012 x64的另一个bug。 非常感谢您验证我的脚本确实可行。 当我在Windows 2012服务器上尝试脚本时,它以同样的404方式出错。 我毫不怀疑这是另一个RabbitMQ + Windows 2012问题,而不是别的。 – Lup 2015-04-02 18:10:33

+0

也许是这样......但有一种观点拖延,你可以使这项工作! – FoxDeploy 2015-04-02 18:11:16

+0

而RabbitMQ是一个了不起的工具。真的爱死它。 即使我发现的技术是hte答案,我仍然保持你的'答案'。 Cuase我现在正在使用你的代码......哈哈 – Lup 2015-04-02 18:36:27

相关问题