PowerShell时间记录脚本

   2015-11-08 0
核心提示:这篇文章主要介绍了PowerShell时间记录脚本的相关资料,非常简单实用,需要的朋友可以参考下

#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举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

点击排行