PowerShell时间记录脚本

所属分类: 脚本专栏 / PowerShell 阅读数: 1715
收藏 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 )

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

Windows Powershell 管道和重定向

这篇文章主要介绍了Windows Powershell 管道和重定向,需要的朋友可以参考下
收藏 0 赞 0 分享

Windows Powershell 进行数学运算

在Windows PowerShell中, 使用数学运算符来进行数学运算,数学运算符允许你在命令参数中计算数值. 你可以使用一个或者多个运算符进行加减乘除法, 也可以返回除法的余数(模). 包含这些计算的参数, 将计算结果作为参数值. 命令就像处理其他类型参数一样, 来处理参数值
收藏 0 赞 0 分享

Windows Powershell 执行外部命令

Windows PowerShell 在使用方面与 Cmd.exe 并无多大不同,只是 Windows PowerShell 的功能更为强大。与 Cmd.exe 一样,Windows PowerShell 具有内置的脚本编写语言,不过它比 Cmd.exe 原始的批处理语言更为灵活
收藏 0 赞 0 分享

Windows Powershell 命令集 cmdlets

在Windows PowerShell中,需要使用cmdlet执行指令。一个cmdlet代表着可操作某一对象的功能命令,cmdlet可使用"动词-名词"形式的语法:一个动词和一个名词,中间使用连字符连接,例如get-service和start-service。
收藏 0 赞 0 分享

Windows Powershell 别名

简单的说在Windows PowerShell中, 别名就是cmdlets或其他命令的替代名称.为什么要替代cmdlets呢,因为cmdlets命令说实话有点麻烦。
收藏 0 赞 0 分享

Windows Powershell 通过函数扩展别名

这篇文章主要介绍了Windows Powershell 通过函数扩展别名,需要的朋友可以参考下
收藏 0 赞 0 分享

Windows Powershell 执行文件和脚本

PowerShell脚本提供了一个方便的方法来自动化各种琐事。下面是关于PowerShell的一些基本概念,对于PowerShell初学者,掌握这些概念有助于加深对PowerShell脚本的理解。
收藏 0 赞 0 分享

Powershell小技巧之系统运行时间

本文主要教你如何使用powershell计算系统运行时间,其实很简单,因为Windows每次启动都有一个高进度计数器并且当系统运行这个计数器将返回一个毫秒,我们把这个毫秒计算下就得到系统运行时间了
收藏 0 赞 0 分享

Powershell小技巧之使用WMI测试服务响应

这篇文章主要介绍了Powershell小技巧之使用WMI测试服务响应,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell小技巧之使用WMI查询插上的U盘

本文主要讲诉了如何使用WMI查询当前插在你电脑上的USB设备,非常简单,学习powershell的同学可以参考下
收藏 0 赞 0 分享
查看更多