您的位置:首页 > 运维架构 > Shell

powershell 编写event log

2015-07-24 09:41 381 查看
PowerShell是个很牛逼的命令行工具,在Windows 7以后的系统里都默认自带。学会了PowerShell就可以提升逼格,让妹子崇拜。今天就教大家用PowerShell操作Windows的Event Log。首先,启动PowerShell很简单,只需要在开始菜单里输入Power,就能找到了(Windows 8用户可以直接在开始屏幕里搜索Power)。

启动PowerShell后,你可以看到一个这样的界面。和CMD不同,它是深蓝色背景的,并且路径前多了PS前缀。看起来很厉害的样子:

有用程序写过Windows Event Log的童鞋都知道,我们的首要任务是创建一个Log和一个Source。PowerShell里语法如下:new-eventlog -LogName 日志名称 -Source 源名称比如,我们创建一个Name为Game,Source为CS的Log,可以这样写:new-eventlog -LogName Game -Source CS。执行后在事件查看器里就能找到了。

写入日志也很方便:write-eventLog -LogName Game -Message " Edi Killed GayShyFool with AWP " -Source CS -id 1

默认写入的是Information级别的Log。如果想要制定Level,就要加上EntryType参数: write-eventLog -LogName Game -Message "GayShyFool invoked 'My Dad is a Gang' method." -Source CS -id 2 -EntryType Warning

要直接在PowerShell里面读取EventLog,用的是get-eventlog命令: get-eventlog -LogName Game -Source CS

要删除整个Log(包括分类),用的是Remove-EventLog命令。注意大小写敏感。在不指定参数的情况下,PowerShell也会询问我们参数。在LogName[0]:后输入Game,按两下回车,就可以移除整个Game的Log了。PS C:\Users\Edi_Wang> Remove-EventLogcmdlet Remove-EventLog at command pipeline position 1Supply values for the following parameters:LogName[0]: Gam
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: