当前位置: 代码网 > it编程>前端脚本>Powershell > PowerShell时间记录脚本

PowerShell时间记录脚本

2024年05月18日 Powershell 我要评论
#initialization $timeinterval = 30 #监测间隔 $record = @{"coding" = 0; "outlook email" = 0; "gmail"

#initialization
   $timeinterval = 30 #监测间隔
   $record = @{"coding" = 0; "outlook email" = 0; "gmail" = 0; "google reader" = 0; "bbs" = 0; "other internet" = 0; "documents" = 0;}
  $count = 0
  $date = date -format "yyyymmdd"
  #try to resume
  if (test-path "d:\temp\timerecord$date.txt") {
    gc "d:\temp\timerecord$date.txt" | % {if ($_ -match "\w+\s+\d+") {
      $groups = [regex]::match($_, "^(\w+\s?\w+)\s+(\d+)").groups;
      $record[$groups[1].value] = [int]::parse($groups[2].value);
    }}
  }
  #start to monitor
  while ($true)
  {
    $titles = ps | ? {$_.mainwindowtitle} | select mainwindowtitle
    $titles | % {
      if ($_ -match "google 阅读器 - windows internet explorer") {$record["google reader"]++;}
      else {if ($_ -match "gmail - windows internet explorer") {$record["gmail"]++;}
      else {if ($_ -match "internet explorer") {$record["other internet"]++;}
      else {if ($_ -match "visual studio") {$record["coding"]++;}
      else {if ($_ -match "microsoft word") {$record["documents"]++;}
      else {if ($_ -match "microsoft office onenote") {$record["documents"]++;}
      else {if ($_ -match "microsoft powerpoint") {$record["documents"]++;}
      else {if ($_ -match "message (html)") {$record["outlook email"]++;}
      else {if ($_ -match "bbs") {$record["bbs"]++;}
      }}}}}}}}
    }
    sleep($timeinterval)
    $count = ($count + 1) % 10 #为了防止数据丢失,每10次记录写入文件一次
    if ($count -eq 0) {$record > "d:\temp\timerecord$date.txt"}
  }

为了解技术思路,研究了一下powershell.
整个开发与部署过程如下:

1.下载windowsxp-kb926139-v2-x86-enu

安装powershell环境;

2.按照代码要求,写一个简单的脚本;

3. 运行powershell时,同 bat是有区别的.注意以下方法:

1) 解除限制:

set-executionpolicy unrestricted

2) 将文件名保存为ps1
3) 通过以下方法运行(假如文件名是c:a.ps1)
ps c:> .a
[@more@]

示例代码:

function foo ( [int] $x)
{
$x = $x + 1
echo $x
}
foo (1 )

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com