{"id":55133,"date":"2008-08-29T02:18:00","date_gmt":"2008-08-29T02:18:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/08\/29\/hey-scripting-guy-whats-going-on-in-the-service-host-process\/"},"modified":"2008-08-29T02:18:00","modified_gmt":"2008-08-29T02:18:00","slug":"hey-scripting-guy-whats-going-on-in-the-service-host-process","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-whats-going-on-in-the-service-host-process\/","title":{"rendered":"Hey, Scripting Guy! What&#8217;s Going on in the Service Host Process?"},"content":{"rendered":"<p><span class=\"Apple-style-span\"><\/p>\n<h2><font class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><span class=\"Apple-style-span\"><font class=\"Apple-style-span\" face=\"Arial\" size=\"6\"><span class=\"Apple-style-span\"><\/span><\/font><\/span><\/font><\/h2>\n<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/> <\/p>\n<p>Hey, Scripting Guy! I think I might have a problem on my server. When I go into Task Manager, all the processes that are eating up the most CPU time and the most memory are the stupid svchost.exe process. I know this is the Service Host process and that it is being used by multiple services, but how can I know what is really going on in there?<\/p>\n<p>&#8211; JH<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\" \/><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><\/a> <\/p>\n<p>Hi JH,<\/p>\n<p>Scripting Guy at your service. Or is it at your Service Host? Yes, I know, I&#8217;m eating your CPU time with lame comedy. Let&#8217;s go.<\/p>\n<p>To look into the Service Host process, what we need to do is first find all the processes with the name of<b>svchost.exe<\/b>. Next we need to find the handle of each one so we can uniquely identify it. Next we need to find all the services that are using a particular process ID, match them up with the handles we had earlier, and we have your answer. Here is the&nbsp;<b>GetServicesInSvchost.ps1<\/b>&nbsp;script: <\/p>\n<pre class=\"codeSample\">$aryPid = @(Get-WmiObject win32_process -Filter \"name='svchost.exe'\") | \n  Foreach-Object { $_.Handle } \n\n\"There are \" + $arypid.length + \" instances of svchost.exe running\"\n\nforeach ($i in $aryPID) \n{ \n Write-Host \"Services running in ProcessID: $i\" ; \n Get-WmiObject win32_service -Filter \" processID = $i\" | \n Format-Table name, state, startMode \n}\n<\/pre>\n<p>Our script begins by using WMI to find all the processes that are named&nbsp;<b>svchost.exe<\/b>. To do this, we use the&nbsp;<b>Get-WmiObject<\/b>&nbsp;cmdlet. For each of these processes we find, we obtain the handle and assign the resulting array of handles to a variable aptly named&nbsp;<b>$aryPid<\/b>. This line of code is seen&nbsp;here: <\/p>\n<pre class=\"codeSample\">$aryPid = @(Get-WmiObject win32_process -Filter \"name='svchost.exe'\") | \n  foreach-object { $_.Handle }\n<\/pre>\n<p>Once we have an array of process IDs for all the&nbsp;<b>svchost<\/b>&nbsp;processes, we use the&nbsp;<b>length<\/b>&nbsp;property to count how many we actually have running. This is not important for the actual script; however, it is very important for our sanity. It is also a nice reality check to assure us the script is working properly. For example if the&nbsp;<b>$ary.length<\/b>&nbsp;is equal to 1, but you see the following in Task Manager, you can be assured you have a problem:<\/p>\n<p><img decoding=\"async\" height=\"350\" alt=\"Svchost in Task Manager image\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/svchostintaskmanager.jpg\" width=\"497\" border=\"0\" \/> <\/p>\n<p>&nbsp;<\/p>\n<p>Now we want to walk through the array contained in the&nbsp;<b>$aryPid<\/b>&nbsp;variable. To do this, we will use the&nbsp;<b>foreach<\/b>&nbsp;statement. We need to use a variable to keep track of things, we choose the&nbsp;<b>$i<\/b>&nbsp;variable. This line of code is seen here:<\/p>\n<pre class=\"codeSample\">foreach ($i in $aryPID) <\/pre>\n<p>To produce a header for our report, we use the&nbsp;<b>Write-Host<\/b>&nbsp;cmdlet to print out the&nbsp;<b>processID<\/b>&nbsp;of the current process we are working with, which is represented by the variable&nbsp;<b>$i<\/b>. We end the line with a semicolon as we want to continue the command. This is seen here:<\/p>\n<pre class=\"codeSample\">Write-Host \"Services running in ProcessID: $i\" ;<\/pre>\n<p>We now need to do another WMI query. This time, we will query from the&nbsp;<b>Win32_Service<\/b>&nbsp;WMI class. We use a filter to limit our query to services that have a particular ProcessID value. We are interested in finding all services that have a ProcessID that is the same as one of our running versions of the&nbsp;<b>Svchost.exe<\/b>&nbsp;processes. When we find a service with a ProcessID that matches a&nbsp;<b>Svchost.exe<\/b>&nbsp;process, we have our answer. We decide to organize our data as a table and print out the&nbsp;<b>name<\/b>,&nbsp;<b>state<\/b>, and&nbsp;<b>startmode<\/b>&nbsp;of the service. This line of code is seen&nbsp;here: <\/p>\n<pre class=\"codeSample\"> Get-WmiObject win32_service -Filter \" processID = $i\" | \n Format-Table name, state, startMode \n<\/pre>\n<p>When we run the script, we obtain a report that is similar to the one seen here:<\/p>\n<p><img decoding=\"async\" height=\"350\" alt=\"Svchost report image\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/svchostreport.jpg\" width=\"561\" border=\"0\" \/> <\/p>\n<p>&nbsp;<\/p>\n<p>In the end, JH, it did not take super scripting powers after all. It was a rather tame script, and leveraging the ease in which Windows PowerShell does WMI, it was actually pretty short as well. To be perfectly candid, we could have written the script in a single line as seen&nbsp;here: <\/p>\n<pre class=\"codeSample\">get-wmiObject win32_process -filter \"name='svchost.exe'\" | \nforeach { gwmi -query \"Select * from win32_service where processID = $($_.handle)\" } | \nsort processID, Name | \nformat-table processID, name, state, startmode -AutoSize\n<\/pre>\n<p>But it would have been a really long line. Hope this script gives you some x-ray eyes and allows you to remove some of the mystery surrounding the&nbsp;<b>svchost<\/b>&nbsp;process.<\/p>\n<p><font class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><span class=\"Apple-style-span\"><b><b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b><\/b><\/span><\/font><\/p>\n<p><font class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><b><\/b><\/font><\/span><font class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I think I might have a problem on my server. When I go into Task Manager, all the processes that are eating up the most CPU time and the most memory are the stupid svchost.exe process. I know this is the Service Host process and that it is being used by multiple [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[31,87,3,39,45],"class_list":["post-55133","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-processes","tag-scripting-guy","tag-services","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I think I might have a problem on my server. When I go into Task Manager, all the processes that are eating up the most CPU time and the most memory are the stupid svchost.exe process. I know this is the Service Host process and that it is being used by multiple [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=55133"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55133\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=55133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}