-
Notifications
You must be signed in to change notification settings - Fork 95
/
Get-Powercfg.ps1
124 lines (114 loc) · 4.19 KB
/
Get-Powercfg.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# https://www.reddit.com/r/PowerShell/comments/6lo8b5/config_settings_check/
function Get-Powercfg {
$schemes = powercfg /l | ? {$_ -match ':'} | % {
$split = $_ -split ':|\s{2}' | % {$_.trim()}
$guid = $split[1]
$name = $split[2]
$active = $name.endswith('*')
if ($active) {
$name = $name.substring(1, $name.length - 4)
} else {
$name = $name.substring(1, $name.length - 2)
}
[pscustomobject]@{
GUID = $guid
Name = $name
Active = $active
}
}
$props = New-Object System.Collections.ArrayList
$props.AddRange(@(
'GUID'
'Name'
'Active'
'SubGUID'
'SubName'
'SubAlias'
'SettingGUID'
'SettingName'
'SettingAlias'
))
$results = foreach ($scheme in $schemes) {
$settings = powercfg /q $scheme.guid
$settings = $settings.trim()
$hash = @{
GUID = $scheme.GUID
Name = $scheme.Name
Active = $scheme.Active
}
$start = $setsubguid = $subguid = $setsettingalias = 0
foreach ($line in $settings) {
if ($line.startswith('Subgroup GUID')) {
$start = 1
$split = $line -split ':|\s{2}' | % {$_.trim()}
$hash.Add('SubGUID', $split[1])
if ($split[2]) {
$hash.Add('SubName', $split[2].substring(1, $split[2].length - 2))
}
$subguid = 1
$setsubguid = 1
} elseif ($line.StartsWith('GUID Alias') -and $subguid -eq 1) {
$hash.Add('SubAlias', $line.Split(':', 2)[1].trim())
$subguid = 0
} elseif ($line.StartsWith('Power Setting GUID')) {
if (!$setsubguid) {
$i = $back = 0
while ($hash.Keys -notcontains 'SubGUID' -and $i -ge 0) {
$back++
$i = $settings.IndexOf($line) - $back
$ln = $settings[$i]
if ($ln.startswith('Subgroup GUID')) {
$split = $ln -split ':|\s{2}' | % {$_.trim()}
$hash.Add('SubGUID', $split[1])
if ($split[2]) {
$hash.Add('SubName', $split[2].substring(1, $split[2].length - 2))
}
$setsubguid = 1
$ln = $settings[$i + 1]
if ($ln.StartsWith('GUID Alias')) {
$hash.Add('SubAlias', $ln.Split(':', 2)[1].trim())
}
}
}
}
$split = $line -split ':|\s{2}' | % {$_.trim()}
$hash.Add('SettingGUID', $split[1])
if ($split[2]) {
$hash.Add('SettingName', $split[2].substring(1, $split[2].length - 2))
}
} elseif ($line.StartsWith('GUID Alias')) {
if (!$start) {
continue
}
$hash.Add('SettingAlias', $line.Split(':', 2)[1].trim())
$setsettingalias = 1
} elseif ($line -match ':') {
if (!$setsettingalias) {
$setsettingalias = 1
continue
}
$split = $line.Split(':', 2).trim()
$split[0] = $split[0].Replace(' ', '')
$num = 0
$new = $split[0] + $num
while ($hash.Keys -contains $new) {
$num++
$new = $split[0] + $num
}
$hash.Add($new, $split[1])
if (!$props.Contains($new)) {
$props.Add($new) | Out-Null
}
} else {
[pscustomobject]$hash
$hash = @{
GUID = $scheme.GUID
Name = $scheme.Name
Active = $scheme.Active
}
$setsubguid = 0
}
}
}
$results | select $props
}