Suggestion for complex DOS command in PowerShell RRS feed

  • Question

  • Hello,

    I am trying to simplify creating an HTTPS listener for PowerShell remoting by running all the commands in a PowerShell command window, instead of having to open up a DOS prompt to do that last part.  One benefit is using a variable to pass the certificate thumbprint to the winrm command, so I don't have to copy and past it into the DOS command.  Here is what I want to do:

    $tp = (gci Cert:\LocalMachine\My | where {$_.Subject -eq "CN=$($env:COMPUTERNAME)"}).Thumbprint
    $hn = ($env:COMPUTERNAME).ToUpper()
    winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{Hostname="$hn";CertificateThumbprint="$tp"}

    The problem being the last line...PowerShell gets hung up on all the odd characters.  I have tried --% but that breaks the $hn and $tp variables.  I also thought about cmd /c - but I think that all breaks the variables too.

    So I am wondering if there is a sure fire method most people use when dealing with variables and odd characters in DOS commands within the PowerShell command window.  Do I just need to escape out all the characters?  If I do that, will the variables still work?  Or maybe there is a native PowerShell command for the winrm that I am just overlooking?



    • Edited by NJK-Work Monday, November 30, 2015 4:05 PM
    Monday, November 30, 2015 4:04 PM


All replies

  • Please read this article first.

    Windows IT Pro: Running Executables in PowerShell

    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by NJK-Work Monday, November 30, 2015 4:10 PM
    Monday, November 30, 2015 4:06 PM
  • Excellent - thank you very much.


    Monday, November 30, 2015 4:09 PM
  • winrm create winrm/config/Listener?Address=*+Transport=HTTPS `@`{Hostname=`"$hn`"`;CertificateThumbprint=`"$tp`"`}
    Monday, November 30, 2015 5:10 PM
  • Here's another way:

    winrm create winrm/config/Listener?Address=*+Transport=HTTPS "@{Hostname=""$hn"";CertificateThumbprint=""$tp""}"

    But whatever makes it easier for you to read is good.

    -- Bill Stewart [Bill_Stewart]

    Monday, November 30, 2015 6:06 PM
  • Awesome.  I like your way better.  I did try using double-quotes when I was testing stuff out but I put my first one in front of the "create" instead of like you have it in front of the "@".  Not sure why my way failed....but at least I now have a solution.

    Thanks again for your time and the article you posted.


    Monday, November 30, 2015 6:24 PM
  • NP. BTW there is no more "DOS". I think you mean console or command line.

    -- Bill Stewart [Bill_Stewart]

    Monday, November 30, 2015 6:51 PM
  • Indeed. I guess old habits, much like Mason Storm, are "hard to kill" :)


    Monday, November 30, 2015 7:31 PM
  • New-Item -Path WSMan:\localhost\Listener -Address * -Hostname $hs -Transport HTTPS -CertificateThumbPrint $tp -force



    Monday, November 30, 2015 10:27 PM
  • Even better! Thanks!


    Monday, November 30, 2015 10:31 PM