0

I have 2 machines (workstation and server) that I am trying to run the sample code below on, and get a different result on each machine. Both machines have a very close PS version (but not exact). I'm stuck at where to look next for this issue.

Sample code:

$xml = "<contactUser><name>Some name</name></contactUser>"
Invoke-RestMethod -Credential (Get-Credential) -Method "Post" -Uri "https://app/api/blah" -Body $xml -ContentType "application/xml"

On our server (Win 2012 R2), I get the following error message. The interesting thing is that the request seems to execute without any issues. I can check the app afterwards and verify that it worked, but I still get the error message:

Invoke-RestMethod : Value cannot be null. 
Parameter name: name

On my workstation (Win10 Enterprise LTSB ver 1607), the same code runs fine.

Here are the values from $PSVersionTable:

Server (not working):

Name Value

PSVersion 5.1.14409.1005
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14409.1005
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

Workstation (working):

Name Value

PSVersion 5.1.14393.3471
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.3471
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

I tried to search out the specific PS version to install on the server, but couldn't find anything. I also thought that the .NET Framework versions on each machine might be different and could be the source of the issue, but they both appear to have the same version installed.

I also tried installing PowerShell 7 on the server, and the code ran without issues, but I ran into other issues when trying to load the module we've developed to work with the API for this app, and I'm not sure how easy it would be to update our code.

So that's where I am now - unsure of what I can look at next to find the difference between my workstation and the server, to hopefully fix the problem.

Any suggestions would be a huge help...

  • Does the `Get-Credential` call work the same in both environments? – Forty3 Aug 05 '20 at 18:42
  • 1
    The issues you are seeing seem to be along the same lines as : https://stackoverflow.com/questions/36275618/why-is-invoke-webrequest-and-invoke-restmethod-failing-and-succeeding-at-the-sam . I don't have an environment to replicate, so this is a guess, but try using -ContentType "application/xml; charset=UTF-8" – Graham Aug 05 '20 at 18:54
  • Yes, same credential. Both machines connect to the same DB/app instance and run the exact same code, the only difference is the computer that the command is being run from –  Aug 05 '20 at 18:54
  • It’s a long shot, but you could try sniffing the http traffic to see if you get the same response from the api to both machines - e.g. using Fiddler, or enabling Network Tracing in powershell.exe.config – mclayton Aug 05 '20 at 21:43
  • @Graham Yes, seems like the exact same issue. It doesn't seem like there is an easy fix for it at this point, adding the charset to the content-type doesn't resolve the issue. It seems like the solution at this point is going to be to migrate any services that rely on the API to Server 2016 –  Aug 05 '20 at 22:41
  • Could this be the same as in [this question](https://stackoverflow.com/questions/36275618/why-is-invoke-webrequest-and-invoke-restmethod-failing-and-succeeding-at-the-sam) – Theo Aug 06 '20 at 12:37

0 Answers0