PowerShell时间记录脚本

所属分类: 脚本专栏 / PowerShell 阅读数: 1673
收藏 0 赞 0 分享

#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 )

更多精彩内容其他人还在看

PowerShell操作Excel、CSV详细介绍

这篇文章主要介绍了PowerShell操作Excel、CSV详解,本文比较深入的探讨了PowerShell中如何操作Excel及CSV,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell中显示隐藏文件的方法

这篇文章主要介绍了Powershell中显示隐藏文件的方法,本文使用是是Get-ChildItem检索文件,加上-Hidden参数就可以显示隐藏文件了,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell中iso8601格式日期和DateTime对象互转实例

这篇文章主要介绍了PowerShell中iso8601格式日期和DateTime对象互转实例,本文讲解了iso8601格式转换成DateTime对象、日期时间转换成iso8601格式两个方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell使用C#实现缩写路径

这篇文章主要介绍了Powershell使用C#实现缩写路径,缩写路径有时候是非常有用的,比如某些报表的路径太长会很难看,缩写后就会好看许多,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell截取字符串并添加省略号的例子

这篇文章主要介绍了Powershell截取字符串并添加省略号的例子,本文直接给出代码实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell读取本机注册表中的所有软件关联扩展名

这篇文章主要介绍了Powershell读取本机注册表中的所有软件关联扩展名,本文直接给出实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell实现按类型排序

这篇文章主要介绍了Powershell实现按类型排序,本文直接给出实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell中打开网页实例

这篇文章主要介绍了Powershell中打开网页实例,本文直接给出实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell实现获取进程所有者

这篇文章主要介绍了PowerShell实现获取进程所有者,本文直接给出实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Windows Powershell Foreach 循环

Foreach-object 为cmdlet命令,使用在管道中,对管道结果逐个处理,foreach为遍历集合的关键字。
收藏 0 赞 0 分享
查看更多