PowerShell时间记录脚本
2019/7/10 21:22:27
本文主要是介绍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时间记录脚本的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-09-14SharePoint 2019 用 PowerShell将启用发布功能站点网站集另存为模板
- 2022-08-30PowerShell教程 - 程序性能和BUG分析工具
- 2022-08-30PowerShell教程 - 模块管理(Modules Management)
- 2022-08-29PowerShell教程 - Web requests(Web请求)
- 2022-08-26PowerShell教程 - 日期时间管理(Date & Time Management)
- 2022-08-25PowerShell教程 - 磁盘与硬件管理(Disk & Hardware Management)
- 2022-08-25PowerShell教程 - 系统事件管理(System Event Management)
- 2022-08-25PowerShell教程 - 文件系统管理(File System Management)
- 2022-08-24PowerShell教程 - 网络管理(Network Management)
- 2022-08-24PowerShell