{"id":682,"date":"2008-09-29T02:00:55","date_gmt":"2008-09-29T10:00:55","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2008\/09\/29\/managing-services-with-powershell-and-adsi\/"},"modified":"2008-09-29T02:00:55","modified_gmt":"2008-09-29T10:00:55","slug":"managing-services-with-powershell-and-adsi","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2008\/09\/29\/managing-services-with-powershell-and-adsi\/","title":{"rendered":"Managing Services with PowerShell and ADSI"},"content":{"rendered":"<p>In PowerShell v1.0, if you want to manage services on a remote computer, you likely used WMI and the <strong>Get-WMIObject<\/strong> cmdlet. But there is another solution using ADSI. If you use the WinNT provider, you can access all of the services on a remote machine in much the same way you would manage local users and groups. Let me show you.<\/p>\n<p>First, connect to the remote computer and specify the service:<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\">PS C:\\&gt; [ADSI]$svc=&#8221;WinNT:\/\/godot\/spooler,service&#8221;<\/font><\/p>\n<p>The $svc object now represents the spooler service on the computer GODOT. There isn&#8217;t really valid way to specify alternate credentials. However, if you have an existing secure channel to the remote computer ADSI will use it. In this situation GODOT is a stand alone machine. I mapped a drive to the C$ share using GODOT credentials. My ADSI object will use this connection. <\/p>\n<p>Pipe the object to <strong>Select-Object<\/strong> to see all of its properties:<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\">PS C:\\&gt; $svc | select *<br \/><\/font> <\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\">Path&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : {C:\\WINDOWS\\system32\\spoolsv.exe}<br \/>LoadOrderGroup&nbsp;&nbsp;&nbsp;&nbsp; : {SpoolerGroup}<br \/>Dependencies&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : {RPCSS}<br \/>ServiceAccountName : {LocalSystem}<br \/>DisplayName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : {Print Spooler}<br \/>ServiceType&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : {272}<br \/>StartType&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : {2}<br \/>ErrorControl&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : {1}<br \/>Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : {spooler}<br \/>AuthenticationType : Secure<br \/>Children&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : {}<br \/>Guid&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : {C3968E50-4C66-11CF-A995-00AA006BC149}<br \/>ObjectSecurity&nbsp;&nbsp;&nbsp;&nbsp; :<br \/>NativeGuid&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : {C3968E50-4C66-11CF-A995-00AA006BC149}<br \/>NativeObject&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : System.__ComObject<br \/>Parent&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : WinNT:\/\/WORKGROUP\/godot<br \/>Password&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<br \/>Properties&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : {Path, LoadOrderGroup, Dependencies, ServiceAccountName&#8230;}<br \/>SchemaClassName&nbsp;&nbsp;&nbsp; : Service<br \/>SchemaEntry&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : System.DirectoryServices.DirectoryEntry<br \/>UsePropertyCache&nbsp;&nbsp; : True<br \/>Username&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<br \/>Options&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<br \/>Site&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<br \/>Container&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/font>  <\/p>\n<p>You might get some error messages about reading object security and the password but that&#8217;s ok. Unfortunately one thing you don&#8217;t see is the Status property, but its there if you know it exists:<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\">PS C:\\&gt; $svc.status<br \/>4<\/font><\/p>\n<p>A value of 4 indicates the service is running. A value of 1 indicates stopped. You also won&#8217;t see this property if you pipe the object to <strong>Get-Member<\/strong>. In fact you won&#8217;t see much. But there are methods you can use to stop and start the service.<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\">PS C:\\&gt; $svc.stop()<br \/>PS C:\\&gt; $svc.status<br \/>1<br \/>PS C:\\&gt; $svc.start()<br \/>PS C:\\&gt; $svc.status<br \/>4<br \/>PS C:\\&gt;<\/font>  <\/p>\n<p>Pretty easy if you ask me. <br \/>There are a few other values you may want help in decoding. First is the StartType. The integer value can be decoded with a function like this:<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\">function Decode-StartType {<br \/>&nbsp;&nbsp;&nbsp; Param([int]$startType) <\/font> <\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\">&nbsp;&nbsp;&nbsp; Switch ($startType) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp; {&#8220;Boot&#8221;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp; {&#8220;System&#8221;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp;&nbsp;&nbsp; {&#8220;Automatic&#8221;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp;&nbsp; {&#8220;Manual&#8221;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4&nbsp;&nbsp;&nbsp; {&#8220;Disabled&#8221;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; default&nbsp;&nbsp;&nbsp; {&#8220;Unknown&#8221;}<br \/>&nbsp;&nbsp;&nbsp; }<br \/>}<\/font>  <\/p>\n<p>You can do something similar with the ServiceType:<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\">Function Decode-ServiceType {<br \/>&nbsp;&nbsp;&nbsp; Param([int]$ServiceType)<br \/>&nbsp;&nbsp;&nbsp; Switch ($ServiceType ) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp; {&#8220;Kernel-Mode Driver&#8221;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp;&nbsp;&nbsp;&nbsp; {&#8220;File System Driver&#8221;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4&nbsp;&nbsp;&nbsp;&nbsp; {&#8220;Adapter Arguments&#8221;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8&nbsp;&nbsp;&nbsp;&nbsp; {&#8220;File System Driver Service&#8221;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 16&nbsp;&nbsp;&nbsp; {&#8220;Own Process&#8221;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 32&nbsp;&nbsp;&nbsp; {&#8220;Shared Process&#8221;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 272&nbsp;&nbsp; {&#8220;Own Process &#8211; Interactive&#8221;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 288&nbsp;&nbsp; {&#8220;Shared Process &#8211; Interactive&#8221;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; default&nbsp;&nbsp; {&#8220;Unknown&#8221;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>}<\/font>  <\/p>\n<p>Thus you might do something along these lines:  <\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\">$svc | select @{name=&#8221;Service&#8221;;Expression={$_.name.value}},`<br \/>@{name=&#8221;DisplayName&#8221;;Expression={$_.displayname.value}},`<br \/>@{name=&#8221;Status&#8221;;Expression={<br \/>&nbsp;&nbsp;&nbsp; if ($_.status -eq 4) {&#8220;Running&#8221;}<br \/>&nbsp;&nbsp;&nbsp; elseif ($_.status -eq 1) {&#8220;Stopped&#8221;}<br \/>&nbsp;&nbsp;&nbsp; else {&#8220;Unknown&#8221;}<br \/>}},`<br \/>@{name=&#8221;StartType&#8221;;Expression={Decode-StartType $_.startType.value}},`<br \/>@{name=&#8221;ServiceType&#8221;;Expression={Decode-ServiceType $_.servicetype.value}},<br \/>@{name=&#8221;ServiceAccount&#8221;;Expression={$_.serviceaccountname.value }}<\/font>  <\/p>\n<p>When executed I get:<\/p>\n<p><font face=\"consolas\" color=\"#0000ff\">Service&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : spooler<br \/>DisplayName&nbsp;&nbsp;&nbsp; : Print Spooler<br \/>Status&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Running<br \/>StartType&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Automatic<br \/>ServiceType&nbsp;&nbsp;&nbsp; : Own Process &#8211; Interactive<br \/>ServiceAccount : LocalSystem<\/font>  <\/p>\n<p>I&#8217;ll be back tomorrow with more fun on this topic.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In PowerShell v1.0, if you want to manage services on a remote computer, you likely used WMI and the Get-WMIObject cmdlet. But there is another solution using ADSI. If you use the WinNT provider, you can access all of the services on a remote machine in much the same way you would manage local users and groups. Let me show you.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[283,25],"tags":[280,28,57,301,300],"class_list":["post-682","post","type-post","status-publish","format-standard","hentry","category-howto","category-windows-powershell","tag-adsi","tag-powershell","tag-scripting","tag-select-object","tag-service"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/682","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=682"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/682\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}