PowerShell时间记录脚本

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

这篇文章主要介绍了Windows Powershell Switch 循环以及PowerShell中数组可以与switch语句结合,产生意想不到的效果。
收藏 0 赞 0 分享

Windows Powershell 定义函数

这篇文章主要介绍了Windows Powershell 定义函数,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell实现导入安装证书功能脚本分享

这篇文章主要介绍了Powershell实现导入安装证书功能脚本分享,本文用编程方法实现把pfx证书文件导入到指定的库中,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell改变脚本执行优先权的代码分享

这篇文章主要介绍了Powershell改变脚本执行优先权的代码分享,本文通过控制进程的方式调整程序的执行顺序,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell读取PFX证书并输入密码的脚本分享

这篇文章主要介绍了Powershell读取PFX证书并输入密码的脚本分享,本文实现直接用脚本输入密码,避免了手动输入密码的麻烦,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell访问SQL Server数据库代码实例

这篇文章主要介绍了Powershell访问SQL Server数据库代码实例,本文直接给出代码,使用时只需要替换数据库配置参数即可,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell打印文本文档例子

这篇文章主要介绍了Powershell打印文本文档例子,本文相对简单,默认给出的是打印系统日志文件,使用时要替换成自己想打印的文件,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell创建简洁的HTML报告例子

这篇文章主要介绍了Powershell创建简洁的HTML报告例子,本文先是讲解了实现的步骤,然后给出了实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell在一个会话中只允许执行指定命令的方法

这篇文章主要介绍了Powershell在一个会话中只允许执行指定命令的方法,使用本文的技巧可以达到控制权限的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell中创建自定义对象例子

这篇文章主要介绍了Powershell中创建自定义对象例子,本文内容需要在3.0版本以上有效,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多