PowerShell时间记录脚本

所属分类: 脚本专栏 / PowerShell 阅读数: 1747
收藏 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隐藏不显示窗口的多种方法

这篇文章主要介绍了PowerShell隐藏不显示窗口的多种方法,本文讲解了启动PowerShell时隐藏自己的窗口、在PowerShell启动其它进程时隐藏窗口、使用PowerShell隐藏其它进程的窗口三种方法,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell启用winrm失败:拒绝访问 0x80070005 -2147024891

这篇文章主要介绍了PowerShell启用winrm失败:拒绝访问 0x80070005 -2147024891,本文给出了详细的排查步骤和解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell目录文件夹管理权限的继承和指定方法

这篇文章主要介绍了Powershell目录文件夹管理权限的继承和指定方法,本文给出了创建文件夹、获取当前权限、添加新的权限、添加管理员权限等,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell查看本机文件关联程序和默认打开程序的方法

这篇文章主要介绍了PowerShell查看本机文件关联程序和默认打开程序的方法,本文给出了查看方法,同时给出了一份读取结果,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell脚本反引号用法实例:随时随地给代码换行

这篇文章主要介绍了PowerShell脚本反引号用法实例:随时随地给代码换行,在遇到一些超长代码行时非常有用,一般编程代码一行的字符数不超过80个哦,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell 数组的多种录入方法

这篇文章主要介绍了PowerShell 数组的多种录入方法,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell String对象方法小结

这篇文章主要介绍了PowerShell String对象方法,需要的朋友可以参考下
收藏 0 赞 0 分享

使用PowerShell获取当前主机内存使用量和总量的方法

这篇文章主要介绍了使用PowerShell获取当前主机内存使用量和总量的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell批量修改AD用户密码属性的代码

这篇文章主要介绍了PowerShell批量修改AD用户密码属性的代码,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell 自动备份oracle并上传到ftp

我这里有这样一个需求:有一个数据库,每天使用SQL Server Agent自动生成备份文件。然后,这个数据库非常重要,需要把每天的备份上传一个远程的FTP服务器上去。下面我们来看看如何使用Powershell来实现吧
收藏 0 赞 0 分享
查看更多