masterzen/winrm

http response error: 401 - invalid content type

DenisBY opened this issue ยท 6 comments

Getting error above on this command:

winrm -hostname "192.168.250.128" --username "user" -password "password" "ifconfig /all"

Windows has default settings, I can connect using pywinrm.

I haven't tried pywinrm yet but be sure to set the following on the server for this winrm client.

winrm set winrm/config/client/auth '@{Basic="true"}'
winrm set winrm/config/service/auth '@{Basic="true"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'

Also checkout http://www.hurryupandwait.io/blog/understanding-and-troubleshooting-winrm-connection-and-authentication-a-thrill-seekers-guide-to-adventure to help debug if the above doesn't work for you.

I had the exact same issue. Unfortunately the error message is somewhat confusing. The status code is there, and points you in to the right direction, but the error message "invalid content type" is a bit meaningless in this case. I think the reason for the "invalid content type" message is because the body is basically empty in this particular scenario. So in http.go on line 87 we get the body and then we append this to the console message. In case of 401, the body is empty and the Body() function throws its error.I think a simple "401 - Unauthorized" message would be sufficient in this case.

So, about the authentication issue. In my case the winrm set winrm/config/service '@{AllowUnencrypted="true"}' has not been set. I didn't notice the error message when I tried to configure this setting:

WSManFault
    Message
        ProviderFault
            WSManFault
                Message = WinRM firewall exception will not work since one of the network connection types on this machi
ne is set to Public. Change the network connection type to either Domain or Private and try again.

Error number:  -2144108183 0x80338169
WinRM firewall exception will not work since one of the network connection types on this machine is set to Public. Chang
e the network connection type to either Domain or Private and try again.

This is due to one of my network adapters being set to the public profile. I couldn't force it though and I didn't have time to figure out if there is a way. It seems that this exception actually prevents the setting from being configured. So you can double check it with this command:

winrm get winrm/config/service

I then used the following command to figure out which interface is configured to public.

# Get the public networks
$PubNets = Get-NetConnectionProfile -NetworkCategory Public -ErrorAction SilentlyContinue 

# Set the profile to private
foreach ($PubNet in $PubNets) {
    Set-NetConnectionProfile -InterfaceIndex $PubNet.InterfaceIndex -NetworkCategory Private
}

# Configure winrm
winrm set winrm/config/service '@{AllowUnencrypted="true"}'

# Restore network categories
foreach ($PubNet in $PubNets) {
    Set-NetConnectionProfile -InterfaceIndex $PubNet.InterfaceIndex -NetworkCategory Public
}

After this, it worked for me in my lab. This is not suitable for production use though :)

I got this error when using the API and had the wrong credentials in my winrm.NewClient call

I'm hitting this error currently, despite winrm get winrm/config/service showing that AllowUnencrypted is set to true:

    AllowUnencrypted = true
    Auth
        Basic = true
        Kerberos = true
        Negotiate = true
        Certificate = false
        CredSSP = false
        CbtHardeningLevel = Relaxed
    DefaultPorts
        HTTP = 5985
        HTTPS = 5986
    IPv4Filter = *
    IPv6Filter = *
    EnableCompatibilityHttpListener = false
    EnableCompatibilityHttpsListener = false
    CertificateThumbprint
    AllowRemoteAccess = true

Is there something obvious I'm missing?

Got this error when I didn't have Basic auth enabled. It looks like pywinrm must have been using Negotiate with username/password (I was able to leave AllowUnencrypted set to false)

It's probably because the server requires NTLM authentication - I have a PR in to implement this in this repo but this seems to be not very active

you could use https://github.com/CalypsoSys/bobwinrm

see https://github.com/CalypsoSys/bobwinrmntlm for usage