Bonjour,

Je pense que ma question est peut-être plutôt un problème de compréhension que de programmation mais le sujet se rapportant au C# cela doit être la catégorie la plus approprié.

Mon problème concerne le WMI. J'arrive parfaitement à l'exécuter en local mais à distance c'est autre chose.

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Management;
 
namespace WMISample
{
    public class MyQuerySample : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label userNameLabel;
        private System.Windows.Forms.TextBox userNameBox;
        private System.Windows.Forms.TextBox passwordBox;
        private System.Windows.Forms.Label passwordLabel;
        private System.Windows.Forms.Button OKButton;
        private System.Windows.Forms.Button cancelButton;
 
        private System.ComponentModel.Container components = null;
 
        public MyQuerySample()
        {
            InitializeComponent();
        }
 
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
 
        private void InitializeComponent()
        {
            this.userNameLabel = new System.Windows.Forms.Label();
            this.userNameBox = new System.Windows.Forms.TextBox();
            this.passwordBox = new System.Windows.Forms.TextBox();
            this.passwordLabel = new System.Windows.Forms.Label();
            this.OKButton = new System.Windows.Forms.Button();
            this.cancelButton = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // userNameLabel
            // 
            this.userNameLabel.Location = new System.Drawing.Point(16, 8);
            this.userNameLabel.Name = "userNameLabel";
            this.userNameLabel.Size = new System.Drawing.Size(160, 32);
            this.userNameLabel.TabIndex = 0;
            this.userNameLabel.Text = "Enter the user name for the remote computer:";
            // 
            // userNameBox
            // 
            this.userNameBox.Location = new System.Drawing.Point(160, 16);
            this.userNameBox.Name = "userNameBox";
            this.userNameBox.Size = new System.Drawing.Size(192, 20);
            this.userNameBox.TabIndex = 1;
            this.userNameBox.Text = "";
            // 
            // passwordBox
            // 
            this.passwordBox.Location = new System.Drawing.Point(160, 48);
            this.passwordBox.Name = "passwordBox";
            this.passwordBox.PasswordChar = '*';
            this.passwordBox.Size = new System.Drawing.Size(192, 20);
            this.passwordBox.TabIndex = 3;
            this.passwordBox.Text = "";
            // 
            // passwordLabel
            // 
            this.passwordLabel.Location = new System.Drawing.Point(16, 48);
            this.passwordLabel.Name = "passwordLabel";
            this.passwordLabel.Size = new System.Drawing.Size(160, 32);
            this.passwordLabel.TabIndex = 2;
            this.passwordLabel.Text = "Enter the password for the remote computer:";
            // 
            // OKButton
            // 
            this.OKButton.Location = new System.Drawing.Point(40, 88);
            this.OKButton.Name = "OKButton";
            this.OKButton.Size = new System.Drawing.Size(128, 23);
            this.OKButton.TabIndex = 4;
            this.OKButton.Text = "OK";
            this.OKButton.Click += new System.EventHandler(this.OKButton_Click);
            // 
            // cancelButton
            // 
            this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.cancelButton.Location = new System.Drawing.Point(200, 88);
            this.cancelButton.Name = "cancelButton";
            this.cancelButton.Size = new System.Drawing.Size(128, 23);
            this.cancelButton.TabIndex = 5;
            this.cancelButton.Text = "Cancel";
            this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click);
            // 
            // MyQuerySample
            // 
            this.AcceptButton = this.OKButton;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.CancelButton = this.cancelButton;
            this.ClientSize = new System.Drawing.Size(368, 130);
            this.ControlBox = false;
            this.Controls.Add(this.cancelButton);
            this.Controls.Add(this.OKButton);
            this.Controls.Add(this.passwordBox);
            this.Controls.Add(this.passwordLabel);
            this.Controls.Add(this.userNameBox);
            this.Controls.Add(this.userNameLabel);
            this.Name = "MyQuerySample";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Remote Connection";
            this.ResumeLayout(false);
 
        }
 
        [STAThread]
        static void Main() 
        {
            Application.Run(new MyQuerySample());
        }
 
        private void OKButton_Click(object sender, System.EventArgs e)
        {
            try
            {
                ConnectionOptions connection = new ConnectionOptions();
                connection.Username = userNameBox.Text;
                connection.Password = passwordBox.Text;
                connection.Authority = "ntlmdomain:MSHOME\\NomUtilisateur";
 
                ManagementScope scope = new ManagementScope(
                    "\\\\192.168.1.21\\root\\CIMV2", connection);
                scope.Connect();
 
                ObjectQuery query= new ObjectQuery(
                    "SELECT * FROM Win32_BIOS"); 
 
                ManagementObjectSearcher searcher = 
                    new ManagementObjectSearcher(scope, query);
 
                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Win32_BIOS instance");
                    Console.WriteLine("-----------------------------------");
 
                    if(queryObj["BIOSVersion"] == null)
                        Console.WriteLine("BIOSVersion: {0}", queryObj["BIOSVersion"]);
                    else
                    {
                        String[] arrBIOSVersion = (String[])(queryObj["BIOSVersion"]);
                        foreach (String arrValue in arrBIOSVersion)
                        {
                            Console.WriteLine("BIOSVersion: {0}", arrValue);
                        }
                    }
                }
                Close();
            }
            catch(ManagementException err)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + err.Message);
            }
            catch(System.UnauthorizedAccessException unauthorizedErr)
            {
                MessageBox.Show("Connection error (user name or password might be incorrect): " + unauthorizedErr.Message);
            }
        }
 
        private void cancelButton_Click(object sender, System.EventArgs e)
        {
            Close();
        }
    }
}
Je pense que le problème viens plutôt de la connexion (le code étant bon en local).

Ce que je ne comprend c'est les paramètres de configuration à rentrer pour établir la connexion.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
 
connection.Authority = "ntlmdomain:MSHOME\\NomUtilisateur";
 
                ManagementScope scope = new ManagementScope(
                    "\\\\192.168.1.21\\root\\CIMV2", connection);
                scope.Connect();
Je souhaite récupérer des informations d'ordinateur qui sont sur le même réseau local (MSHOME).

Chaque ordinateur possède un nom d'utilisateur (administrateur + mot de passe), dois-je configurer autre chose sur ces ordinateurs (firewall?) ?

Merci beaucoup.

Innop.