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 { Add-Type -TypeDefinition $source Get-ChildItem HKLM:\SYSTEM\CurrentControlSet\Services | foreach { $i = Get-Item -Path $_.PSPath for ($i=0; $i -le $($data.length–1); $i++){ } |
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:\SYSTEM\CurrentControlSet\Services – 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:\SYSTEM\CurrentControlSet\Control\ServiceGroupOrder. 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