{"id":22,"date":"2014-07-18T13:14:05","date_gmt":"2014-07-18T12:14:05","guid":{"rendered":"http:\/\/banfi.co.uk\/blog\/?p=22"},"modified":"2014-07-18T13:14:05","modified_gmt":"2014-07-18T12:14:05","slug":"using-wmi-code-creator-for-projects","status":"publish","type":"post","link":"https:\/\/banfi.co.uk\/blog\/2014\/07\/using-wmi-code-creator-for-projects\/","title":{"rendered":"Using WMI Code Creator for projects"},"content":{"rendered":"<p>WMI Code Creator v1.0 &#8211; <a href=\"http:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=8572\" title=Download\" target=\"_blank\">Download Here<\/a><br \/>\nAllows you to browse the available WMI classes and create query&#8217;s that can be used either through command-line through visual basic script or in your .NET projects.<\/p>\n<p>Once you have your query you can then export the code for Visual C# or Visual Basic.<\/p>\n<p>I used this program to create some code for a quick way of viewing installed printers on a remote print server in Visual C# by populating the results to a datagrid.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n            try\r\n            {\r\n                ConnectionOptions connection = new ConnectionOptions();\r\n                connection.Username = userName.Text;\r\n                connection.Password = password.Text;\r\n                connection.Authority = &quot;ntlmdomain:DOMAINNAME&quot;;\r\n                ManagementScope scope = new ManagementScope(\r\n                    &quot;\\\\\\\\&quot; + serverName.Text + &quot;.FullyQualifiedDomainName\\\\root\\\\CIMV2&quot;, connection);\r\n                scope.Connect();\r\n\r\n                ObjectQuery query = new ObjectQuery(\r\n                    &quot;SELECT * FROM Win32_Printer&quot;); \r\n\r\n                ManagementObjectSearcher searcher =  new ManagementObjectSearcher(scope, query);\r\n\r\n                    DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows&#x5B;0].Clone();\r\n                    row.Cells&#x5B;0].Value = queryObj&#x5B;&quot;DeviceID&quot;];\r\n                    row.Cells&#x5B;1].Value = queryObj&#x5B;&quot;DriverName&quot;];\r\n                    row.Cells&#x5B;2].Value = queryObj&#x5B;&quot;Location&quot;];\r\n                    row.Cells&#x5B;3].Value = queryObj&#x5B;&quot;Name&quot;];\r\n                    row.Cells&#x5B;4].Value = queryObj&#x5B;&quot;Comment&quot;];\r\n                    row.Cells&#x5B;5].Value = queryObj&#x5B;&quot;Portname&quot;];\r\n                    row.Cells&#x5B;6].Value = queryObj&#x5B;&quot;SystemName&quot;];\r\n                    dataGridView1.Rows.Add(row);\r\n                    \r\n                }\r\n            }\r\n            catch (ManagementException e)\r\n            {\r\n                MessageBox.Show(&quot;An error occurred while querying for WMI data: &quot; + e.Message);\r\n            }\r\n\r\n<\/pre>\n<p>Fast and effective to what I needed &#8211; Instead of trying to copy all the printer details out in to excel.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WMI Code Creator v1.0 &#8211;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[9,8],"tags":[],"class_list":["post-22","post","type-post","status-publish","format-standard","hentry","category-visual-c","category-wmi"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/banfi.co.uk\/blog\/wp-json\/wp\/v2\/posts\/22","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/banfi.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/banfi.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/banfi.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/banfi.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=22"}],"version-history":[{"count":3,"href":"https:\/\/banfi.co.uk\/blog\/wp-json\/wp\/v2\/posts\/22\/revisions"}],"predecessor-version":[{"id":25,"href":"https:\/\/banfi.co.uk\/blog\/wp-json\/wp\/v2\/posts\/22\/revisions\/25"}],"wp:attachment":[{"href":"https:\/\/banfi.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/banfi.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/banfi.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}