-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.ps1
150 lines (132 loc) · 5.19 KB
/
update.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Get the current directory where the script is running
$currentDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
# Paths to the XML files
$task1XML = "BATTERY.XML"
$task2XML = "BATTERY-SLEEP.XML"
# Function to update XML files
function Update-TaskXML {
param (
[string]$xmlPath,
[string]$currentDir,
[int]$sleepTimeoutMinutes = 0
)
# Check if the XML file exists
if (-not (Test-Path $xmlPath)) {
Write-Host "Error: File $xmlPath not found."
return
}
try {
# Load the XML file
[xml]$xml = Get-Content $xmlPath
# Define the XML namespace for the Task Scheduler schema
$namespace = "http://schemas.microsoft.com/windows/2004/02/mit/task"
$namespaceManager = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
$namespaceManager.AddNamespace("ns", $namespace)
if ($xmlPath -eq $task2XML) {
$idleTriggerEnabledNode = $xml.SelectNodes("//ns:Task//ns:Triggers/ns:IdleTrigger/ns:Enabled", $namespaceManager)
foreach ($node in $idleTriggerEnabledNode) {
if ($sleepTimeoutMinutes -gt 0) {
$node.InnerText = "true"
} else {
$node.InnerText = "false"
}
}
$durationNode = $xml.SelectNodes("//ns:Task//ns:Settings//ns:IdleSettings/ns:Duration", $namespaceManager)
foreach ($node in $durationNode) {
$newDuration = "PT${sleepTimeoutMinutes}M"
$node.InnerText = $newDuration
}
}
$execNodes = $xml.SelectNodes("//ns:Task//ns:Actions//ns:Exec/ns:Command", $namespaceManager)
foreach ($node in $execNodes) {
$fileName = [System.IO.Path]::GetFileName($node.InnerText)
$node.InnerText = Join-Path -Path $currentDir -ChildPath $fileName
}
$workingDirNodes = $xml.SelectNodes("//ns:Task//ns:Actions//ns:Exec/ns:WorkingDirectory", $namespaceManager)
foreach ($node in $workingDirNodes) {
$node.InnerText = $currentDir
}
# Save the updated XML
$xml.Save($xmlPath)
Write-Host "Success: Updated $xmlPath."
} catch {
Write-Host "Error: Failed to update $xmlPath. $_"
}
}
# Function to get the sleep timeout value in minutes
function Get-SleepTimeout {
try {
$powercfgOutput = powercfg /QUERY SCHEME_CURRENT SUB_SLEEP STANDBYIDLE
$lines = $powercfgOutput -split "`n"
foreach ($line in $lines) {
if ($line -match 'Current DC Power Setting Index: 0x([0-9a-fA-F]+)') {
$dcSleepTimeoutSeconds = [convert]::ToInt32($matches[1], 16)
return [math]::Round($dcSleepTimeoutSeconds / 60)
}
}
} catch {
Write-Host "Error: Failed to get sleep timeout. $_"
}
return 0
}
# Function to get the hibernate timeout value in minutes
function Get-HibernateTimeout {
try {
# Query the current power scheme for hibernate timeout settings
$powercfgOutput = powercfg /QUERY SCHEME_CURRENT SUB_SLEEP HIBERNATEIDLE
$lines = $powercfgOutput -split "`n"
foreach ($line in $lines) {
if ($line -match 'Current DC Power Setting Index: 0x([0-9a-fA-F]+)') {
$dcHibernateTimeoutSeconds = [convert]::ToInt32($matches[1], 16)
return [math]::Round($dcHibernateTimeoutSeconds / 60)
}
}
} catch {
Write-Host "Error: Failed to get hibernate timeout. $_"
}
return 0
}
# Function to get available sleep states (Sleep and Hibernate)
function Get-AvailableSleepStates {
try {
$powercfgOutput = powercfg /AVAILABLESLEEPSTATES
$lines = $powercfgOutput -split "`n"
$states = @{
Sleep = $false
Hibernate = $false
}
foreach ($line in $lines) {
if ($line -like '*Standby (S3)*') {
$states.Sleep = $true
} elseif ($line -like '*Hibernate*') {
$states.Hibernate = $true
} elseif ($line -like '*The following sleep states are not available on this system:*') {
break
}
}
return $states
} catch {
Write-Host "Error: Failed to get available sleep states. $_"
}
return @{
Sleep = $false
Hibernate = $false
}
}
# Initialize $sleepTimeoutMinutes
$sleepTimeoutMinutes = 0
# Get the available sleep states on the system
$states = Get-AvailableSleepStates
# If the system supports Sleep, calculate the sleep timeout value
if ($states.Sleep) {
$sleepTimeoutMinutes = [math]::Max((Get-SleepTimeout) - 15, 0)
}
# If the system supports Hibernate, and sleep timeout is invalid, calculate hibernate timeout
if ($states.Hibernate) {
if ($sleepTimeoutMinutes -le 0) {
$sleepTimeoutMinutes = [math]::Max((Get-HibernateTimeout) - 15, 0)
}
}
# Update the XML files
Update-TaskXML -xmlPath $task1XML -currentDir $currentDir
Update-TaskXML -xmlPath $task2XML -currentDir $currentDir -sleepTimeoutMinutes $sleepTimeoutMinutes