Variable Substitution within Single Quotes

This topic contains 5 replies, has 4 voices, and was last updated by Profile photo of Tracey Mr D J Tracey Mr D J 1 month, 3 weeks ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #49543
    Profile photo of Tracey Mr D J
    Tracey Mr D J
    Participant

    Hi Guys,

    I'm trying to programatically configure WinRM for a number of servers, and am having an issue with one of the commands.

    winrm create winrm/config/listener?Address=*+Transport=HTTPS @{Hostname="$hostname";CertificateThumbprint="$thumbprint"}

    Now – Powershell see's the @ symbol as an operator (array?) and therefore this command only works if i use single quotes before the @ and at the end of the line (and using actual values for hostname and thumbprint, but doing so means that my two variables $hostname and $thumbprint are taken as literals.

    Any ideas on how to approach this?

    Thanks very much.

    Dan

    • This topic was modified 1 month, 3 weeks ago by Profile photo of Tracey Mr D J Tracey Mr D J.
    #49546
    Profile photo of Craig Duff
    Craig Duff
    Participant
    Start-Process -FilePath 'winrm.exe' -ArgumentList "create","winrm/config/listener?Address=*+Transport=HTTPS","@{Hostname='$hostname';CertificateThumbprint='$thumbprint'}" -Wait

    Give that a try.

    #49548
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    This will probably also work:

    winrm create winrm/config/listener?Address=*+Transport=HTTPS "@{Hostname=`"$hostname`";CertificateThumbprint=`"$thumbprint`"}"
    

    That being said, you can also just create listeners via the WSMan:\ drive in PowerShell, which avoids the need for the quotes / escaping:

    New-Item -Path WSMan:\localhost\Listener -Address * -Transport HTTPS -HostName $hostname -CertificateThumtprint $thumbprint
    
    • This reply was modified 1 month, 3 weeks ago by Profile photo of Dave Wyatt Dave Wyatt.
    #49551
    Profile photo of Chris Bakker
    Chris Bakker
    Participant

    Here some info i found:

    #49553
    Profile photo of Tracey Mr D J
    Tracey Mr D J
    Participant

    Guys – thank you very much for your help – i'll give the native powershell code a try 😉

    Dan

    #49567
    Profile photo of Tracey Mr D J
    Tracey Mr D J
    Participant

    one thing that I don't seem to be able to do is to execute this command remotely (in my case via VMware PowerCLi Invoke-VMScript) – i get Access Denied. I'm able to generate the self-signed certificate without an issue.

    Is there a limitation on the WSMan:\ namespace that means it can't be manipulated remotely?

    Thanks,

    Dan

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.