1

I have a script like this:

Write-Host "Create RabbitMQ infrastructure..."

$rabbitHost = "http://my.company.com:15672"
$serverPort = 12345
$prefix = "unit"

$user = "test"
$pass = "test"

$secpasswd = ConvertTo-SecureString $pass -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($user, $secpasswd)

$exchanges = ("MyExchange")

$exchanges | foreach {
    Write-Host "Create $_"

    $body = @{
        type    = "direct"
        durable = "true"
    }

    $json = $body | ConvertTo-Json

    try{
        $url = "$rabbitHost/api/exchanges/%2f/$prefix" + "_" + "$serverPort" + "_" + $_
        Invoke-RestMethod $url -Body $json -Method Put -Credential $cred -ContentType 'application/json'
    } catch {$_.Exception}
}

Everything fine, when I launch it from PowerShell ISE, but when I run it from PowerShell I get an error:

The remote server returned an error: (405) Method Not Allowed.

I know, that it can be somehow connected with RabbitMq, but I don't see how, because it is just a simple request.

Can you help me find a root cause of these differences?

  • When I try send request using RabbitMq API with -Method Get -- everything fine in powershell and powershell ise without any error. – user3674195 Oct 22 '17 at 21:27
  • And I launch powershell and powershell ise under Administrator mode. – user3674195 Oct 22 '17 at 21:28
  • 3
    Powershell is session-based. That is your ISE session may have already some variables set and connections open. Also ISE and console may use different .NET frameworks. – Anton Krouglov Oct 22 '17 at 21:29
  • Thank you for you reply. Everything fine when I use -Method Get. And can you tell me, how I can determine .NET frameworks version for cmd and ise? – user3674195 Oct 23 '17 at 06:52
  • You can see the .net version from [System.Environment]::Version or $PSVersionTable (more specifically $PSVersionTable.CLRVersion). – robbp Dec 12 '17 at 18:57

1 Answers1

0

I wasn't able to reproduce this locally but after some further digging I found this post which refers to another post with more information regarding issues with the %2f URL-encoded character (/).

The first post I referenced likely is the issue you were experiencing where the %2f is decoded before the request is made therefore changing the way the URL is interpreted. Note that the %2f refers to the default virtual host (see RabbitMQ api documentation).

robbp
  • 175
  • 1
  • 5