{"id":2349,"date":"2015-06-24T18:13:54","date_gmt":"2015-06-24T22:13:54","guid":{"rendered":"http:\/\/batchpatch.com\/?p=2349"},"modified":"2019-10-29T22:55:27","modified_gmt":"2019-10-30T02:55:27","slug":"advanced-script-integration-with-batchpatch-part-2","status":"publish","type":"post","link":"https:\/\/batchpatch.com\/advanced-script-integration-with-batchpatch-part-2","title":{"rendered":"Advanced Script Integration with BatchPatch &#8211; Part 2"},"content":{"rendered":"<p>BatchPatch currently provides functionality for retrieving from target computers the list of services that are set to &#8216;automatic&#8217; but currently in the &#8216;stopped&#8217; state.  The reason this is valuable\/convenient for most users is because after you reboot computers you frequently want to have a quick way to determine that all of the services that should be running are, in fact, running.  <\/p>\n<p>If a service is set to &#8216;automatic&#8217; it generally should be running after Windows boots.  However, unfortunately there are actually some cases where a service might be set to &#8216;automatic&#8217; but isn&#8217;t always running.  In these cases we may or may not actually care about the particular service in question.<\/p>\n<p>For example, the following services on my computer are currently set to &#8216;automatic,&#8217; but none of them are actually running at the moment:<\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"text\" style=\"font-family:monospace;\">Microsoft .NET Framework NGEN v4.0.30319_X86\nMicrosoft .NET Framework NGEN v4.0.30319_X64\nGoogle Update Service (gupdate)\nMultimedia Class Scheduler\nSoftware Protection\nSkype Updater<\/pre><\/td><\/tr><\/table><\/div>\n\n<p>We have had some customers request the ability to create an exclusion list for the BatchPatch &#8220;Get stopped automatic services&#8221; action.  The idea here is that you would be able to create a list of services that you don&#8217;t really care about, so that when you execute &#8220;Get stopped automatic services&#8221; it only lists the services that are NOT contained in the exclusion list.  So if a really important &#8216;automatic&#8217; service, like SQL Server, had not started after rebooting a computer, it would be easier to identify it if it weren&#8217;t buried in a list of other services that aren&#8217;t started, like the &#8216;Software Protection&#8217; service, which is usually not started, even though it&#8217;s set to &#8216;automatic.&#8217;  We intend to provide this functionality in a future version of BatchPatch <strong>(EDIT: The feature was added 2015-07-22 and is available under &#8216;Tools > Settings > General > Exclusions list&#8217;)<\/strong>.  However, in the meantime while you are waiting for it, there is actually a very easy way to accomplish the same task in the current version of BatchPatch.<\/p>\n<h3 class=\"brownishOrange\">Sample script:<\/h3>\n<p>Download <a href=\"\/downloads\/get-list-of-stopped-automatic-services\" title=\"StoppedAutoServices.vbs\" target=\"_blank\" rel=\"noopener noreferrer\">StoppedAutoServices.vbs<\/a><\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"line_numbers\"><pre>1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n<\/pre><\/td><td class=\"code\"><pre class=\"vb\" style=\"font-family:monospace;\"><span style=\"color: #008000;\">'Gets the list of services on a computer that are set to automatic and stopped but do not exist in the script's hardcoded list: arrayServiceExclusions.  Cocobolo Software, LLC June 2015\n<\/span>\n<span style=\"color: #008000;\">'usage: cscript.exe StoppedAutoServices.vbs COMPUTERNAME\n<\/span>\n<span style=\"color: #008000;\">'the first argument from the command line is assigned to strComputer\n<\/span>strComputer = WScript.Arguments(0)\n&nbsp;\n<span style=\"color: #008000;\">'create an array containing the list of service display names to exclude from the check for stopped automatic services.\n<\/span>arrayServiceExclusions = Array(<span style=\"color: #800000;\">&quot;Microsoft .NET Framework NGEN v4.0.30319_X86&quot;<\/span>,<span style=\"color: #800000;\">&quot;Microsoft .NET Framework NGEN v4.0.30319_X64&quot;<\/span>,<span style=\"color: #800000;\">&quot;Google Update Service (gupdate)&quot;<\/span>,<span style=\"color: #800000;\">&quot;Multimedia Class Scheduler&quot;<\/span>,<span style=\"color: #800000;\">&quot;Software Protection&quot;<\/span>,<span style=\"color: #800000;\">&quot;Skype Updater&quot;<\/span>)\nstrStoppedAutoServicesList = <span style=\"color: #800000;\">&quot;&quot;<\/span>\nintCounter = 0\n&nbsp;\n<span style=\"color: #151B8D; font-weight: bold;\">on<\/span> <span style=\"color: #151B8D; font-weight: bold;\">error<\/span> <span style=\"color: #151B8D; font-weight: bold;\">resume<\/span> <span style=\"color: #8D38C9; font-weight: bold;\">next<\/span>\nErr.Clear\n&nbsp;\n<span style=\"color: #151B8D; font-weight: bold;\">Set<\/span> objWMIService = <span style=\"color: #E56717; font-weight: bold;\">GetObject<\/span>(<span style=\"color: #800000;\">&quot;winmgmts:\\\\&quot;<\/span> &amp; strComputer &amp; <span style=\"color: #800000;\">&quot;\\root\\cimv2&quot;<\/span>)\n&nbsp;\n<span style=\"color: #008000;\">'Get list of running services\n<\/span><span style=\"color: #151B8D; font-weight: bold;\">Set<\/span> colService = objWMIService.ExecQuery(<span style=\"color: #800000;\">&quot;Select * from Win32_Service&quot;<\/span>)\n\t\t<span style=\"color: #8D38C9; font-weight: bold;\">For<\/span> <span style=\"color: #8D38C9; font-weight: bold;\">Each<\/span> objService <span style=\"color: #8D38C9; font-weight: bold;\">in<\/span> colService\n\t\t\t<span style=\"color: #8D38C9; font-weight: bold;\">If<\/span> ((objService.StartMode = <span style=\"color: #800000;\">&quot;Auto&quot;<\/span>) <span style=\"color: #8D38C9; font-weight: bold;\">And<\/span> (objService.State = <span style=\"color: #800000;\">&quot;Stopped&quot;<\/span>)) <span style=\"color: #8D38C9; font-weight: bold;\">Then<\/span>\n\t\t\t\tboolIsServiceContainedInList = 0\n\t\t\t\t<span style=\"color: #008000;\">'loop through our hardcoded list and compare\n<\/span>\t\t\t\t<span style=\"color: #8D38C9; font-weight: bold;\">For<\/span> <span style=\"color: #8D38C9; font-weight: bold;\">Each<\/span> strServiceName <span style=\"color: #8D38C9; font-weight: bold;\">in<\/span> arrayServiceExclusions\n\t\t\t\t\t<span style=\"color: #8D38C9; font-weight: bold;\">If<\/span> objService.DisplayName = strServiceName <span style=\"color: #8D38C9; font-weight: bold;\">Then<\/span>\n\t\t\t\t\t\tboolIsServiceContainedInList = 1\n\t\t\t\t\t<span style=\"color: #8D38C9; font-weight: bold;\">End<\/span> <span style=\"color: #8D38C9; font-weight: bold;\">If<\/span>\n\t\t\t\t<span style=\"color: #8D38C9; font-weight: bold;\">Next<\/span>\n\t\t\t\t<span style=\"color: #008000;\">'if a service is set to automatic and in the stopped state and NOT contained in our hardcoded list, then add it to our final report and increment the counter\n<\/span>\t\t\t\t<span style=\"color: #8D38C9; font-weight: bold;\">If<\/span> boolIsServiceContainedInList = 0 <span style=\"color: #8D38C9; font-weight: bold;\">Then<\/span>\n\t\t\t\t\tstrStoppedAutoServicesList = strStoppedAutoServicesList &amp; vbLf &amp; objService.DisplayName\n\t\t\t\t\tintCounter = intCounter + 1\n\t\t\t\t<span style=\"color: #8D38C9; font-weight: bold;\">End<\/span> <span style=\"color: #8D38C9; font-weight: bold;\">If<\/span>\n\t\t\t<span style=\"color: #8D38C9; font-weight: bold;\">End<\/span> <span style=\"color: #8D38C9; font-weight: bold;\">If<\/span>\n\t\t<span style=\"color: #8D38C9; font-weight: bold;\">Next<\/span>\n&nbsp;\n<span style=\"color: #008000;\">'write the results list to the console\n<\/span>WScript.Echo strStoppedAutoServicesList\n<span style=\"color: #008000;\">'exit the script with the return value as the number of items in our list\n<\/span>Wscript.Quit(intCounter)<\/pre><\/td><\/tr><\/table><\/div>\n\n<h3 class=\"brownishOrange\">To integrate the StoppedAutoServices.vbs script into BatchPatch:<\/h3>\n<ol>\n<li>Create a &#8216;Local process\/command&#8217; in BatchPatch.  Select <strong><em>Actions > Execute local process\/command > Create\/modify local commands<\/em><\/strong>.<br \/>\n<a href=\"\/wp-content\/uploads\/2015\/04\/2015-04-27-16_15_11-Program-Manager.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2015\/04\/2015-04-27-16_15_11-Program-Manager.png\" alt=\"2015-04-27 16_15_11-Program Manager\" width=\"664\" height=\"739\" class=\"aligncenter size-full wp-image-2274\" srcset=\"https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/04\/2015-04-27-16_15_11-Program-Manager.png 664w, https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/04\/2015-04-27-16_15_11-Program-Manager-270x300.png 270w\" sizes=\"auto, (max-width: 664px) 100vw, 664px\" \/><\/a><\/li>\n<li>Add the StoppedAutoService.vbs script to the grid.<br \/>\n<a href=\"\/wp-content\/uploads\/2015\/06\/2015-06-24-17_41_07-Program-Manager.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2015\/06\/2015-06-24-17_41_07-Program-Manager.png\" alt=\"2015-06-24 17_41_07-Program Manager\" width=\"667\" height=\"459\" class=\"aligncenter size-full wp-image-2353\" srcset=\"https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/06\/2015-06-24-17_41_07-Program-Manager.png 667w, https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/06\/2015-06-24-17_41_07-Program-Manager-300x206.png 300w\" sizes=\"auto, (max-width: 667px) 100vw, 667px\" \/><\/a>  Note, we use <strong><em>$computer<\/em><\/strong> as a parameter in the cscript.exe command.  This tells BatchPatch to send the host name from the row that executes the script.  This is what allows us to execute a script locally on the computer running BatchPatch to retrieve information from a remote computer.\n<\/li>\n<p><\/p>\n<li>Now we&#8217;re ready to execute the script.  For the sake of this example, I have removed all but &#8216;Software Protection&#8217; from the exclusion list hardcoded into the script as arrayServiceExclusions.  The reason for this is to demonstrate what the output looks like when some &#8216;automatic&#8217; services are found in the &#8216;stopped&#8217; state.  However, we will not see &#8216;Software Protection&#8217; appear in our output since it remains in the arrayServiceExclusions.  Highlight the target computers in the grid and then select <strong><em>Actions > Execute local process\/command > Execute saved local commands > Get Stopped Automatic Services<\/em><\/strong><br \/>\n<a href=\"https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/06\/ExecuteLocalCommandGetStoppedAutoServices.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/06\/ExecuteLocalCommandGetStoppedAutoServices.png\" alt=\"ExecuteLocalCommandGetStoppedAutoServices\" width=\"924\" height=\"734\" class=\"aligncenter size-full wp-image-2355\" srcset=\"https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/06\/ExecuteLocalCommandGetStoppedAutoServices.png 924w, https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/06\/ExecuteLocalCommandGetStoppedAutoServices-300x238.png 300w\" sizes=\"auto, (max-width: 924px) 100vw, 924px\" \/><\/a>\n<\/li>\n<li>When the script completes a couple of seconds later, we can see that the &#8216;Exit Code&#8217; value in the &#8216;All Messages&#8217; column is equal to the number of stopped automatic services that were found, excluding (of course) the items hardcoded into the script arrayServiceExclusions, which in this instance was only &#8216;Software Protection.&#8217;  In the screenshot below I have revealed the cell contents for the first row, and we can see the 4 services that were found.  If a machine is clear and no stopped automatic services are found, then the Exit Code will be 0, indicating that we do not need to further examine that computer.<br \/>\n<a href=\"https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/06\/ExecuteLocalCommandGetStoppedAutoServices_Result.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/06\/ExecuteLocalCommandGetStoppedAutoServices_Result.png\" alt=\"ExecuteLocalCommandGetStoppedAutoServices_Result\" width=\"663\" height=\"417\" class=\"aligncenter size-full wp-image-2356\" srcset=\"https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/06\/ExecuteLocalCommandGetStoppedAutoServices_Result.png 663w, https:\/\/batchpatch.com\/wp-content\/uploads\/2015\/06\/ExecuteLocalCommandGetStoppedAutoServices_Result-300x189.png 300w\" sizes=\"auto, (max-width: 663px) 100vw, 663px\" \/><\/a>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>BatchPatch currently provides functionality for retrieving from target computers the list of services that are set to &#8216;automatic&#8217; but currently in the &#8216;stopped&#8217; state. The reason this is valuable\/convenient for most users is because after you reboot computers you frequently want to have a quick way to determine that all of the services that should [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1,17,3],"tags":[32,128],"class_list":["post-2349","post","type-post","status-publish","format-standard","hentry","category-blog","category-general","category-tutorials","tag-automatic-services","tag-remote-script-execution"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/batchpatch.com\/wp-json\/wp\/v2\/posts\/2349","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/batchpatch.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/batchpatch.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/batchpatch.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/batchpatch.com\/wp-json\/wp\/v2\/comments?post=2349"}],"version-history":[{"count":10,"href":"https:\/\/batchpatch.com\/wp-json\/wp\/v2\/posts\/2349\/revisions"}],"predecessor-version":[{"id":4933,"href":"https:\/\/batchpatch.com\/wp-json\/wp\/v2\/posts\/2349\/revisions\/4933"}],"wp:attachment":[{"href":"https:\/\/batchpatch.com\/wp-json\/wp\/v2\/media?parent=2349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/batchpatch.com\/wp-json\/wp\/v2\/categories?post=2349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/batchpatch.com\/wp-json\/wp\/v2\/tags?post=2349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}