Powershell is hell

winrm complaining “Error: Invalid use of command line.” – easy fix :)

This post was most recently updated on December 7th, 2022.

2 min read.

Heh, this article is going to be another one of those “Drat, I should’ve known this” articles. But maybe it’ll be helpful for someone else as well!

In this post I will explain how to get around the “Error: Invalid use of command line. Type “winrm -?” for help.” error being thrown at you. And if you’re like me (and your error is the same as mine), you’ll mostly get to blame yourself for this one!

Background

The other day I was tweaking some PowerShell Remoting settings.

winrm set winrm/config/client/auth @{CredSSP="true"}

But this kept throwing an error:

Error: Invalid use of command line. Type “winrm -?” for help.

Running winrm set -? produces this help:

Windows Remote Management Command Line Tool

winrm set RESOURCE_URI [-SWITCH:VALUE [-SWITCH:VALUE] ...]
          [@{KEY="VALUE"[;KEY="VALUE"]}]
          [-file:VALUE]

Modifies settings in RESOURCE_URI using specified switches
and input of changed values via key-value pairs or updated
object via an input file.

Example: Modify a configuration property of WinRM:
  winrm set winrm/config @{MaxEnvelopeSizekb="100"}

Example: Disable a listener on this machine:
  winrm set winrm/config/Listener?Address=*+Transport=HTTPS @{Enabled="false"}

Example: Disable a certmapping entry on this machine:
  Winrm set winrm/config/service/certmapping?Issuer=1212131238d84023982e381f20391a2935301923+Subject=*.example.com+URI=wmicimv2/* @{Enabled="false"}


See also:
  winrm help uris
  winrm help aliases
  winrm help input
  winrm help switches

But running winrm set winrm/config @{MaxEnvelopeSizekb=”100″} produces exactly the same error:

PS C:\Users\anko> winrm set winrm/config @{MaxEnvelopeSizekb="100"}
Error: Invalid use of command line. Type "winrm -?" for help.

What’s up with that?

Reason

The help files have not been “localized” for PowerShell. In PowerShell, you need to enquote and escape more.

Let’s dive in, shall we?

Solution

Time needed: 5 minutes

Localizing the files for PowerShell.

  1. Enclose the configuration value itself in single quotes.

    Something like this:

    winrm set winrm/config/client/auth '@{CredSSP="true"}'

  2. Or, like in the misleading sample above but properly enclosed:


    winrm set winrm/config '@{MaxEnvelopeSizekb="100"}'

  3. Likewise, in PowerShell, you must escape any variables in your script. A sample I found on GitHub (link in references) after digging into the error:


    winrm create winrm/config/Listener?Address=*+Transport=HTTPS '@{Hostname="'"$ENV:ComputerName"'";CertificateThumbprint="'"$($Cert.Thumbprint)"'"}'

well, that’s it for today :)

References

mm
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments