Limit the time of use of a .exe with POWERSHELL

71 Views Asked by At

I've been looking during 2 days for a sotfware which allows me to limit the time I play(or use any program) and I found nothing usefull so I decided to put my coding cape back since 6 years ago and program it myself.

A friend recommended me powershell as the easiest and faster way to do it. I have no idea of powershell but I've come to this: With

gps | ? { $_.MainWindowTitle } 

I check the running processes . Then with an if clause I would define if the game is being runned, if it is being runned I use

$StartTime = Get-Process processOfTheGame | select starttime 

to know when the game started. And then I should use another if clause to compare it with actual date

Get-Date 

but im finding problems to compare it as Get-Process processOfTheGame | select starttime data type is PSCustomObject so it is throwing me errors when I try to change the format to datetype.

So i need help to convert the $StartTime variable to datetype and then to compare it with the actual date. and if the actual date is 2 houres more than $StartTime close the program with

Stop-process -name GAME

Things i've tried

$testConversion = [datetime]::ParseExact($StartTime, 'dd/MM/yyyy' ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest
[datetime]::parseexact($StartTime, 'dd-MMM-yy', $null)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest
$Date = get-date $StartTime -Format "dd-MM-yyyy"
+                      ~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand
PS> $Obj = ((get-date "10/22/2020 12:51:1") - (get-date "10/22/2020 12:20:1 ")) 

I tried it with a "flat" date cause it is supposed to work that way but it does not. It neither works with the variable StartTime

PS> $Obj = ((get-date "10/22/2020 12:51:1") - (get-date "10/2 ...
+                               ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand
2

There are 2 best solutions below

0
On

You could use [System.Diagnostics.Stopwatch] to start a stopwatch and compare the time passed by with a timespan object... but there is a far simpler way:

#Specify process name
$processName = 'gameXy.exe'
#Specify your game time in hours
$timeSleep = 2

#wait until process gets started
do {
    start-sleep -Seconds 1
}
until (gps | ? {$_.MainWindowTitle -and $_.processName -eq $processName})
#wait 
Start-Sleep -Seconds ($timeSleep * 3600)
#kill process
Stop-Process -Name $processName

this runs continously until the process gets detected -> kills the process after the specified amount of time.

0
On

You were very close to getting a DateTime object from get-process. Here's an example from my system (I'm running multiple instances of notepad.exe, so $times is an array).

PS C:\> $times =(get-process -name notepad | select starttime)
PS C:\> $times[0].starttime

Wednesday, November 9, 2022 08:22:07 PM

PS C:\> $tmp = $times[0].starttime;
PS C:\> $tmp.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     DateTime                                 System.ValueType