IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

WMI & remote computer


Sujet :

C#

  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Décembre 2008
    Messages
    28
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2008
    Messages : 28
    Par défaut WMI & remote computer
    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.

  2. #2
    Rédacteur
    Avatar de Paul Musso
    Profil pro
    Inscrit en
    Août 2008
    Messages
    368
    Détails du profil
    Informations personnelles :
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations forums :
    Inscription : Août 2008
    Messages : 368
    Par défaut
    Bonjour,

    Peut-tu me dire qu'elle instruction génère l'exception et qu'elle est l'erreur.
    Cela peut venir en effet d'un problème de port. WMI utilise le protocole RPC avec les ports TCP suivants : 135,139,445,593. Pour éliminer cette éventualité, il vaut mieux désactiver le firewall des 2 machines.

    Il faut aussi s'assurer que le service COM+/DCOM est bien activé sur les 2 machines.

    Finalement, le compte utilisé pour lancer l'appli cliente doit être administrateur de la machine distante.

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Décembre 2008
    Messages
    28
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2008
    Messages : 28
    Par défaut
    Bonjour,

    Merci pour la réponse.
    L'antivirus de Windows et désactivé. Nod32 filtre le trafic mais normalement les portes sont ouvertes au sein du réseau local.

    L'erreur est tout simplement un accès refusé : Connection error (user name or password might be incorrect): Accès refusé. (Exceptionde HRESULT : 0x80070005 (E_ACCESSDENIED))

    Je vérifie les informations sur les ordinateurs :
    - Services COM et RPC Locator activés.
    - Firewall : Zone fiable au sein du réseau local (j'ai aussi essayé avec les firewall désactivé cela ne fonctionne pas plus).
    - Compte du pc distant : Nom d'utilisateur : Administrateur , Mot de passe : motdepasseY
    - Compte du pc source : Nom d'utilisateur : Utilisateur , Mot de passe : motdepasseX

    Dois-je toucher au 'gestion des mot de passe réseau' dans le compte utilisateur Administrateur du pc distant ?

    Concernant le code pour l'authentification, es-ce bon ? (Sachant que c'est l'adresse IP de l'ordinateur distant, et le nom du réseau local avec le nom d'utilisateur Administrateur ?)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    connection.Authority = "ntlmdomain:MSHOME\\Administrateur";
     
                    ManagementScope scope = new ManagementScope(
                        "\\\\192.168.1.21\\root\\CIMV2", connection);
                    scope.Connect();
    Merci.

  4. #4
    Rédacteur
    Avatar de Paul Musso
    Profil pro
    Inscrit en
    Août 2008
    Messages
    368
    Détails du profil
    Informations personnelles :
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations forums :
    Inscription : Août 2008
    Messages : 368
    Par défaut
    Ok ça devient plus clair,

    Tu as 2 choix pour t'authentifier, soit tu utilises l'impersonation, soit tu spécifie un compte comme tu l'as fait.

    L'impersonation prend le compte utilisé pour exécuter ton client pour s'authentifier sur le poste distant. Cela sous-entend que le compte du client doit être admin du poste distant. Voici un ptit bout de code pour faire cela :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
                ConnectionOptions options = new ConnectionOptions();
                options.Authentication = AuthenticationLevel.Packet;
                options.Impersonation = true;
    Après tu utilises options comme argument pour le constructeur de ManagementScope.

    Sinon, j'ai vu qu'il y avait des propriétés UserName et Password, peut-être faut-il utiliser cela au lieu de Authority pour spécifier le compte. D'ailleurs dans ton exemple, je ne vois nul par le password ... cela vient peut être de là.

  5. #5
    Membre averti
    Profil pro
    Inscrit en
    Décembre 2008
    Messages
    28
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2008
    Messages : 28
    Par défaut
    Je vais peut-être paraitre un peu bête mais je ne comprends pas trop comment le compte client peut-il être admin tu compte distant sans utiliser le compte administrateur du compte distant ^^'

    Concernant les propriétés 'username' et 'password' elle permette de créer la fenêtre de dialogue d'authentification (pour rentrer les paramètres correspondant) ou concernant les erreurs.
    Mais peut-être en essayant de rentrer les paramètres directement dans le code, oui c'est pas bête.

    Il semble que ceci devrait convenir (je test sa un peu plus tard, d'ailleur sa reprend l'inpersonation) :

    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
    public static ConnectionOptions ProcessConnectionOptions()
            {
                ConnectionOptions options = new ConnectionOptions();
                options.Impersonation = ImpersonationLevel.Impersonate;
                options.Authentication = AuthenticationLevel.Default;
                options.EnablePrivileges = true;
                return options;
            }
     
     public static ManagementScope ConnectionScope(  string machineName,
                                                            string Username,
                                                            string Password,
                                                            string Domain,
                                                            ConnectionOptions options)
            {
                ManagementScope connectScope = new ManagementScope();
                connectScope.Path = new ManagementPath(@"\\" + machineName + @"\root\CIMV2");
                if (Domain != null || Username != null)
                {
                    options.Username = Domain + "\\" + Username;
                    options.Password = Password;
                }
                connectScope.Options = options;
     
                try
                {
                    connectScope.Connect();
                }
                catch (ManagementException e)
                {
                    //Console.WriteLine("An Error Occurred: " + e.Message.ToString());
                }
                catch
                {
                }
                return connectScope;
            }
        }
    Merci.

  6. #6
    Rédacteur
    Avatar de Paul Musso
    Profil pro
    Inscrit en
    Août 2008
    Messages
    368
    Détails du profil
    Informations personnelles :
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations forums :
    Inscription : Août 2008
    Messages : 368
    Par défaut
    Ué carrément ça me semble bien aussi.

    Fait bien attention à utiliser un compte qui est administrateur de la machine distante.

    En ce qui concerne l'histoire de droits, l'impersonation sert à changer le compte utilisé pour lancer l'application cliente. Tu trouveras plus d'info ici et .

Discussions similaires

  1. Réponses: 3
    Dernier message: 12/05/2011, 14h41
  2. Utilisations de flash remoting et com servers
    Par bolo dans le forum Flash
    Réponses: 8
    Dernier message: 11/07/2006, 10h04

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo