1

I am trying to configure WinRM from a PowerShell script. I am using the following code

$WinrmCreate= "winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{Hostname=""$sHostName"";CertificateThumbprint=""$CertificateThumbPrint""}"
Invoke-Expression $WinrmCreate

The hostname and certificate thumprint are variables above and they are validated to be coming out. Verbose output shows

VERBOSE: winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{Hostname="RSNODE1";CertificateThumbprint="6C79C8E6E88779037593E6234DCE6E1A55662F87"}

However, the execution of the "Invoke-Expression" give the following error

Invalid use of command line. Type "winrm -?

Interestingly if I copy/paste the output of my line to a command prompt it will create the listener without error.WInrm

Researching execution of commands I find nothing that would explain this behavior. My only theory is that the "winrm" command is a script itself and something is misbehaving.

1
  • Your theory is correct. It is a cmd script that kicks off a ~3900 line VB script: @cscript //nologo "%~dpn0.vbs" %* Commented Jul 25, 2014 at 20:50

2 Answers 2

6

Hashtables e.g. @{Hostname=...} are interpreted by PowerShell. If you are on V3 or higher try the --% simple argument parsing operator:

$env:Hostname = $sHostName
$env:CertThumbPrint = $CertificateThumbPrint
$WinrmCreate= "winrm create --% winrm/config/Listener?Address=*+Transport=HTTPS @{Hostname=`"%HostName%`";CertificateThumbprint=`"%CertThumbPrint%`"}"
Sign up to request clarification or add additional context in comments.

1 Comment

You gave me an "ah HA!" moment , I was down the same track. ended up using: $WinrmCreate= "winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{Hostname=""$sHostName"";CertificateThumbprint=""$CertificateThumbPrint""}" $WinrmArgs = "create winrm/config/Listener?Address=*+Transport=HTTPS @{Hostname=""$sHostName"";CertificateThumbprint=""$CertificateThumbPrint""}" Start-Process "winrm" -ArgumentList $WinrmArgs`
0

This is what I did for me, the certificates have the FQDN:

$fqdn ="CertificateSubjectName"

$certificate="CN="+$fqdn

$CertificateThumbPrint=(Get-ChildItem cert:\LocalMachine\My | Where-Object { $_.Subject -eq $certificate }).thumbprint

$WinrmCreate= 'winrm create winrm/config/Listener?Address=*+Transport=HTTPS `@`{Hostname=`"`'+$fqdn+'`"`;CertificateThumbprint=`"`'+$CertificateThumbPrint+'`"`}' 

invoke-expression $WinrmCreate

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.