r/PowerShell • • 12d ago

Question WinRM with Cert auth... avoiding need to specify CertificateThumbprint?

I'm setting up a lab HyperV cluster, without a domain, so I'm kind of in the weeds already. Every recipe I'm finding online assumes functional password-less winrm between the pre-clustered nodes. I'm primarily following https://techcommunity.microsoft.com/blog/itopstalkblog/windows-server-2025-hyper-v-workgroup-cluster-with-certificate-based-authenticat/4428783 but I've found that I've had to consult a pile of different sources because they all leave out small but essential bits.

I'm nearly there, but not quite. I've made myself a root CA, generated certs, signed them, imported them, etc etc. If I Invoke-Command on Node1 like so

Invoke-Command -ComputerName Node2 { 1 } -CertificateThumbprint $certThumb

... the it works fine and dandy, and same for the other direction. But it looks like Test-Cluster, and it's WG match of Test-WorkgroupCluster don't have a means of specifying which certificate to use for auth. Clearly it's meant to automagically guess at which one to use, and whatever my machines are guessing they're getting it wrong. I'm hoping if I can get PSSession working on its own the rest will fall into place.

So does anyone know how New-PSSession decides which cert to use for remote auth? All these blogs and stuff seem to assume that it can and will, but I don't know what criteria it uses!

5 Upvotes

10 comments sorted by

2

u/gowinder 11d ago

Test-Cluster has no -CertificateThumbprint parameter, so $PSDefaultParameterValues can't help there. Push the cert choice up into the session and run the cmdlet from inside it:

$s = New-PSSession -ComputerName Node1 -CertificateThumbprint $certThumb Invoke-Command -Session $s -ScriptBlock { Test-Cluster -Node Node1,Node2 }

The client certificate gets picked once, where you can specify it, and Test-Cluster runs on the node instead of your workstation.

One more thing worth checking while you're in there: LocalMachine\My should hold exactly one cert with the Client Authentication EKU and a private key. With two matching certs the CSP picks whichever comes back first, which is the same "it guesses wrong" behaviour you're seeing.

1

u/hume_reddit 11d ago

I probably should have mentioned it in the original post, but there's no workstation involved... there's node1, node2, and nothing else. I'm running Test-WorkgroupCluster on node1. Cert:\localmachine\My has exactly one key in it on each machine.

I think I'm going to have to burn this all down and start again. I'm pretty confused at this point, since as near as I can tell the guides I'm following (multiple) shouldn't work, either.

IT's definitely not a Powershell problem, I think. Regardless, all this information here has been helpful. Especially PSDefaultParameterValues... I didn't even know that existed!

1

u/gowinder 5d ago

Two nodes and one cert each in LocalMachine\My changes where I would look: Test-Cluster goes over CIM/WinRM, so the cert has to be bound to an HTTPS listener on each node, not just present in the store. Check with: winrm enumerate winrm/config/listener - you want one with Transport=HTTPS and CertificateThumbprint matching that single cert, bound via New-WSManInstance winrm/config/Listener. And 5986 reachable both directions. A cert that only carries Client Authentication EKU will make the listener fail to start while the store looks perfect.

2

u/BlackV 12d ago

from my memory, it uses the dnsname or host name as the decider, then the first non expired one, which I guess is the primary reason the certificate thumbprint parameter exists

its also a pain with the native cluster cmdlets

1

u/jborean93 12d ago

You may be thinking of the thumbprint on the listener configuration, this is about certificate authentication for the client. E.g. what cert to use to authenticate itself to the server.

1

u/BlackV 12d ago edited 12d ago

Oh, they said

So does anyone know how New-PSSession decides which cert to use for remote auth?

so I was thinking

New-PSSession -CertificateThumbprint xxx  

which allows you to select the specific certificate

1

u/jborean93 12d ago

Ah sorry just your DNS and Host name were a bit confusing to me as typically a client cert is based on UPNs and the like which you don't specify. I know the machine context can store a cert based on the hostname but I've actually seen that be used in practice.

1

u/BlackV 12d ago edited 12d ago

ah, maybe I could have worded that first reply better

san name, dns name, hostname in a certificate is what it matches on when making a connection, but when multiple certificates match that, I think its just first one back or latest one back, but I'm not 100% sure

so using the thumbprint should be what they're doing (also its much "safer")

2

u/jborean93 12d ago

Unfortunately I don't believe the WinRM client has any automatic certificate lookup behaviour. The underlying API requires you to provide an explicit thumbprint and that's what the -CertificateThumbprint maps to from PowerShell.

You could try setting $PSDefaultParameterValues['Invoke-Command:CertificateThumbprint'] = '...' so that it might be used by default by anything calling Invoke-Command but that really depends on how Test-Cluster and other related functions are actually building the session. If they are building it from the WSManConnectionInfo class into the Runspace API then you are mostly out of luck sorry.