PowerShell時間記錄腳本
更新時間:2015年09月15日 09:01:53 投稿:hebedich
這篇文章主要介紹了PowerShell時間記錄腳本的相關(guān)資料,非常簡單實用,需要的朋友可以參考下
#initialization
$timeInterval = 30 #監(jiān)測間隔
$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.
整個開發(fā)與部署過程如下:
1.下載WindowsXP-KB926139-v2-x86-ENU
安裝powershell環(huán)境;
2.按照代碼要求,寫一個簡單的腳本;
3. 運行powershell時,同 bat是有區(qū)別的.注意以下方法:
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 )
相關(guān)文章
Windows Powershell 執(zhí)行外部命令
Windows PowerShell 在使用方面與 Cmd.exe 并無多大不同,只是 Windows PowerShell 的功能更為強大。與 Cmd.exe 一樣,Windows PowerShell 具有內(nèi)置的腳本編寫語言,不過它比 Cmd.exe 原始的批處理語言更為靈活。Cmd.exe 做到的事情,Windows PowerShell 幾乎都能做到。2014-08-08
Powershell實現(xiàn)捕獲系統(tǒng)內(nèi)置EXE程序的異常
這篇文章主要介紹了Powershell實現(xiàn)捕獲系統(tǒng)內(nèi)置EXE程序的異常,系統(tǒng)內(nèi)置的EXE程序是指如robocopy.exe、ipconfig.exe等命令的實現(xiàn)程序,需要的朋友可以參考下2014-12-12
PowerShell面向?qū)ο缶幊袒A(chǔ)知識總結(jié)
這篇文章主要介紹了PowerShell面向?qū)ο缶幊袒A(chǔ)知識總結(jié),本文著重講解面向?qū)ο蟮囊恍└拍?又給出了Get-Member命令輸出類的屬性和方法的例子,需要的朋友可以參考下2014-08-08

