{"id":33300,"date":"2011-01-01T17:05:00","date_gmt":"2011-01-01T16:05:00","guid":{"rendered":"https:\/\/www.fuhaijun.com\/?p=33300"},"modified":"2024-11-06T11:21:14","modified_gmt":"2024-11-06T03:21:14","slug":"powershell-2-0%e7%ae%a1%e7%90%86%e4%ba%8b%e4%bb%b6%e6%97%a5%e5%bf%97%e4%ba%8c%e5%86%99%e5%85%a5%e5%92%8c%e6%90%9c%e7%b4%a2%e4%ba%8b%e4%bb%b6%e6%97%a5%e5%bf%97","status":"publish","type":"post","link":"https:\/\/www.fuhaijun.com\/?p=33300","title":{"rendered":"PowerShell 2.0\u7ba1\u7406\u4e8b\u4ef6\u65e5\u5fd7(\u4e8c)\u5199\u5165\u548c\u641c\u7d22\u4e8b\u4ef6\u65e5\u5fd7"},"content":{"rendered":"<p>\u4e8b\u4ef6\u65e5\u5fd7\u662f\u64cd\u4f5c\u7cfb\u7edf\u7528\u6765\u4fdd\u5b58\u672c\u8eab\u53ca\u5176\u4ed6\u7a0b\u5e8f\u4fe1\u606f\u7684\u5de5\u5177\uff0c\u672c\u6587\u5c06\u4f1a\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7PowerShell\u811a\u672c\u9605\u8bfb\u4e8b\u4ef6\u65e5\u5fd7\u5e76\u521b\u5efa\u65b0\u7684\u65e5\u5fd7\u6761\u76ee\u3002Windows\u7cfb\u7edf\u4e2d\u5305\u542b\u591a\u79cd\u4e8b\u4ef6\u65e5\u5fd7\uff0c\u5728Windows XP\u7cfb\u7edf\u4e2d\u5305\u62ec4\u79cd\u4e3b\u8981\u7684\u4e8b\u4ef6\u65e5\u5fd7\u3002\u5373\u5e94\u7528\u7a0b\u5e8f\u3001\u7cfb\u7edf\u3001\u5b89\u5168\u6027\u53caInternet Explorer\u65e5\u5fd7\uff0c\u5982\u56fe1\u6240\u793a\u3002\u8fd9\u6837\u7528\u6237\u53ef\u4ee5\u65b9\u4fbf\u5730\u4ece\u7cfb\u7edf\u4e2d\u63d0\u53d6\u548c\u67e5\u770b\u65e5\u5fd7\uff0c\u4ece\u800c\u4e86\u89e3\u7cfb\u7edf\u7684\u5f53\u524d\u8fd0\u884c\u60c5\u51b5\u3002\u672c\u6587\u5c06\u4f1a\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7PowerShell\u5199\u5165\u548c\u641c\u7d22Windows\u4e8b\u4ef6\u65e5\u5fd7\u7684\u65b9\u6cd5\u3002<\/p>\n<p><strong>1 \u5199\u5165\u4e8b\u4ef6\u65e5\u5fd7<\/strong><\/p>\n<p>PowerShell\u5199\u5165\u4e8b\u4ef6\u65e5\u5fd7\u7684\u529f\u80fd\u5f88\u6709\u7528\uff0cWindows Vista\u548cWindows Server 2008\u7684\u4e8b\u4ef6\u65e5\u5fd7\u5747\u63d0\u4f9b\u4e86\u591a\u79cd\u7ba1\u7406\u7cfb\u7edf\u4e2d\u53d1\u751f\u4e8b\u4ef6\u7684\u529f\u80fd\u3002\u901a\u8fc7\u4f7f\u7528.NET Framework\u7684System.Diagnostics.EventLog\u7c7b\uff0c\u5373\u53ef\u5c06\u4fe1\u606f\u5199\u5165\u5230\u4f20\u7edf\u4e8b\u4ef6\u65e5\u5fd7\uff08\u5373\u7cfb\u7edf\u3001\u5e94\u7528\u7a0b\u5e8f\uff0c\u4ee5\u53ca\u5b89\u5168\uff09\u4e2d\uff1b\u53e6\u5916\u8fd8\u53ef\u4ee5\u521b\u5efa\u81ea\u5df1\u7684\u4e8b\u4ef6\u65e5\u5fd7\uff0c\u5e76\u5c06\u65e5\u5fd7\u5199\u5165\u5176\u4e2d\u3002<\/p>\n<p>\uff081\uff09\u521b\u5efa\u6765\u6e90<\/p>\n<p>\u5728\u5199\u5165\u4e8b\u4ef6\u65e5\u5fd7\u4e4b\u524d\uff0c\u5fc5\u987b\u521b\u5efa\u6765\u6e90\uff0c\u6765\u6e90\u53ef\u4ee5\u88ab\u4e8b\u4ef6\u65e5\u5fd7\u7528\u4e8e\u533a\u522b\u53d1\u751f\u8be5\u4e8b\u4ef6\u7684\u6e90\u5934\u3002\u8fd9\u6837\u5982\u679c\u9700\u8981\u5199\u5165\u5171\u4eab\u6765\u6e90\u7684\u4e8b\u4ef6\u65e5\u5fd7\u4e2d\uff0c\u5373\u53ef\u901a\u8fc7\u5c5e\u6027\u67e5\u8be2\u3002\u5728\u521b\u5efa\u6765\u6e90\u540e\uff0c\u8fd8\u9700\u8981\u4e3a\u65e5\u5fd7\u5bf9\u8c61\u521b\u5efa\u65b0\u7684\u5b9e\u4f8b\u3002\u5c06\u6765\u6e90\u4e0e\u4e8b\u4ef6\u65e5\u5fd7\u5173\u8054\uff0c\u7136\u540e\u6307\u5b9a\u8981\u5199\u5165\u7684\u4fe1\u606f\u3002<\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cWriteToAppLog.ps1\u201d\u7684\u811a\u672c\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\uff0c\u4ee3\u7801\u5982\u4e0b\uff0c\u540e\u9762\u5c06\u6839\u636e\u4ee3\u7801\u7ed9\u51fa\u76f8\u5173\u7684\u8bf4\u660e\uff1a<\/p>\n<p>if(![system.diagnostics.eventlog]::sourceExists(&quot;ps_script&quot;))<\/p>\n<p>{<\/p>\n<p>$applog = [system.diagnostics.eventlog]::CreateEventSource(&quot;ps_script&quot;,&quot;Application&quot;)<\/p>\n<p>}<\/p>\n<p>$applog = New-Object system.diagnostics.eventlog(&quot;application&quot;,&quot;.&quot;)<\/p>\n<p>$applog.source = &quot;ps_script&quot;<\/p>\n<p>$applog.writeEntry(&quot;test for script write applog&quot;)<\/p>\n<p>\u5728\u8be5\u811a\u672c\u4e2d\u8c03\u7528\u6765\u6e90\u9700\u8981\u9996\u5148\u786e\u5b9a\u662f\u5426\u5df2\u7ecf\u5b9a\u4e49\u6b64\u6765\u6e90\u5e76\u5173\u8054\u4e86\u4e8b\u4ef6\u65e5\u5fd7\uff0c\u4e3a\u6b64\u9700\u8981\u6267\u884c\u975e\u8fd0\u7b97\u3002\u5728\u6765\u6e90\u6ca1\u6709\u5b9a\u4e49\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u5bb9\u9519\u5904\u7406\uff0c\u5373\u5728\u6765\u6e90\u4e0d\u5b58\u5728\u7684\u60c5\u51b5\u4e0b\u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u6765\u6e90\u540d\u7684\u6765\u6e90\u3002\u7531\u4e8e\u5728\u811a\u672c\u4e2d\u7528system.diagnostics.eventlog\u7c7b\u7684SourceExist\u9759\u6001\u65b9\u6cd5\u5224\u65ad\u6765\u6e90\u662f\u5426\u5b58\u5728\uff0c\u6240\u4ee5\u9759\u6001\u65b9\u6cd5\u540d\u524d\u9700\u8981\u52a0\u4e24\u4e2a\u5192\u53f7\u3002\u5982\u679c\u6765\u6e90\u4e0d\u5b58\u5728\uff0c\u5219\u9700\u8981\u521b\u5efa\u3002\u5728\u4f7f\u7528CreateEventSource\u65b9\u6cd5\u65f6\uff0c\u5fc5\u987b\u6307\u5b9a\u6765\u6e90\u53ca\u8981\u5173\u8054\u5230\u8be5\u6765\u6e90\u7684\u4e8b\u4ef6\u65e5\u5fd7\u7684\u540d\u79f0\u3002<\/p>\n<p>\u5728\u5b9a\u4e49\u4e8b\u4ef6\u6765\u6e90\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528New-Object cmdlet\u521b\u5efa\u4e8b\u4ef6\u65e5\u5fd7\u7684\u5b9e\u4f8b\u3002\u5728\u4e0a\u9762\u7684\u811a\u672c\u4e2d\uff0c\u4f7f\u7528\u53d8\u91cf$applog\u4fdd\u5b58\u7531New-Object cmdlet\u8fd4\u56de\u7684\u65f6\u95f4\u5bf9\u8c61\u540d\u79f0\u3002\u5728\u8c03\u7528\u8be5cmdlet\u65f6\u9700\u8981\u6307\u5b9a\u65e5\u5fd7\u53ca\u5176\u6240\u5728\u8ba1\u7b97\u673a\u7684\u540d\u79f0\u3002\u5728\u4e0a\u9762\u7684\u811a\u672c\u4e2d\uff0c\u4f7f\u7528\u7684\u662f\u672c\u8ba1\u7b97\u673a\u7684\u65e5\u5fd7\u3002\u53e5\u70b9\uff08.\uff09\u8868\u793a\u5f53\u524d\u8ba1\u7b97\u673a\u7684\u5feb\u6377\u65b9\u5f0f\u3002<\/p>\n<p align=\"left\">\u4e00\u65e6\u5728$Applog\u65e5\u5fd7\u4e2d\u521b\u5efa\u4e86\u5230\u5e94\u7528\u7a0b\u5e8f\u65e5\u5fd7\u7684\u5f15\u7528\uff0c\u8fd8\u9700\u8981\u4f7f\u7528Source\u5c5e\u6027\u5c06ps_script\u6765\u6e90\u6307\u5b9a\u7ed9\u8be5\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528WriteEntry\u65b9\u6cd5\u5c06\u5185\u5bb9\u5199\u5165\u5230\u5e94\u7528\u7a0b\u5e8f\u65e5\u5fd7\u3002\u4e0a\u9762\u7684\u4ee3\u7801\u6267\u884c\u540e\u5199\u5165\u7cfb\u7edf\u65e5\u5fd7\u4e2d\u7684\u65b0\u8bb0\u5f55\u5982\u56fe1\u6240\u793a\uff0c\u5728\u4e8b\u4ef6\u67e5\u770b\u5668\u4e2d\u663e\u793a\u7684\u5185\u5bb9\u56fe2\u6240\u793a\u3002<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" height=\"456\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image_thumb.png\" width=\"642\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe1 \u5199\u5165\u7cfb\u7edf\u65e5\u5fd7\u4e2d\u7684\u65b0\u8bb0\u5f55<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image1.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" height=\"454\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image_thumb1.png\" width=\"408\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe2 \u5728\u4e8b\u4ef6\u67e5\u770b\u5668\u4e2d\u663e\u793a\u7684\u5185\u5bb9<\/p>\n<p>\uff082\uff09\u5c06cmdlet\u7684\u8f93\u51fa\u5185\u5bb9\u4fdd\u5b58\u5230\u65e5\u5fd7\u4e2d<\/p>\n<p>\u524d\u9762\u5728\u65e5\u5fd7\u4e2d\u5199\u5165\u5b57\u7b26\u4e32\u4e5f\u8bb8\u770b\u8d77\u6765\u6bd4\u8f83\u7b80\u5355\uff0c\u4f46\u5374\u6709\u5f88\u591a\u6df1\u5c42\u6b21\u7684\u7528\u9014\u3002\u5982\u53ef\u4ee5\u4f7f\u7528WriteToAppLog.ps1\u5c06\u811a\u672c\u7684\u8fd0\u884c\u60c5\u51b5\u53ca\u8fd0\u884c\u662f\u5426\u6210\u529f\u7684\u7ed3\u679c\u5199\u5165\u4e8b\u4ef6\u65e5\u5fd7\uff0c\u8fd9\u5bf9\u540e\u9762\u7684\u7a0b\u5e8f\u6392\u9519\u548c\u8c03\u8bd5\u5f88\u6709\u7528\u3002<\/p>\n<p>\u53e6\u5916\u8fd8\u53ef\u4ee5\u4f7f\u7528\u8fd9\u79cd\u5c06\u4fe1\u606f\u5199\u5165\u5230\u4e8b\u4ef6\u65e5\u5fd7\u4e2d\u7684\u65b9\u6cd5\u6765\u4fdd\u5b58PowerShell\u4ee3\u7801\u7684\u8fd0\u884c\u7ed3\u679c\u3002\u4e3a\u4e86\u80fd\u591f\u5c06WMI\u7684\u67e5\u8be2\u7ed3\u679c\u5199\u5165\u5230\u5e94\u7528\u7a0b\u5e8f\u65e5\u5fd7\u4e2d\uff0c\u901a\u8fc7\u8bb0\u5f55\u7279\u5b9a\u65f6\u95f4\u5728\u7279\u5b9a\u8ba1\u7b97\u673a\u4e2d\u8fd0\u884c\u7684\u8fdb\u7a0b\uff0c\u5219\u5f97\u5230\u8c03\u6574\u6027\u80fd\u548c\u589e\u5f3a\u5b89\u5168\u6027\u7684\u7b2c\u4e00\u624b\u8d44\u6599\u3002\u521b\u5efa\u540d\u4e3a\u201cWriteProcessToAppLog.ps1\u201d\u7684\u811a\u672c\uff0c\u5176\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>$strProcess = Get-WmiObject win32_process |<\/p>\n<p>select-object name | Out-String<\/p>\n<p>if(![system.diagnostics.eventlog]::sourceExists(&quot;ps_script&quot;))<\/p>\n<p>{<\/p>\n<p>$applog = [system.diagnostics.eventlog]::CreateEventSource(&quot;ps_script&quot;,&quot;Application&quot;)<\/p>\n<p>}<\/p>\n<p>$applog = New-Object system.diagnostics.eventlog(&quot;application&quot;,&quot;.&quot;)<\/p>\n<p>$applog.source = &quot;ps_script&quot;<\/p>\n<p>$applog.writeEntry($strProcess)<\/p>\n<p>\u8be5\u811a\u672c\u4f7f\u7528Get-WmiObject cmdlet\u5bf9Win32_process\u7c7b\u6267\u884c\u57fa\u672c\u7684WMI\u67e5\u8be2\uff0c\u8fd9\u6837\u5c06\u4f1a\u751f\u6210\u4e00\u4e2a\u5bf9\u8c61\u96c6\u5408\uff0c\u5176\u4e2d\u5305\u62ec\u672c\u673a\u4e2d\u6240\u6709\u8fdb\u7a0b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u4fe1\u606f\u3002\u4e3a\u4e86\u51cf\u5c11\u8fd4\u56de\u7684\u4fe1\u606f\u6570\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528Select-Object\u5e76\u9009\u62e9\u9700\u8981\u7684\u540d\u79f0\uff0c\u8fd9\u6837\u83b7\u5f97\u7684\u7ed3\u679c\u5c06\u662f\u81ea\u5b9a\u4e49\u7684Windows PowerShell\u5bf9\u8c61\u3002\u4e3a\u4e86\u5c06\u4fe1\u606f\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u9700\u8981\u4f7f\u7528Out-String cmdlet\u3002\u4e5f\u53ef\u4ee5\u4f7f\u7528\u540d\u4e3a\u201cSystem.Diagnostics.EventLog\u201d\u7684.NET Framework\u7c7b\u7684WriteEntry\u65b9\u6cd5\u5c06\u5305\u62ec\u5728$strProcess\u53d8\u91cf\u4e2d\u7684\u7c7b\u8868\u4e2d\u7684\u8fdb\u7a0b\u540d\u79f0\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u6267\u884c\u540e\u5199\u5165\u7684\u65e5\u5fd7\u5982\u56fe3\u6240\u793a\u3002<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image2.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" height=\"454\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image_thumb2.png\" width=\"408\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe3 \u6267\u884c\u540e\u5199\u5165\u7684\u65e5\u5fd7<\/p>\n<p><strong>2 \u641c\u7d22\u4e8b\u4ef6\u65e5\u5fd7<\/strong><\/p>\n<p>\u5c06\u4e8b\u4ef6\u65e5\u5fd7\u5bfc\u51fa\u4e3a\u6587\u672c\u3001XML\u6216\u5176\u4ed6\u683c\u5f0f\u540e\uff0c\u641c\u7d22\u65e5\u5fd7\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u662f\u4f7f\u7528Get-EventLog cmdlet\u3002\u4e0d\u9700\u8981\u5c06\u65e5\u5fd7\u4fdd\u5b58\u4e3a\u5176\u4ed6\u683c\u5f0f\uff0c\u800c\u53ea\u9700\u8981\u7528\u7ba1\u9053\u547d\u4ee4\u5c06\u7ed3\u679c\u4f20\u9012\u7ed9\u5176\u4ed6\u641c\u7d22\u7684cmdlet\u5373\u53ef\uff0c\u672c\u8282\u4ecb\u7ecd\u4e00\u4e9b\u6280\u5de7\uff0c\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cSearchByEventID.ps1\u201d\u7684\u811a\u672c\uff0c\u5176\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>Get-EventLog \u2013LogName system | Where-Object {$_.eventID \u2013eq 1023}<\/p>\n<p>\u8981\u641c\u7d22\u4e8b\u4ef6\u65e5\u5fd7\uff0c\u8fd8\u9700\u8981\u77e5\u9053EventLogEntry\u5bf9\u8c61\u4e2d\u7684\u6210\u5458\u3002\u8be5\u5bf9\u8c61\u662f\u540d\u4e3a\u201cSystem.Diagnostics.EventLogEntry\u201d\u7684\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u6807\u51c6\u7684.NET Framework\u7c7b\u3002\u53ef\u4ee5\u4f7f\u7528Get-Member cmdlet\u68c0\u7d22\u88681\u6240\u793a\u7684System.Diagnostics.EventLogEntry\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<\/p>\n<p>\u88681 System.Diagnostics.EventLogEntry\u5bf9\u8c61\u5c5e\u6027    <\/p>\n<table cellspacing=\"0\" cellpadding=\"0\" border=\"1\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"27%\">\n<p>\u540d \u79f0<\/p>\n<\/td>\n<td valign=\"top\" width=\"72%\">\n<p>\u63cf \u8ff0<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"27%\">\n<p>Category<\/p>\n<\/td>\n<td valign=\"top\" width=\"72%\">\n<p>System.String Category {get;}<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"27%\">\n<p>CategoryNumber<\/p>\n<\/td>\n<td valign=\"top\" width=\"72%\">\n<p>System.Int16 Category {get;}<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"27%\">\n<p>Container<\/p>\n<\/td>\n<td valign=\"top\" width=\"72%\">\n<p>System.ConponementModel.ICotainer Container{get;}<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"27%\">\n<p>Data<\/p>\n<\/td>\n<td valign=\"top\" width=\"72%\">\n<p>System.Byte[] Data {get;}<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"27%\">\n<p>entryType<\/p>\n<\/td>\n<td valign=\"top\" width=\"72%\">\n<p>System.Diagnostics.EventLogEntry EntryType{get;}<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"27%\">\n<p>Index<\/p>\n<\/td>\n<td valign=\"top\" width=\"72%\">\n<p>System.Int32 Index {get;}<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"27%\">\n<p>IndexID<\/p>\n<\/td>\n<td valign=\"top\" width=\"72%\">\n<p>System.Int64 InstanceId {get;}<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"27%\">\n<p>MachineName<\/p>\n<\/td>\n<td valign=\"top\" width=\"72%\">\n<p>System.String MachineName {get;}<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"27%\">\n<p>Message<\/p>\n<\/td>\n<td valign=\"top\" width=\"72%\">\n<p>System.String Message {get;}<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"27%\">\n<p>ReplacementStrings<\/p>\n<\/td>\n<td valign=\"top\" width=\"72%\">\n<p>System.String[] ReplacementStrings {get;}<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"27%\">\n<p>Site<\/p>\n<\/td>\n<td valign=\"top\" width=\"72%\">\n<p>System.String Site {get;}<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"27%\">\n<p>Source<\/p>\n<\/td>\n<td valign=\"top\" width=\"72%\">\n<p>System.String Source {get;}<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"27%\">\n<p>TimeGenerated<\/p>\n<\/td>\n<td valign=\"top\" width=\"72%\">\n<p>System.DateTime TimeGenerated {get;}<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"27%\">\n<p>TimeWritten<\/p>\n<\/td>\n<td valign=\"top\" width=\"72%\">\n<p>System.DateTime TimeWritten {get;}<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"27%\">\n<p>UserName<\/p>\n<\/td>\n<td valign=\"top\" width=\"72%\">\n<p>System.String Source {get;}<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"27%\">\n<p>EventID<\/p>\n<\/td>\n<td valign=\"top\" width=\"72%\">\n<p>System.Object EventID {get=$this.get_EventID() \u2013band 0xFFFF;}<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\uff081\uff09\u7b5b\u9009\u5c5e\u6027<\/p>\n<p>\u5982\u679c\u8981\u51cf\u5c11Get-EventLog cmdlet\u8fd4\u56de\u7684\u4fe1\u606f\u6570\u91cf\uff0c\u53ef\u4ee5\u7528Where-Object\u8fc7\u6ee4\u4e0d\u9700\u8981\u7684\u5bf9\u8c61\uff0c\u4e8b\u4ef6\u65e5\u5fd7\u4e2d\u7684\u5e38\u7528\u7b5b\u9009\u5c5e\u6027\u662fEventID\u3001Source\u3001Security\u53caMessage Text\u3002<\/p>\n<p>\uff082\uff09\u9009\u62e9\u6765\u6e90<\/p>\n<p>\u5982\u679cWindows Installer\uff08Windows\u5b89\u88c5\u8f6f\u4ef6\uff0c\u5305\u62ec\u9002\u7528\u4e8e32 \u4f4dWindows\u64cd\u4f5c\u7cfb\u7edf\u7684Windows Installer\u670d\u52a1\u5668\uff0c\u4ee5\u53ca\u4e00\u4e2a\u7528\u4e8e\u5b58\u50a8\u6709\u5173\u914d\u7f6e\u548c\u5b89\u88c5\u4fe1\u606f\u7684\u65b0\u8f6f\u4ef6\u5305\u6587\u4ef6\u683c\u5f0f\uff09\u51fa\u73b0\u95ee\u9898\uff0c\u5219\u53ef\u4ee5\u5728\u4e8b\u4ef6\u65e5\u5fd7\u4e2d\u67e5\u627e\u6240\u6709\u6765\u6e90\u540d\u4e3a\u201cMsiInstaller\u201d\u7684\u4e8b\u4ef6\u3002\u4e3a\u6b64\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cFindMsiEvent.ps1\u201d\u811a\u672c\u67e5\u627e\u6240\u6709\u6709\u5173Msi\u5b89\u88c5\u4fe1\u606f\u7684\u65e5\u5fd7\uff0c\u4ee5\u5728\u5b89\u88c5\u9519\u8bef\u65f6\u89e3\u51b3\u95ee\u9898\uff0c\u5176\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>Get-EventLog application | where-object {$_.source -like &quot;*Msi*&quot;}<\/p>\n<p>\u6267\u884c\u7ed3\u679c\u5982\u56fe11\u6240\u793a\u3002<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image3.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" height=\"442\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image_thumb3.png\" width=\"673\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe4\u6267\u884c\u7ed3\u679c<\/p>\n<p>\uff083\uff09\u9009\u62e9\u4e25\u91cd\u6027<\/p>\n<p>\u901a\u5e38\u60c5\u51b5\u4e0b\u7528\u6237\u53ef\u80fd\u53ea\u60f3\u770b\u5230\u201c\u9519\u8bef\u201d\u7ea7\u522b\u7684\u4e8b\u4ef6\u65e5\u5fd7\uff0c\u4e3a\u6b64\u521b\u5efa\u4e00\u4e2aGetSystemLogError.ps1\u811a\u672c\u6839\u636e\u4e8b\u4ef6\u7684\u4e25\u91cd\u6027\u7b5b\u9009\u7cfb\u7edf\u65e5\u5fd7\u3002\u5176\u4e2d\u4f7f\u7528$StrType\u53d8\u91cf\u4fdd\u5b58\u8981\u83b7\u53d6\u7684\u4e8b\u4ef6\u65e5\u5fd7\u9879\u7684\u7c7b\u578b\u540d\u79f0\uff0c\u7136\u540e\u4f7f\u7528Get-EventLog cmdlet\u68c0\u7d22\u7cfb\u7edf\u4e8b\u4ef6\u65e5\u5fd7\uff0c\u5e76\u8fd4\u56de\u4e8b\u4ef6\u65e5\u5fd7\u7684\u5bf9\u8c61\u96c6\u5408\u3002\u6700\u540e\u901a\u8fc7\u7ba1\u9053\u4f20\u9012\u7ed9Where-Object cmdlet\u8fc7\u6ee4\uff0c\u8fc7\u6ee4\u4f7f\u7528\u811a\u672c\u4e2d\u7684$_\u53d8\u91cf\u503c\u3002\u4f7f\u7528\u9ed8\u8ba4\u89c6\u56fe\u8f93\u51fa\u8be5\u5bf9\u8c61\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>$strLog = &quot;system&quot;<\/p>\n<p>$strType = &quot;error&quot;<\/p>\n<p>Get-EventLog $strLog | Where-Object {$_.entryType -eq $strType}<\/p>\n<p>\u6267\u884c\u7ed3\u679c\u5982\u56fe5\u6240\u793a\u3002<\/p>\n<p><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image4.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" height=\"442\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image_thumb4.png\" width=\"673\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe5 \u6267\u884c\u7ed3\u679c<\/p>\n<p>\uff084\uff09\u9009\u62e9\u6d88\u606f<\/p>\n<p>\u641c\u7d22\u65e5\u5fd7\u7684\u53e6\u5916\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u65e5\u5fd7\u4e2d\u7684\u6d88\u606f\u5185\u5bb9\uff0c\u53ef\u4ee5\u901a\u8fc7-match\u53c2\u6570\u4f7f\u7528Where-Object cmdlet\u8c03\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<p>\u7528\u7ba1\u9053\u5c06Get-EventLog cmdlet\u751f\u6210\u7684\u7ed3\u679c\u5bf9\u8c61\u4f20\u9012\u7ed9Where-Object cmdlet\uff0c\u4e3a\u6b64\u4f7f\u7528\u81ea\u52a8\u53d8\u91cf$_\uff0c\u5176\u4e2d\u4fdd\u5b58\u9700\u8981\u68c0\u7d22Message\u5c5e\u6027\u7684\u4e8b\u4ef6\u65e5\u5fd7\u9879\u5bf9\u8c61\u3002\u968f\u540e\u4f7f\u7528-match\u53c2\u6570\u914d\u5408Where-Object cmdlet\u67e5\u627e\u5305\u62ec\u5728$strText\u53d8\u91cf\u4e2d\u7684\u5b57\u7b26\u4e32\uff0c\u8fd9\u91cc\u641c\u7d22\u7684\u662f\u6709\u5173Firebird\u6570\u636e\u5e93\u7684\u6240\u6709\u4fe1\u606f\u3002\u4ee5\u4e0b\u662fGetMessage.ps1\u811a\u672c\u7684\u4ee3\u7801\uff1a<\/p>\n<p>$strLog = &quot;System&quot;<\/p>\n<p>$strText = &quot;Firebird&quot;<\/p>\n<p>Get-EventLog -LogName $strLog | Where-Object {$_.message -match $strText}<\/p>\n<p>\u6267\u884c\u7ed3\u679c\u5982\u56fe6\u6240\u793a\u3002<\/p>\n<p><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image5.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" height=\"442\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image_thumb5.png\" width=\"673\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe6 \u6267\u884c\u7ed3\u679c<\/p>\n<p><strong>3 \u603b\u7ed3<\/strong><\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u901a\u8fc7PowerShell\u5bf9Windows Vista\u548cWindows Server 2008\u7684\u4e8b\u4ef6\u65e5\u5fd7\u8fdb\u884c\u7ba1\u7406\u7684\u64cd\u4f5c\uff0c\u5305\u62ec\u4f7f\u7528Get-EventLog cmdlet\u751f\u6210\u53ef\u7528\u7684\u4e8b\u4ef6\u65e5\u5fd7\u6e05\u5355\u3001\u4f7f\u7528\u540c\u4e00\u4e2acmdlet\u5199\u5165\u3001\u641c\u7d22\u4e0d\u540c\u7684\u4e8b\u4ef6\u65e5\u5fd7\u3002<\/p>\n<p>&#160;<\/p>\n<p>\u4f5c\u8005: \u4ed8\u6d77\u519b   <br \/>\u7248\u6743\uff1a\u672c\u6587\u7248\u6743\u5f52\u4f5c\u8005\u6240\u6709    <br \/>\u8f6c\u8f7d\uff1a\u6b22\u8fce\u8f6c\u8f7d\uff0c\u4e3a\u4e86\u4fdd\u5b58\u4f5c\u8005\u7684\u521b\u4f5c\u70ed\u60c5\uff0c\u8bf7\u6309\u8981\u6c42\u3010\u8f6c\u8f7d\u3011\uff0c\u8c22\u8c22    <br \/>\u8981\u6c42\uff1a\u672a\u7ecf\u4f5c\u8005\u540c\u610f\uff0c\u5fc5\u987b\u4fdd\u7559\u6b64\u6bb5\u58f0\u660e\uff1b\u5fc5\u987b\u5728\u6587\u7ae0\u4e2d\u7ed9\u51fa\u539f\u6587\u8fde\u63a5\uff1b\u5426\u5219\u5fc5\u7a76\u6cd5\u5f8b\u8d23\u4efb     <br \/>\u4e2a\u4eba\u7f51\u7ad9: <a href=\"http:\/\/txj.shell.tor.hu\/\">http:\/\/txj.shell.tor.hu\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e8b\u4ef6\u65e5\u5fd7\u662f\u64cd\u4f5c\u7cfb\u7edf\u7528\u6765\u4fdd\u5b58\u672c\u8eab\u53ca\u5176\u4ed6\u7a0b\u5e8f\u4fe1\u606f\u7684\u5de5\u5177\uff0c\u672c\u6587\u5c06\u4f1a\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7PowerShell\u811a\u672c\u9605\u8bfb\u4e8b\u4ef6\u65e5\u5fd7\u5e76 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-33300","post","type-post","status-publish","format-standard","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33300"}],"collection":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=33300"}],"version-history":[{"count":1,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33300\/revisions"}],"predecessor-version":[{"id":33767,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33300\/revisions\/33767"}],"wp:attachment":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}