0

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
  • In short: [`Select-Object`](https://docs.microsoft.com/powershell/module/microsoft.powershell.utility/select-object) (`select`) by default returns _a `[pscustomobject]` instance_ that has the _requested properties_ - even when you're only asking for a _single_ property. To get only that property's _value_, use `-ExpandProperty $propertyName` instead - see the [linked duplicate](https://stackoverflow.com/q/48807857/45375) for details and alternatives, notably the ability to simply use `(...).$propertyName` – mklement0 Nov 09 '22 at 21:25
  • In other words: use `Get-Process processOfTheGame | Select-Object -ExpandProperty StartTime` or simply `(Get-Process processOfTheGame).StartTime` to get the start time as a `[datetime]` value. Then you can use `((Get-Date) - $StartTime).TotalHours -ge 2` to test if more than 2 hours have elapsed. – mklement0 Nov 09 '22 at 21:28

2 Answers2

0

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.

Toni
  • 1,738
  • 1
  • 3
  • 11
0

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
George
  • 2,034
  • 1
  • 15
  • 16