PowerShell時(shí)間記錄腳本
#initialization $timeInterval = 30 #監(jiān)測(cè)間隔 $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 #為了防止數(shù)據(jù)丟失,每10次記錄寫入文件一次 if ($count -eq 0) {$record > "d:\temp\timeRecord$date.txt"} }
為了解技術(shù)思路,研究了一下powershell.
整個(gè)開(kāi)發(fā)與部署過(guò)程如下:
1.下載WindowsXP-KB926139-v2-x86-ENU
安裝powershell環(huán)境;
2.按照代碼要求,寫一個(gè)簡(jiǎn)單的腳本;
3. 運(yùn)行powershell時(shí),同 bat是有區(qū)別的.注意以下方法:
1) 解除限制:
set-executionpolicy Unrestricted
2) 將文件名保存為ps1
3) 通過(guò)以下方法運(yùn)行(假如文件名是c:a.ps1)
PS C:> .a
[@more@]
示例代碼:
function foo ( [int] $x) { $x = $x + 1 echo $x } foo (1 )
相關(guān)文章
PowerShell中刪除空格、點(diǎn)號(hào)、減號(hào)和換行方法代碼實(shí)例
這篇文章主要介紹了PowerShell中刪除空格、點(diǎn)號(hào)、減號(hào)和換行方法代碼實(shí)例,本文直接給出實(shí)例代碼,需要的朋友可以參考下2015-05-05用PowerShell刪除N天前或指定日期(前后)創(chuàng)建(或修改)的文件
這篇文章主要介紹了用PowerShell刪除N天前或指定日期(前后)創(chuàng)建(或修改)的文件,需要的朋友可以參考下2016-11-11Windows Powershell Switch 循環(huán)
這篇文章主要介紹了Windows Powershell Switch 循環(huán)以及PowerShell中數(shù)組可以與switch語(yǔ)句結(jié)合,產(chǎn)生意想不到的效果。2014-10-10PowerShell腳本實(shí)現(xiàn)網(wǎng)卡DHCP自動(dòng)獲取IP地址、設(shè)置靜態(tài)IP地址的方法
這篇文章主要介紹了PowerShell腳本實(shí)現(xiàn)網(wǎng)卡DHCP自動(dòng)獲取IP地址、設(shè)置靜態(tài)IP地址的方法,本文同時(shí)講解了用PowerShell設(shè)置網(wǎng)卡DHCP、靜態(tài)IP的方法,需要的朋友可以參考下2014-08-08PowerShell實(shí)現(xiàn)在控制臺(tái)中插入綠色的打勾符號(hào)
這篇文章主要介紹了PowerShell實(shí)現(xiàn)在控制臺(tái)中插入綠色的打勾符號(hào),本文重點(diǎn)的過(guò)分如何輸出個(gè)一個(gè)特殊字符以及如何輸出一個(gè)有顏色的字符,需要的朋友可以參考下2015-06-06