-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiningRoutine.ps1
120 lines (105 loc) · 4.05 KB
/
MiningRoutine.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
[CmdletBinding()]
param (
[Parameter(Mandatory)][string]$processName,
[Parameter(Mandatory)][string]$ScheduleJson,
[Parameter(Mandatory)][string]$executablePath,
[Parameter(Mandatory)][string]$executableArgs
)
# Void period
$ElectricitySchedule = Get-Content $ScheduleJson | ConvertFrom-Json
# Function that checks if a process is running. If it is returns the info about the process, if not it doesn't return anything
function CheckProcess {
[CmdletBinding()]
param (
$processName
)
$process = get-process -Name $processName -ErrorAction SilentlyContinue
$parentProcessID = ( Get-CimInstance win32_process | Where-Object processid -eq $process.Id).parentprocessid
return $parentProcessID, $process.Id
}
# Function that stops a process based on a process and reports back informing the process was terminated
function StopProcess {
[CmdletBinding()]
param (
$processId
)
Stop-Process -Id $processId -Force
Write-Host "Job terminated!"
}
# Function that puts the computer to sleep
function SleepingBeauty {
# load assembly System.Windows.Forms which will be used
Add-Type -AssemblyName System.Windows.Forms
# so you can wake up your computer from sleep
$DisableWake = $false;
# set powerstate to suspend (sleep mode)
$PowerState = [System.Windows.Forms.PowerState]::Suspend;
# do not force putting Windows to sleep
$Force = $false;
# do it! Set computer to sleep
[System.Windows.Forms.Application]::SetSuspendState($PowerState, $Force, $DisableWake);
}
# Assigns the day object to a variable
$Monday = $ElectricitySchedule.Monday
$Tuesday = $ElectricitySchedule.Tuesday
$Wednesday = $ElectricitySchedule.Wednesday
$Thursday = $ElectricitySchedule.Thursday
$Friday = $ElectricitySchedule.Friday
$Saturday = $ElectricitySchedule.Saturday
$Sunday = $ElectricitySchedule.Sunday
# Gets the current date for the timer
$EndTime = Get-Date
while ($true) {
# Check day of the week
$dayOfWeek = $(get-date).DayOfWeek
# Assigns the day interval to a variable
switch ($dayOfWeek) {
"Monday" { $Intervals = $Monday }
"Tuesday" { $Intervals = $Tuesday }
"Wednesday" { $Intervals = $Wednesday }
"Thursday" { $Intervals = $Thursday }
"Friday" { $Intervals = $Friday }
"Saturday" { $Intervals = $Saturday }
"Sunday" { $Intervals = $Sunday }
}
# Check current interval
$members = $intervals | Get-Member -MemberType NoteProperty
# Assigns the minimum and maximum values of each interval to a separate array
$minTimes = @()
$maxTimes = @()
foreach ($member in $members) {
$minTimes += $([datetime]::parseexact($($($Intervals.($member.Name)[0]).ToString()), "HH:mm", $null)).TimeOfDay.TotalMinutes
$maxTimes += $([datetime]::parseexact($($($Intervals.($member.Name)[1]).ToString()), "HH:mm", $null)).TimeOfDay.TotalMinutes
}
$startMining = 0
For ($i = 0; $i -lt $minTimes.Length; $i++) {
if ([int]((get-date).TimeOfDay.TotalMinutes) -in $minTimes[$i]..$maxTimes[$i]) {
$startMining = 1
}
}
if ($startMining -eq 1 ) {
# Check process is running
$checkProcessRunning = CheckProcess -processName $processName
if (!$checkProcessRunning[1]) {
# Start mining job
Start-Process -FilePath $executablePath -ArgumentList $executableArgs
}
else {
Write-Host "$processName is running!"
}
}
if ($startMining -eq 0 ) {
# Check process is running
$checkProcessRunning = CheckProcess -processName $processName
# Kill mining job
if ($checkProcessRunning[1]) {
StopProcess -processId $checkProcessRunning[1]
Start-Sleep -Seconds 300
Start-Job -ScriptBlock ${Function:SleepingBeauty}
}
}
# Runs the while loop every 5 minutes to decrease CPU consumption
$EndTime = $(get-date).AddMinutes(5)
Write-Host "Running in the background..."
Start-Sleep -Seconds $(New-TimeSpan -End $EndTime).TotalSeconds
}