Service loading

This isn’t quite finished but there is enough useful stuff to share.  I was wondering what order things are loaded – especially services and found that this information can be found in the registry – only difficulty is reading it.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
$data = @()
## create class for object
$source=@"
public class StartOrder
{
    private string _servicename;
    private string _displayname;
    private int _tag;
    private string _group;
    private int _grouporder;
   
     public string DisplayName {
        get {return _displayname;}
         set {_displayname = value;}
    }
   
    public string ServiceName {
        get {return _servicename;}
         set {_servicename = value;}
    }
   
    public string Group {
        get {return _group;}
         set {_group = value;}
    }
   
    public int Tag {
        get {return _tag;}
         set {_tag = value;}
    }

    public int GroupOrder {
        get {return _grouporder;}
         set {_grouporder = value;}
    }
}
"@
Add-Type -TypeDefinition $source

Get-ChildItem HKLM:SYSTEMCurrentControlSetServices | foreach {
    $so = New-Object -TypeName StartOrder
    $so.ServiceName = $_.PSChildName

    $i = Get-Item -Path $_.PSPath
    if ($i.Property -contains "DisplayName"){
        $dn = Get-ItemProperty -Path $_.PSPath -Name DisplayName
        $so.displayname = $dn.displayname
    }
    if ($i.Property -contains "Group"){
        $gp = Get-ItemProperty -Path $_.PSPath -Name Group
        $so.group = $gp.Group
    }
    if ($i.Property -contains "Tag"){
        $tag = Get-ItemProperty -Path $_.PSPath -Name Tag
        $so.tag = $tag.Tag
    }
    $data += $so
}
## now we set the group order
$gpo = @{}
$groups = (Get-ItemProperty -Path HKLM:SYSTEMCurrentControlSetControlServiceGroupOrder -Name List).List
for ($i=0; $i -le $($groups.length1); $i++){$gpo += @{$($groups[$i]) = $i}}

for ($i=0; $i -le $($data.length1); $i++){
    if ($data[$i].Group -eq $null){$data[$i].GroupOrder = 255}
    else {$data[$i].GroupOrder = $gpo[$($data[$i].Group)]} 
   
    ## reset for service groups not is start list
    if ($data[$i].GroupOrder -eq 0){$data[$i].GroupOrder = 250}
   
    ## tidy display names
   
    if (($data[$i].DisplayName -eq $null) -or ($data[$i].DisplayName.StartsWith("@"))){
        $data[$i].DisplayName = (Get-Service $($data[$i].ServiceName) -ErrorAction SilentlyContinue ).DisplayName
    }

}
$data | sort grouporder, group, tag |  Out-GridView -Title "Service Start Order"
#$data | sort grouporder, tag | select -Propert DisplayName, ServiceName, Group, Tag -ExcludeProperty grouporder | Out-GridView -Title "Service Start Order"

 

We’ll start by creating an empty array and a new object class.  Basically all we are doing is defining a set of properties on the object.  Add-Type is then used to create the class.

We can read the services out of the registry at HKLM:SYSTEMCurrentControlSetServices – note that this includes drivers as well as services. We can populate the properties of our object as we loop through the services. Name and DisplayName should be self explanatory. Group  indicates which group the service loads in – services in groups load before those that aren’t in groups. The tag indicates the relative load order within the group. Not all services have a group or tag which is why I test for them.

The list of groups in load order can found at HKLM:SYSTEMCurrentControlSetControlServiceGroupOrder.  Loop through them and create a hash table with the name and load order. We loop through the services setting the group order property – this is a fudge to give me something to sort on – may be a better way but still working on it.  If there isn’t a group we get an order of 255 (arbitrary) to put it at the end.  If a group isn’t in the list set to 250 otherwise pick the load order from the hash table.

The DisplayName is always what we are used to so take the opportunity to reset with get-service.  The  -ErrorAction SilentlyContinue masks any error messages.

Finally sort the data and use out-gridview to display.

I want to add a few more properties but this is already quite useful. It will end up as another function in my services module

Technorati Tags: ,

This entry was posted in PowerShell V2. Bookmark the permalink.

Leave a comment