You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PowerShell限制exe运行时间:StartTime类型转换及时间对比求助

问题:PowerShell进程启动时间类型转换与程序时长限制实现

背景

花两天找限制游戏/任意程序使用时长的软件无果,时隔6年重拾编程自行开发。朋友推荐PowerShell,零基础状态下已实现:

  • 用gps | ? { $_.MainWindowTitle }查看带窗口的运行进程
  • 用$StartTime = Get-Process processOfTheGame | select starttime获取目标程序启动时间

核心问题

$StartTime是PSCustomObject类型,无法转换为datetime与Get-Date获取的当前时间对比,尝试多种转换方法均报错,需解决类型转换问题,实现当前时间比启动时间晚2小时时,自动用Stop-process -name GAME关闭程序。

已尝试的报错方法

方法1:[datetime]::ParseExact转换

$testConversion = [datetime]::ParseExact($StartTime, 'dd/MM/yyyy' ...

报错信息:

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

方法2:另一种ParseExact写法

[datetime]::parseexact($StartTime, 'dd-MMM-yy', $null)

报错信息:

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

方法3:Get-Date转换

$Date = get-date $StartTime -Format "dd-MM-yyyy"

报错信息:

+                      ~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand

方法4:直接日期计算

手动输入日期计算可执行:

PS> $Obj = ((get-date "10/22/2020 12:51:1") - (get-date "10/22/2020 12:20:1 ")) 

替换为$StartTime变量后报错:

PS> $Obj = ((get-date "10/22/2020 12:51:1") - (get-date $StartTime ...

报错信息:

+                               ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand

解决方案

问题根源是select starttime返回的是包含StartTime属性的自定义对象,而非直接的datetime值,直接提取属性即可解决:

步骤1:正确获取进程启动时间

# 直接提取进程的StartTime属性(原生datetime类型)
$process = Get-Process -Name processOfTheGame -ErrorAction SilentlyContinue
if ($process) {
    $startTime = $process.StartTime
}

-ErrorAction SilentlyContinue用于避免进程未运行时抛出错误。

步骤2:计算时长并执行关闭逻辑

$currentTime = Get-Date
# 计算运行时长(小时数,支持小数)
$timeDiff = ($currentTime - $startTime).TotalHours

# 运行时长超过2小时则关闭进程
if ($timeDiff -ge 2) {
    Stop-Process -Name processOfTheGame -Force
}

完整示例脚本

# 目标进程名称
$targetProcessName = "processOfTheGame"

# 获取目标进程
$process = Get-Process -Name $targetProcessName -ErrorAction SilentlyContinue

if ($process) {
    $startTime = $process.StartTime
    $currentTime = Get-Date
    $totalHoursRunning = ($currentTime - $startTime).TotalHours

    if ($totalHoursRunning -ge 2) {
        Write-Host "进程已运行超过2小时,正在关闭..."
        Stop-Process -Name $targetProcessName -Force
    } else {
        Write-Host "进程已运行 $([math]::Round($totalHoursRunning, 2)) 小时,未达到限制时长。"
    }
} else {
    Write-Host "目标进程未运行。"
}

关键说明

  • Get-Process返回的进程对象自带StartTime属性,类型为datetime,无需额外转换
  • (当前时间 - 启动时间).TotalHours可直接获取精确到小数的运行时长
  • -Force参数确保强制关闭进程,避免弹窗提示

内容的提问来源于stack exchange,提问作者Logo Niusan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 02:25:47