我正在研究这个tutorial之后的PowerShell / RabbitMQ项目,我在尝试与RabbitMQ的监听器连接时发现了一个问题。
Invoke-RestMethod : {"error":"bad_request","reason":"[{key_missing,ackmode}]"}
我设法解决了在GetMessage.ps1文件中键入以下代码行:
"ackmode" = @("ack_requeue_true", "ack_requeue_false")[[bool]$Remove]
现在它正常工作,每次执行查询/程序时,我都会在RabbitMQ上收到一条消息,告诉我发生了什么。
主要问题是PowerShell要求我提供sql结果中每一行的凭据,如果我还没有执行任何操作,它仍然会每隔3-5秒请求一次凭据,即使我已经使用它保存了它们:
$Credential = Get-Credential
编辑:GetMessage.ps1的代码
function Get-RabbitMQMessage
{
[CmdletBinding(DefaultParameterSetName='defaultLogin', SupportsShouldProcess=$true, ConfirmImpact='None')]
Param
(
# Name of RabbitMQ Queue.
[parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)]
[Alias("queue", "QueueName")]
[string]$Name = "",
# Name of the virtual host to filter channels by.
[parameter(ValueFromPipelineByPropertyName=$true)]
[Alias("vh", "vhost")]
[string]$VirtualHost,
# Name of the computer hosting RabbitMQ server. Defalut value is localhost.
[parameter(ValueFromPipelineByPropertyName=$true)]
[Alias("HostName", "hn", "cn")]
[string]$ComputerName = $defaultComputerName,
# Number of messages to get. Default value is 1.
[parameter(ValueFromPipelineByPropertyName=$true)]
[int]$Count = 1,
# Indicates whether messages should be removed from the queue. Default setting is to not remove messages.
[parameter(ValueFromPipelineByPropertyName=$true)]
[switch]$Remove,
# Determines message body encoding.
[parameter(ValueFromPipelineByPropertyName=$true)]
[ValidateSet("auto", "base64")]
[string]$Encoding = "auto",
# Indicates whether messages body should be truncated to given size (in bytes).
[parameter(ValueFromPipelineByPropertyName=$true)]
[int]$Truncate,
# Indicates what view should be used to present the data.
[ValidateSet("Default", "Payload", "Details")]
[string]$View = "Default",
# UserName to use when logging to RabbitMq server.
[Parameter(Mandatory=$true, ParameterSetName='login')]
[string]$UserName,
# Password to use when logging to RabbitMq server.
[Parameter(Mandatory=$true, ParameterSetName='login')]
[string]$Password,
# Credentials to use when logging to RabbitMQ server.
[Parameter(Mandatory=$true, ParameterSetName='cred')]
[PSCredential]$Credential
)Begin
{
$Credential = NormaliseCredentials
$cnt = 0
}
Process
{
if (-not $VirtualHost)
{
# figure out the Virtual Host value
$p = @{}
$p.Add("Credentials", $Credential)
if ($ComputerName) { $p.Add("ComputerName", $ComputerName) }
$queues = Get-RabbitMQQueue @p | ? Name -eq $Name
if (-not $queues) { return; }
if (-not $queues.GetType().IsArray)
{
$VirtualHost = $queues.vhost
} else {
$vhosts = $queues | select vhost
$s = $vhosts -join ','
Write-Error "Queue $Name exists in multiple Virtual Hosts: $($queues.vhost -join ', '). Please specify Virtual Host to use."
}
}
[string]$s = ""
if ([bool]$Remove) { $s = "Messages will be removed from the queue." } else {$s = "Messages will be requeued."}
if ($pscmdlet.ShouldProcess("server: $ComputerName/$VirtualHost", "Get $Count message(s) from queue $Name. $s"))
{
$url = "http://$([System.Web.HttpUtility]::UrlEncode($ComputerName)):15672/api/queues/$([System.Web.HttpUtility]::UrlEncode($VirtualHost))/$([System.Web.HttpUtility]::UrlEncode($Name))/get"
$body = @{
"count" = $Count
"requeue" = -not [bool]$Remove
"encoding" = $Encoding
"ackmode" = @("ack_requeue_true", "ack_requeue_false")[[bool]$Remove]
}
if ($Truncate) { $body.Add("truncate", $Truncate) }
$bodyJson = $body | ConvertTo-Json
Write-Debug "body: $bodyJson"
$result = Invoke-RestMethod $url -Credential $Credential -AllowEscapedDotsAndSlashes -DisableKeepAlive -ErrorAction Continue -Method Post -ContentType "application/json" -Body $bodyJson
$result | Add-Member -NotePropertyName "QueueName" -NotePropertyValue $Name
foreach ($item in $result)
{
$cnt++
$item | Add-Member -NotePropertyName "no" -NotePropertyValue $cnt
$item | Add-Member -NotePropertyName "ComputerName" -NotePropertyValue $ComputerName
$item | Add-Member -NotePropertyName "VirtualHost" -NotePropertyValue $VirtualHost
}
if ($View)
{
switch ($View.ToLower())
{
'payload'
{
SendItemsToOutput $result "RabbitMQ.QueueMessage" | fc
}
'details'
{
SendItemsToOutput $result "RabbitMQ.QueueMessage" | ft -View Details
}
Default { SendItemsToOutput $result "RabbitMQ.QueueMessage" }
}
}
}
}
End
{
Write-Verbose "`r`nGot $cnt messages from queue $Name, vhost $VirtualHost, server: $ComputerName."
}
}