PowerShell时间记录脚本

 更新时间:2015年09月15日 09:01:53   投稿:hebedich  
这篇文章主要介绍了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 )

相关文章

  • PowerShell 3.0管理Hyper-V 3.0

    PowerShell 3.0管理Hyper-V 3.0

    这篇文章主要介绍了PowerShell 3.0管理Hyper-V 3.0的相关资料,非常的详细,需要的朋友可以参考下
    2015-09-09
  • Windows Powershell 执行外部命令

    Windows Powershell 执行外部命令

    Windows PowerShell 在使用方面与 Cmd.exe 并无多大不同,只是 Windows PowerShell 的功能更为强大。与 Cmd.exe 一样,Windows PowerShell 具有内置的脚本编写语言,不过它比 Cmd.exe 原始的批处理语言更为灵活。Cmd.exe 做到的事情,Windows PowerShell 几乎都能做到。
    2014-08-08
  • PowerShell 自动备份oracle并上传到ftp

    PowerShell 自动备份oracle并上传到ftp

    我这里有这样一个需求:有一个数据库,每天使用SQL Server Agent自动生成备份文件。然后,这个数据库非常重要,需要把每天的备份上传一个远程的FTP服务器上去。下面我们来看看如何使用Powershell来实现吧
    2015-08-08
  • powershell常用命令大全

    powershell常用命令大全

    PowerShell是强大的自动化工具,涵盖服务管理、事件日志查询、变量操作、脚本执行、远程管理以及模块管理等,本文介绍powershell常用命令大全,感兴趣的朋友一起看看吧
    2025-02-02
  • Powershell实现捕获系统内置EXE程序的异常

    Powershell实现捕获系统内置EXE程序的异常

    这篇文章主要介绍了Powershell实现捕获系统内置EXE程序的异常,系统内置的EXE程序是指如robocopy.exe、ipconfig.exe等命令的实现程序,需要的朋友可以参考下
    2014-12-12
  • PowerShell面向对象编程基础知识总结

    PowerShell面向对象编程基础知识总结

    这篇文章主要介绍了PowerShell面向对象编程基础知识总结,本文着重讲解面向对象的一些概念,又给出了Get-Member命令输出类的属性和方法的例子,需要的朋友可以参考下
    2014-08-08
  • Windows Powershell 介绍和安装

    Windows Powershell 介绍和安装

    Powershell 是运行在windows机器上实现系统和应用程序管理自动化的命令行脚本环境。微软之所以将Powershell 定位为Power,并不是夸大其词,因为它完全支持对象。其可读性,易用性,可以位居当前所有shell之首。
    2014-08-08
  • 使用 powershell 创建虚拟机

    使用 powershell 创建虚拟机

    Azure PowerShell 模块用于从 PowerShell 命令行或脚本创建和管理 Azure 资源。 本指南详细介绍了如何使用 Azure PowerShell 模块创建虚拟机。
    2017-10-10
  • 探索PowerShell(十二) 筛选器 Filters

    探索PowerShell(十二) 筛选器 Filters

    筛选器是函数的一种特殊形式
    2012-12-12
  • Powershell脚本中包含文件资源的例子

    Powershell脚本中包含文件资源的例子

    这篇文章主要介绍了Powershell脚本中包含文件资源的例子,本文直接给出示例代码,本文脚本代码适应Powershell3.0及以后版本,需要的朋友可以参考下
    2015-03-03

最新评论