0

I want to remotely make WinRM listen to the HTTPS port using PowerShell.

If I activate the "HTTPS mode" manually, my remote command works well using this function

function Execute-Remote {
    [CmdletBinding()]
    Param(
        [Parameter(mandatory=$true)][String]$command
    )

    Process {
        $finalCommand = "start cmd.exe '/C " + $command + "'"
        Invoke-Expression -Command $finalCommand
        return $finalCommand
    }
}

#Works
Execute-Remote "WinRM delete winrm/config/Listener?Address=*+Transport=HTTPS"
#Command sent : start cmd.exe '/C WinRM delete winrm/config/Listener?Address=*+Transport=HTTPS'

But, as soon as I add the certificate thumbprint, it doesn't work anymore

#Doesn't work
Execute-Remote 'winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{CertificateThumbprint="example"}'
#Command sent start cmd.exe '/C winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{CertificateThumbprint="example"}

I guess that it is the argument that doesn't pass.

I tried using variables and casts. The command that I try to execute remotely is exactly the same as the one I use to activate HTTPS manually but it does nothing.

What do I miss?

Nix
  • 351
  • 5
  • 13
  • Please elaborate "doesn't work" - do you receive an error? What happens? (FWIW the thumbprint in your example is not valid, but I guess you just put nonsense data there) – Mathias R. Jessen Jan 31 '18 at 14:05
  • I get nothing, no error message, but the "HTTPS mode" is not activated. Yeap I have put nonsense in this example. – Nix Jan 31 '18 at 14:09
  • `Execute-Remote` is not a standard PowerShell cmdlet. Where does this come from, and what is it supposed to do? – Jeff Zeitlin Jan 31 '18 at 14:12
  • Oh my... It's better with function definition, you're right – Nix Jan 31 '18 at 14:19
  • The problem arises b/c `Invoke-Expression` (which [should be avoided anyway](https://blogs.msdn.microsoft.com/powershell/2011/06/03/invoke-expression-considered-harmful/)) interprets the `@{...}` as a PowerShell hashtable, which `cmd` can't process. Try `& cmd /c $command`. – Ansgar Wiechers Jan 31 '18 at 14:36

0 Answers0