{"id":2592,"date":"2022-08-30T15:26:09","date_gmt":"2022-08-30T15:26:09","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/04\/wmi-to-reboot-remote-machine-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:26:09","modified_gmt":"2022-08-30T15:26:09","slug":"wmi-to-reboot-remote-machine-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/wmi-to-reboot-remote-machine-collection-of-common-programming-errors\/","title":{"rendered":"WMI to reboot remote machine-Collection of common programming errors"},"content":{"rendered":"<p>To address WMI queries to a remote computer, you simply specify that computer&#8217;s name (or IP address) in the <code>ManagementScope<\/code> object.<\/p>\n<p>I&#8217;m not well up in C#, but here&#8217;s an example I came up with using MSDN and WMI Code Creator (which is, by the way, an excellent tool for generating WMI code, and supports C# among others). Hope this code will give you the idea.<\/p>\n<p><em>(<strong>Disclaimer:<\/strong> This code is untested.)<\/em><\/p>\n<pre><code>using System;\nusing System.Management;\n...\n\nvoid Shutdown()\n{\n    try\n    {\n        const string computerName = \"COMPUTER\"; \/\/ computer name or IP address\n\n        ConnectionOptions options = new ConnectionOptions();\n        options.EnablePrivileges = true;\n        \/\/ To connect to the remote computer using a different account, specify these values:\n        \/\/ options.Username = \"USERNAME\";\n        \/\/ options.Password = \"PASSWORD\";\n        \/\/ options.Authority = \"ntlmdomain:DOMAIN\";\n\n        ManagementScope scope = new ManagementScope(\n          \"\\\\\\\\\" + computerName +  \"\\\\root\\\\CIMV2\", options);\n        scope.Connect();\n\n        SelectQuery query = new SelectQuery(\"Win32_OperatingSystem\");\n        ManagementObjectSearcher searcher = \n            new ManagementObjectSearcher(scope, query);\n\n        foreach (ManagementObject os in searcher.Get())\n        {\n            \/\/ Obtain in-parameters for the method\n            ManagementBaseObject inParams = \n                os.GetMethodParameters(\"Win32Shutdown\");\n\n            \/\/ Add the input parameters.\n            inParams[\"Flags\"] =  2;\n\n            \/\/ Execute the method and obtain the return values.\n            ManagementBaseObject outParams = \n                os.InvokeMethod(\"Win32Shutdown\", inParams, null);\n        }\n    }\n    catch(ManagementException err)\n    {\n        MessageBox.Show(\"An error occurred while trying to execute the WMI method: \" + err.Message);\n    }\n    catch(System.UnauthorizedAccessException unauthorizedErr)\n    {\n        MessageBox.Show(\"Connection error (user name or password might be incorrect): \" + unauthorizedErr.Message);\n    }\n}\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2014-02-04 02:37:01. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>To address WMI queries to a remote computer, you simply specify that computer&#8217;s name (or IP address) in the ManagementScope object. I&#8217;m not well up in C#, but here&#8217;s an example I came up with using MSDN and WMI Code Creator (which is, by the way, an excellent tool for generating WMI code, and supports [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2592","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2592"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2592\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}