Bonjour,

Cela fait quelque jour que j'essaye tant bien que mal de créer un remote runspace sur un serveur Hyper-V 2008R2

Je crée cette petit application dans le but de pouvoir configurer mes VMs depuis une interface user friendly et plus devoir prendre la main sur le powershell du serveur.

Donc pour cela j'ai installer le module PShyperv-R2 sur le serveur 2008r2, jusque la pas de problème, j'arrive même a prendre le powershell du serveur avec Enter-PSSession 192.168.1.158

J'importe le module et je peut jouer avec mes vm

Le problème est que depuis C# lorsque je veux me connecter, il me dit:
La connexion au seveur distant a échoué avec le message d'erreur suivant:
Le client WinRM ne peut pas traiter la demande.
Le trafic non chiffé est actuellement désactivé dans la configuration du client.
Modifiez la configuration du client et renouvelez la demande.
Pour plus d'informations, voir la rubrique d'aide about_Remote_Troubleshooting.
J'ai été voir mais j'ai rien trouvé de spécial concernant ce message.

voici le code utilisé:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
 
class RemotePS
    {
        private Runspace runspace;
 
        public RemotePS(string uri, string schema, string username, string livePass)
        {
            this.runspace = openRunspace(uri, schema, username, livePass);
        }
 
        /*<summary>
        Opens remote runspace
        </summary>
        <param name="uri">Uri to connect to, for example https://pod51002psh.outlook.com/powershell/"</param>
        <param name="schema">Schema of connection, for example http://schemas.microsoft.com/powershell/Microsoft.Exchange</param>
        <param name="username">Username</param>
        <param name="password">Secure pasword</param>
        <returns>Runspace</returns> */
        public Runspace openRunspace(string uri, string schema, string username, string livePass)
        {
            SecureString password = new SecureString();
            foreach (char c in livePass.ToCharArray())
            {
                password.AppendChar(c);
            }
 
            PSCredential psc = new PSCredential(username, password);
            WSManConnectionInfo rri = new WSManConnectionInfo(new Uri(uri), schema, psc);
            rri.AuthenticationMechanism = AuthenticationMechanism.Basic;
           // rri.ProxyAuthentication = AuthenticationMechanism.Negotiate;
            Runspace runspace = RunspaceFactory.CreateRunspace(rri);
            try
            {
                runspace.Open();
            }
            catch (InvalidRunspaceStateException ex)
            {
                MessageBox.Show("" + ex);
            }
            catch (Exception ex)
            {
                MessageBox.Show("" + ex);
            }
            return runspace;
        }
 
        private Runspace getRunSpace()
        {
            if (this.runspace.RunspaceStateInfo.State != RunspaceState.Opened)
                try { this.runspace.Open(); }
                catch (Exception ex)
                {
                    MessageBox.Show("" + ex);
                } 
            return this.runspace;
        }
 
        /*
        <summary>
        Invoke simple command
            Ex.
                Collection<PSObject> col = RunCommand("Get-Mailbox");
        </summary>
        <param name="command">Command Name</param>
        <returns></returns>
         */
        public Collection<PSObject> RunCommand(string command)
        {
            Command myCommand = new Command(command);
            Pipeline pipeLine = getRunSpace().CreatePipeline();
            pipeLine.Commands.Add(myCommand);
            Collection<PSObject> ret = null;
            try
            {
                pipeLine.Invoke();
            }
             catch (Exception ex)
            {
                MessageBox.Show("" + ex);
            }
            return ret;
        }
Je l'appel comme ceci:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
 
//rps est un attibut de mon winform.
RemotePS rps;
 
// au click sur le bouton connect
this.rps = new RemotePS("http://192.168.1.158", "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", "user", "password");           
            Collection<PSObject> Test = rps.RunCommand("get-VM");
           ...
Voila voila...

Je suis un peut dépiter par ce problème parce que j'arrive bien a me connecter depuis mon powershell.

Si quelqu'un a une piste ou une solution je suis preneur.

Merci d'avance