Bonjour à tous,

Voici mon problème :

J'aimerais écrire une petite application capable d'exécuter une cmdlet Powershell, voir un script PowerShell sur une machine distante !

J'ai écris ceci en me basant sur quelques articles du net :

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
            SecureString pwd = new SecureString();
            foreach (char c in "mypassword!".ToCharArray())
	        {
	            pwd.AppendChar(c);
	        }
            PSCredential cred = new PSCredential("MyUserName", pwd);
 
            // Create a WSManConnectionInfo object using the default constructor to 
            // connect to the "localhost". The WSManConnectionInfo object can also 
            // specify connections to remote computers.
 
            WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri(string.Format("http://{0}:5985/wsman", textBox2.Text)), "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", cred);
 
            connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Negotiate;
 
 
            // Create a remote runspace pool that uses the WSManConnectionInfo object.  
            // The minimum runspaces value of 1 specifies that Windows PowerShell will 
            // keep at least 1 runspace open. The maximum runspaces value of 2 specifies
            // that Windows PowerShell will keep a maximum of 2 runspaces open at the 
            // same time so that two commands can be run concurrently.
            using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo)) 
            {
                // Call the Open() method to open a runspace from the pool and establish 
                // the connection. 
                remoteRunspace.Open();
 
                // Call the Create() method to create a pipeline, call the AddCommand(string) 
                // method to add the "get-process" command, and then call the BeginInvoke() 
                // method to run the command asynchronously using a runspace of the pool.
                PowerShell gpsCommand = PowerShell.Create().AddCommand("get-process");
                gpsCommand.Runspace = remoteRunspace;
                IAsyncResult gpsCommandAsyncResult = gpsCommand.BeginInvoke();
                ////<SnippetRemoteRunspacePool01CreatePowerShell01/>
 
                // The previous call does not block the current thread because it is 
                // running asynchronously. Because the remote runspace pool can open two 
                // runspaces, the second command can be run.
                PowerShell getServiceCommand = PowerShell.Create().AddCommand("get-service");
                getServiceCommand.Runspace = remoteRunspace;
                IAsyncResult getServiceCommandAsyncResult = getServiceCommand.BeginInvoke();
 
                // When you are ready to handle the output, wait for the command to complete 
                // before extracting results. A call to the EndInvoke() method will block and return 
                // the output.
                PSDataCollection<PSObject> gpsCommandOutput = gpsCommand.EndInvoke(gpsCommandAsyncResult);
 
                // Process the output as needed.
                if ((gpsCommandOutput != null) && (gpsCommandOutput.Count > 0))
                {
                    Console.WriteLine("The first output from running get-process command: ");
                    Console.WriteLine(
                                      "Process Name: {0} Process Id: {1}",
                                      gpsCommandOutput[0].Properties["ProcessName"].Value,
                                      gpsCommandOutput[0].Properties["Id"].Value);
                    Console.WriteLine();
                }
 
                // Now process the output from second command. As discussed previously, wait 
                // for the command to complete before extracting the results.
                PSDataCollection<PSObject> getServiceCommandOutput = getServiceCommand.EndInvoke(
                                getServiceCommandAsyncResult);
 
                // Process the output of the second command as needed.
                if ((getServiceCommandOutput != null) && (getServiceCommandOutput.Count > 0))
                {
                    Console.WriteLine("The first output from running get-service command: ");
                    Console.WriteLine(
                                      "Service Name: {0} Description: {1} State: {2}",
                                      getServiceCommandOutput[0].Properties["ServiceName"].Value,
                                      getServiceCommandOutput[0].Properties["DisplayName"].Value,
                                      getServiceCommandOutput[0].Properties["Status"].Value);
                }
 
                // Once done with running all the commands, close the remote runspace pool.
                // The Dispose() (called by using primitive) will call Close(), if it
                // is not already called.
                remoteRunspace.Close();
            }
        }
Je reçois le message d'erreur suivant lorsqu'il tente d'exécuter remoteRunspace.Open() :
Connecting to remote server failed with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.
Quelqu'un a une idée ?

Merci