DocNougat / Meraki-Powershell-Module

A PowerShell Module for the Cisco Meraki API
GNU General Public License v3.0
18 stars 3 forks source link

Call to Invoke-MerakiNetworkClaimDevices fails in Powershell but works in Powershell Core #8

Closed abelal83 closed 5 months ago

abelal83 commented 8 months ago

So this is a very strange behaviour.

When this call is made and body is {"serials":["xxxx","xxxx"]}

$response = Invoke-RestMethod -Method Post -Uri $url -Header $header -UserAgent "MerakiPowerShellModule/1.0.2 DocNougat" -Body $body

In Powershell core it works fine, the same in Powershell 5 fails with "The remote server returned an error: (400) Bad Request."

Any ideas what's going on with that?

abelal83 commented 8 months ago

Bit more info

It appears in Powershell Core we get this response in the error

WARNING: { "errors": [ "Primary and spare MXes must be the same model when using Warm Spare." ] }

While in Powershell it's just "The remote server returned an error: (400) Bad Request." and nothing else.

abelal83 commented 8 months ago

Found the issue thanks to https://stackoverflow.com/questions/35986647/how-do-i-get-the-body-of-a-web-request-that-returned-400-bad-request-from-invoke

Not an issue with this module, just a poor implementation of invoke-restmethod in old powershell.

abelal83 commented 8 months ago

Can I suggest this bit of code is added to each of the invoke-restmethod try blocks TO help those unfortunates like me forced to use Powershell 5 or below?

try {
   .....
}
catch {
        if ($PSVersionTable.PSEdition -eq 'Desktop') {
            # Classic PS swallows the error response, so we get it here
            $streamReader = [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream())
            $errResp = $streamReader.ReadToEnd()
            $streamReader.Close()
            Write-Debug $errResp
        }

throw $_
}
DarkStar-Zaxxon commented 8 months ago

@abelal83 Using v5 of PowerShell. I have had the same (400) errors and what I have found it that it is a matter of the CASE of the information being supplied. An example of this is working with WAPs I found that the ipAssignmentMode has to be set as "NAT mode" not "NAT Mode" or "nat mode" it has to be upper lower case exactly right. I have not tested in other versions of PowerShell but if that is the case then using a newer version would have caused a lot less programming time for me.

abelal83 commented 8 months ago

@DarkStar-Zaxxon it's definitely the case. It's a well known issue with invoke-rest that doesn't exist in powershell core.

In any case I recommended using powershell core over powershell desktop. Unfortunately for me we have servicenow which only works with powershell desktop so I don't have a choice.

DocNougat commented 5 months ago

@abelal83 Sorry it took so long to respond to this, haven't been on my git in a while. Closing since this isn't an issue with the module.

Good to know about ServiceNow. My company uses it as well and I'm in the process of converting my Power Platform based workflows over to it, so thanks for the heads up on that one.

abelal83 commented 5 months ago

No problem. I would strongly recommend avoid using servicenow powershell actions. They are extremely limited in functionality and goes against well established development practices.

What I now do is use servicenow to call to my scripts via an api because I simply can't stand for a tool to force me to learn it's way of working and limiting what I can do. Good luck with servicenow, I wish we didn't use it!