Page 1 of 1

Wie richten Sie eine Remote -PowerShell mit wsmanconnectionInfo in C# mithilfe der Zertifikatauthentifizierung ein?

Posted: 11 Jul 2025, 11:06
by Anonymous
Ich versuche, eine Remote PowerShell (RPS) zu erstellen, die eine Verbindung zwischen Maschinen herstellt, um RPS -Befehle auszuführen. Ich habe dieses folgende Beispiel-Setup " hostPC mit C# -Konsole-Anwendung (die RPS ausführt), um eine Verbindung zwischen zwei Remote-PCs zu formen "
Der folgende Link (Zertifikat (kennwortfrei) basiert in WinRM in WinRM) wurde von einem der Kommentare in der Antwort in der Antwort gefunden. Aus dem Artikel stellte der Autor einen hochrangigen Ansatz zum Erstellen von Zertifikat (kennwortfrei) basierter Authentifizierung in WinRM vor und sind Folgendes: < /p>
  • Konfigurieren von SSL-Konnektivität zu WinRM auf dem Endpoint < /li>
    generieren Sie eine Benutzerzertifikat-Zertifikat-Authentifizierung. Es ist standardmäßig für Serverauth deaktiviert und auf der Client -Seite aktiviert. /> Zertifikatauthentifizierung Setup: < /h1>

    Konfiguriertes WinRM für die Arbeit mit HTTPS in Remote -PCs < /li>
    Erstellt Client -Zertifikat in HostPC < /li>
    Das Client -Zertifikat und importiert das Remote -PCS -. on the Remote PCs
  • Created Certificates in each Remote PC
  • Binded the Remote PC certificates to the HTTPS Listener
  • Created the winrm user mapping using the HostPC certificate in each Remote PC
Configured WinRM to work with Https

Code: Select all

winrm quickconfig -transport:https
Zertifikaterstellung

Code: Select all

New-SelfSignedCertificate `
-DnsName 
 `
-CertStoreLocation  `
-KeyLength 2048 `
-NotAfter (Get-Date).AddYears(xx) `
-TextExtension @("2.5.29.37={text}")
Zertifikatauthentifizierung

Code: Select all

winrm set winrm/config/service/auth @{Certificate="true"}
Bindungszertifikat an Https Listener

Code: Select all

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

Code: Select all

New-Item -Path WSMan:\localhost\ClientCertificate `
-Subject '' `
-URI * `
-Issuer   `
-Credential (Get-Credential)
< /code>
 C# Anwendungsseite: < /h1>

connectionInfo = new WSManConnectionInfo(new Uri($"https://{MachineName}:5986/wsman"),
"http://schemas.microsoft.com/powershell/Microsoft.PowerShell", (PSCredential?)null);
connectionInfo.CertificateThumbprint = CertLoader.CertificateLoad(MachineName);


public string? CertificateLoad(string? machinename)
{
X509Store? store = null;
X509Certificate2? cert = null;

try
{
if(machinename == null)
{


return null;
}

store = new X509Store(, );
store.Open(OpenFlags.ReadOnly);
cert = store.Certificates.OfType().FirstOrDefault(c =>
c.Subject.Contains($"{machinename}", StringComparison.OrdinalIgnoreCase));
store.Close();
return cert.Thumbprint;
}
catch (Exception ex)
{

return null;
}
}



using (runspace = RunspaceFactory.CreateRunspace(connectioninfo))
{
runspace.Open();
using (pipeline = PowerShell.Create())
{
fehlschlägt jedoch immer wieder, wenn es mit der Verwendung (runspace = runspacefactory.createrunpace (ConnectionInfo)) mit der folgenden Fehlermeldung:
erreicht wird:

Code: Select all

Exception Message: System.Management.Automation.PSInvalidOperationException:  The WS-Management service cannot find the certificate that was requested.
at System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager.Initialize(Uri connectionUri, WSManConnectionInfo connectionInfo)
at System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager..ctor(Guid runspacePoolInstanceId, WSManConnectionInfo connectionInfo, PSRemotingCryptoHelper cryptoHelper, String sessionName)
at System.Management.Automation.Runspaces.WSManConnectionInfo.CreateClientSessionTransportManager(Guid instanceId, String sessionName, PSRemotingCryptoHelper cryptoHelper)
at System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerImpl..ctor(ClientRemoteSession session, PSRemotingCryptoHelper cryptoHelper, RunspaceConnectionInfo connectionInfo, URIDirectionReported uriRedirectionHandler)
at System.Management.Automation.Remoting.ClientRemoteSessionImpl..ctor(RemoteRunspacePoolInternal rsPool, URIDirectionReported uriRedirectionHandler)
at System.Management.Automation.Internal.ClientRunspacePoolDataStructureHandler.CreateClientRemoteSession(RemoteRunspacePoolInternal rsPoolInternal)
at System.Management.Automation.Internal.ClientRunspacePoolDataStructureHandler..ctor(RemoteRunspacePoolInternal clientRunspacePool, TypeTable typeTable)
at System.Management.Automation.Runspaces.Internal.RemoteRunspacePoolInternal.CreateDSHandler(TypeTable typeTable)
at System.Management.Automation.Runspaces.Internal.RemoteRunspacePoolInternal..ctor(Int32 minRunspaces, Int32 maxRunspaces, TypeTable typeTable, PSHost host, PSPrimitiveDictionary applicationArguments, RunspaceConnectionInfo connectionInfo, String name)
at System.Management.Automation.Runspaces.RunspacePool..ctor(Int32 minRunspaces, Int32 maxRunspaces, TypeTable typeTable, PSHost host, PSPrimitiveDictionary applicationArguments, RunspaceConnectionInfo connectionInfo, String name)
at System.Management.Automation.RemoteRunspace..ctor(TypeTable typeTable, RunspaceConnectionInfo connectionInfo, PSHost host, PSPrimitiveDictionary applicationArguments, String name, Int32 id)
at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable, PSPrimitiveDictionary applicationArguments, String name)
at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable)
at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(PSHost host, RunspaceConnectionInfo connectionInfo)
at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(RunspaceConnectionInfo connectionInfo)
Zusätzlich (ich kann mich nicht erinnern, was ich getan habe), bevor ich den Fehler oben bekam, ist es im Runspace fehlgeschlagen.open (); Teil mit der folgenden Nachricht:
System.Management.Automation.Remoting.PSRemotingTransportException: 'Connecting to remote server xxxxxxxxx failed with the following error message :
WS-Management cannot process the request.
The operation failed because of an HTTP error.
The HTTP error (12186) is: The client certificate credentials were not recognized. . For more information, see the about_Remote_Troubleshooting Help topic.'
< /code>
Kann mir jemand helfen: < /p>

Identifizieren Sie, was ich falsch gemacht habe?>