0

I have trouble with following piece of code:

$result = "F651F545A059588ABFDCE7EE3EEB27EED8CED28D"
winrm create winrm/config/Listener?Address=*+Transport=HTTPS '@{Hostname="my.host.winagent"; CertificateThumbprint="$result"}'

it yields:

Message = The WS-Management service cannot process the configuration request because the certificate thumbprint in the request is not a valid hex string: $result.

it looks like variable result is not being expanded, probably because it is inside array? How to get the variable value expanded there?

7
  • 2
    Type the command into powershell and the syntax highlighter will show you the difference between using ' and ". Commented Nov 19, 2021 at 15:10
  • Try removing the single quotes. Commented Nov 19, 2021 at 15:14
  • In short: Only "..." strings (double-quoted) perform string interpolation (expansion of variable values) in PowerShell, not '...' strings (single-quoted): see this answer for an overview of PowerShell's expandable strings (interpolating strings) and this answer for an overview of PowerShell string literals in general. Commented Nov 19, 2021 at 18:08
  • @Santiago, not quoting the argument would cause PowerShell to parse it as a regular hashtable, whose .ToString() stringification would then be passed (because an external program is being called), (uselessly) resulting in verbatim System.Collections.Hashtable getting passed. Commented Nov 19, 2021 at 18:11
  • 1
    I see, @Santiago, but the linked article's commands were written for cmd.exe. The behavior I describe is a pitfall, so much so that core members of the PowerShell team, many years ago, pondered making an exception for passing such hashtable-like arguments, but decided against it - see GitHub issue #1761. You can demonstrate that it doesn't work via the following commands: cmd /c echo @{ foo = 1 } (Windows) and /bin/echo @{ foo = 1 } (Unix) Commented Nov 19, 2021 at 19:38

1 Answer 1

2

You need to use " (not ') to create an expandable string literal - escape the literal "'s by doubling them:

winrm create winrm/config/Listener?Address=*+Transport=HTTPS "@{Hostname=""my.host.winagent""; CertificateThumbprint=""$result""}"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.