Quelqu'un a-t-il un exemple de code C# sur la façon dont les objets de la classe Win32_ComputerSystem WMI peuvent être récupérés à partir d'un système distant en utilisant nom d'hôte, nom d'utilisateur et mot de passe?Demander Win32_ComputerSystem class remote
1
A
Répondre
1
Connexion:
try
{
rcOptions = new ConnectionOptions();
rcOptions.Authentication = AuthenticationLevel.Packet;
rcOptions.Impersonation = ImpersonationLevel.Impersonate;
rcOptions.EnablePrivileges = true;
rcOptions.Username = servername + @"\" + username;
rcOptions.Password = password;
mScope = new ManagementScope(String.Format(@"\\{0}\root\cimv2", servername), rcOptions);
mScope.Connect();
if (mScope.IsConnected == true) { MessageBox.Show("Connection Succeeded", "Alert"); } else { MessageBox.Show("Connection Failed", "Alert"); }
if (mScope.IsConnected == true) { lblConnectionStateWarning.Text = "Connected"; } else { lblConnectionStateWarning.Text = "Disconnected"; } //I have a label that displays connectionstate, you can leave that out
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
Obtenir requête avec la méthode, le chargement dans dictonary & retour dans listview
private void FindWMI(string servername, string classSelection, ConnectionOptions rcOptions, ListView listView)
{
try
{
var dct = new Dictionary<string, string>();
List<ListViewItem> itemsList = new List<ListViewItem>();
oQuery = new ObjectQuery("select * from " + classSelection);
moSearcher = new ManagementObjectSearcher(mScope, oQuery);
moCollection = moSearcher.Get();
Invoke(new MethodInvoker(() =>
{
listView.Items.Clear();
}));
foreach (ManagementObject mObject in moCollection)
{
if (mObject != null)
{
foreach (PropertyData propData in mObject.Properties)
{
if (propData.Value != null && propData.Value.ToString() != "" && propData.Name != null && propData.Name != "")
dct[propData.Name] = propData.Value.ToString();
//Don't forget this, when the result is an array, you want all the strings in that array..
if (propData.Value is Array) { dct[propData.Name] = ""; foreach (string stringArray in (string[])propData.Value) { dct[propData.Name] += stringArray + "\n"; } }
}
}
}
foreach (KeyValuePair<string, string> listItem in dct)
{
ListViewItem lstItem = new ListViewItem(listItem.Key);
lstItem.SubItems.Add(listItem.Value);
itemsList.Add(lstItem);
}
Invoke(new MethodInvoker(() =>
{
listView.Items.AddRange(itemsList.ToArray());
}));
}
catch (Exception) { }
}
+0
@TWT Si vous utilisez cet exemple, la valeur de la propriété renvoie parfois un tableau, vous devez donc ajouter: if (propData.Value est Array) {dct [propData.Name] = ""; foreach (string stringArray dans (chaîne []) propData.Value) {dct [propData.Name] + = stringArray + "\ n"; }} –
Questions connexes
- 1. form_for [@ parent, @ son] ,: remote => true ne pas demander JS
- 2. Vous ne pouvez pas obtenir Win32_ComputerSystem - OEMStringArray dans C#
- 3. Remote Locking
- 4. Syntaxe pour link_to avec bloc dans rails3 avec: remote => true et incluant: class et: id
- 5. C# remote events
- 6. Attribut MVC3 Remote, comment implémenter?
- 7. application Remote Desktop
- 8. CruiseControl Remote Dashboard Access
- 9. Compact SQL Remote Access
- 10. REmote LIft Acteurs
- 11. git push remote repo
- 12. capistrano remote requirements
- 13. Remote Debugging dans Eclipse
- 14. Remote Database Managemnet
- 15. Remote Repository Connection
- 16. Jquery remote validation timeout
- 17. Problèmes d'IO Remote iPhone
- 18. Ruby Remote HTTP Post
- 19. OPENXML Remote Scan Performance
- 20. mysql remote management
- 21. Eclipse Remote Search Annulé
- 22. Cordova IOS Remote Build
- 23. Remote EJB sur Wildfly
- 24. mstsc Remote Desktop questions
- 25. SVN Forced remote commit
- 26. remote form_tag rails3
- 27. Android Remote Services
- 28. JQTouch load remote html
- 29. contournement pour l'attribut "Remote"
- 30. EXTJS Remote Combobox
Il suffit de regarder le code d'exemple dans l'article MSDN Library pour la classe ManagementScope. http://msdn.microsoft.com/en-us/library/w7sx1w4f.aspx –