PowerShell连接RabbitMQ时重复要求输入凭据问题求助
PowerShell/RabbitMQ 项目中重复弹出凭据输入框的问题
我正在开发一个PowerShell/RabbitMQ项目,之前连接RabbitMQ监听器时遇到了如下错误:
Invoke-RestMethod : {"error":"bad_request","reason":"[{key_missing,ackmode}]"}
我通过在GetMessage.ps1文件中添加以下代码修复了这个问题:
"ackmode" = @("ack_requeue_true", "ack_requeue_false")[[bool]$Remove]
现在功能基本正常——执行查询或存储过程时RabbitMQ能返回操作消息,但又出现了新问题:明明已经用$Credential = Get-Credential保存了凭据,PowerShell却会针对SQL结果的每一行要求输入凭据,甚至在没有执行任何操作时,每隔3-5秒就会弹出凭据输入请求。
以下是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." } }
有没有大佬能帮忙解决这个凭据重复输入的问题?
内容的提问来源于stack exchange,提问作者Anion
相关产品推荐
相关产品推荐

