{"id":33319,"date":"2011-01-05T19:01:19","date_gmt":"2011-01-05T18:01:19","guid":{"rendered":"https:\/\/www.fuhaijun.com\/?p=33319"},"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%b8%89%e7%ae%a1%e7%90%86%e5%92%8c%e5%88%9b%e5%bb%ba%e4%ba%8b%e4%bb%b6%e6%97%a5%e5%bf%97","status":"publish","type":"post","link":"https:\/\/www.fuhaijun.com\/?p=33319","title":{"rendered":"PowerShell 2.0\u7ba1\u7406\u4e8b\u4ef6\u65e5\u5fd7(\u4e09)\u7ba1\u7406\u548c\u521b\u5efa\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\u7ba1\u7406\u548c\u521b\u5efaWindows\u4e8b\u4ef6\u65e5\u5fd7\u7684\u65b9\u6cd5\u3002<\/p>\n<p><strong>1 \u7ba1\u7406\u4e8b\u4ef6\u65e5\u5fd7<\/strong><\/p>\n<p>\u5728\u4f7f\u7528\u4e8b\u4ef6\u65e5\u5fd7\u65f6\u9700\u8981\u7ba1\u7406\u591a\u4e2a\u7ec4\u4ef6\uff0c\u5176\u4e2d\u6700\u91cd\u8981\u7684\u662f\u4e8b\u4ef6\u65e5\u5fd7\u6587\u4ef6\u7684\u5927\u5c0f\u3002\u4e3a\u4e86\u80fd\u591f\u5305\u62ec\u6240\u9700\u65f6\u95f4\u5185\u7684\u6240\u6709\u7279\u5b9a\u7cfb\u7edf\u4e8b\u4ef6\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u65e5\u5fd7\u6587\u4ef6\u9700\u8981\u8db3\u591f\u5927\uff0c\u4f46\u662f\u8bfb\u53d6\u8fc7\u5927\u7684\u6587\u4ef6\u5c06\u4f1a\u8017\u8d39\u5927\u91cf\u7684\u65f6\u95f4\u3002<\/p>\n<p>\uff081\uff09\u9009\u62e9\u6765\u6e90<\/p>\n<p>\u4f7f\u7528\u4e8b\u4ef6\u65e5\u5fd7\u65f6\u5fc5\u987b\u80fd\u591f\u77e5\u9053\u4f5c\u4e3a\u8bb0\u5f55\u4f7f\u7528\u7684\u65e5\u5fd7\uff0c\u4e3a\u6b64\u9700\u8981\u67e5\u770b\u4e8b\u4ef6\u65e5\u5fd7\u7684\u6e90\u662f\u5426\u5df2\u7ecf\u6ce8\u518c\uff0c\u5b9e\u73b0\u8fd9\u4e2a\u76ee\u7684\u7684\u7b80\u5355\u65b9\u6cd5\u662f\u4f7f\u7528WMI\u7c7bWin32_NtEventLogFile\u3002\u521b\u5efa\u540d\u4e3a\u201cGetLogSources.ps1\u201d\u7684\u811a\u672c\uff0c\u5176\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>$strLog = &quot;Application&quot;<\/p>\n<p>Write-Host &quot;The following sources are registered for the $strLog log: `n&quot;<\/p>\n<p>Get-WmiObject Win32_nteventlogfile -Filter &quot;logfilename like &#8216;%$strLog%&#8217;&quot; |<\/p>\n<p>foreach { $_.sources }<\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u9488\u5bf9\u5e94\u7528\u7a0b\u5e8f\u65e5\u5fd7\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5176\u4ed6\u65e5\u5fd7\u3002\u9996\u5148\u4f7f\u7528Write-Host cmdlet\u8f93\u51fa\u5c06\u8981\u641c\u7d22\u7684\u6807\u9898\u5b57\u7b26\u4e32\uff0c\u901a\u8fc7Get-WmiObject cmdlet\u67e5\u8be2Win32_nteventlogfile WMI\u7c7b\u5e76\u7b5b\u9009\u6765\u6e90\u4fe1\u606f\u4e2d\u5305\u62ec\u4e8b\u4ef6\u65e5\u5fd7\u540d\u79f0\u7684\u4e8b\u4ef6\u3002\u6700\u540e\u4f7f\u7528\u7ba1\u9053\u4f20\u9012\u7ed9ForEach-Object cmdlet\u4ee5\u9010\u4e2a\u8f93\u51fa\u6765\u6e90\u540d\u79f0\uff0c\u6267\u884c\u7ed3\u679c\u5982\u56fe1\u6240\u793a\u3002<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image6.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_thumb6.png\" width=\"673\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe1 \u6267\u884c\u7ed3\u679c<\/p>\n<p>\uff082\uff09\u4fee\u6539\u4e8b\u4ef6\u65e5\u5fd7\u8bbe\u7f6e<\/p>\n<p>\u5b58\u6863\u7b56\u7565\u5bf9\u4e8e\u7cfb\u7edf\u65e5\u5fd7\u5f88\u91cd\u8981\uff0c\u4e0d\u540c\u7b56\u7565\u51b3\u5b9a\u7559\u5b58\u7684\u65e5\u5fd7\u53ca\u5176\u7684\u65f6\u95f4\u3002\u6839\u636e\u7cfb\u7edf\u7684\u7279\u70b9\uff0c\u4e0d\u540c\u65e5\u5fd7\u5206\u7c7b\u7684\u5b58\u6863\u7b56\u7565\u51b3\u5b9a\u7cfb\u7edf\u7684\u7a33\u5b9a\u548c\u5b89\u5168\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u4e8b\u4ef6\u65e5\u5fd7\u7684\u6700\u5927\u503c\uff08\u4ee5KB\u4e3a\u5355\u4f4d\uff09\u3001\u6700\u77ed\u4fdd\u7559\u671f\uff08\u4ee5\u5929\u4e3a\u5355\u4f4d\uff09\uff0c\u4ee5\u53ca\u5176\u4ed6\u5404\u79cd\u7b56\u7565\u90fd\u6709\u76f8\u5e94\u7684\u7559\u5b58\u7b56\u7565\u3002\u5728Windows Vista\u548cWindows 2008\u4e2d\u4e3b\u8981\u6709\u5982\u4e0b3\u4e2a\u53ef\u4ee5\u8bbe\u7f6e\u7684\u5b58\u6863\u7b56\u7565\u3002<\/p>\n<p>\u00d8 DoNotOverwrite\uff1a\u5f53\u4e8b\u4ef6\u65e5\u5fd7\u8fbe\u5230\u9650\u5236\u7684\u6700\u5927\u503c\u65f6\u4fdd\u5b58\u73b0\u6709\u65e5\u5fd7\u9879\uff0c\u6b64\u540e\u7684\u7cfb\u7edf\u65e5\u5fd7\u5c06\u4e0d\u4f1a\u518d\u88ab\u5199\u5165\u800c\u4e22\u5931\u3002<\/p>\n<p>\u00d8 OverwriteNeeded:\uff1a\u5f53\u4e8b\u4ef6\u65e5\u5fd7\u6ee1\u540e\u6dfb\u52a0\u9879\u4f1a\u4f9d\u6b21\u66ff\u6362\u6700\u65e9\u7684\u9879\u76ee\u3002<\/p>\n<p>\u00d8 OverwriteOlder\uff1a\u5f53\u4e8b\u4ef6\u65e5\u5fd7\u6ee1\u540e\u65b0\u7684\u9879\u76ee\u5c06\u4f1a\u8986\u76d6\u6bd4MinimumRetentionDays\u5c5e\u6027\u503c\u5b9a\u4e49\u7684\u65f6\u95f4\u66f4\u65e9\u7684\u9879\u3002\u5982\u679c\u4e8b\u4ef6\u65e5\u5fd7\u5df2\u6ee1\uff0c\u800c\u4e14\u6ca1\u6709\u5176\u4ed6\u9879\u6bd4\u8be5\u5c5e\u6027\u503c\u5b9a\u4e49\u7684\u65f6\u95f4\u65e9\uff0c\u5219\u4e22\u5f03\u65b0\u9879\u3002<\/p>\n<p>\u521b\u5efa\u7528\u4e8e\u67e5\u8be2\u7cfb\u7edf\u65e5\u5fd7\u5b58\u6863\u7b56\u7565\u7684\u811a\u672c\u6587\u4ef6GetEventLogRetentionPolicy.ps1\uff0c\u5176\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>$strLog = &quot;Application&quot;<\/p>\n<p>$objLog = New-Object System.Diagnostics.Eventlog(&quot;$strLog&quot;)<\/p>\n<p>Write-Host `<\/p>\n<p>&quot;<\/p>\n<p>The current settings on the $($objlog.LogDisplayName) file are: <\/p>\n<p>max kilobytes: $($objLog.maximumKiloBYtes)<\/p>\n<p>min retention days: $($objLog.minimumRetentionDays)<\/p>\n<p>overflow policy: $($objLog.overFlowAction)<\/p>\n<p>&quot;<\/p>\n<p>\u5176\u4e2d\u4f7f\u7528New-Object cmdlet\u521b\u5efaSystem.Diagnostics.Eventlog\u7c7b\u7684\u5b9e\u4f8b\u3002\u5728\u521b\u5efa\u65f6\u9700\u8981\u4f20\u9012\u6307\u5b9a\u53c2\u6570\u7528\u4e8e\u786e\u5b9a\u9700\u8981\u5904\u7406\u7684\u4e8b\u4ef6\u65e5\u5fd7\u540d\uff0c\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u65e5\u5fd7\u7684\u5bf9\u8c61\u540e\u4f7f\u7528Write-Host cmdlet\u8f93\u51faLogDisplayName\u3001maximumKiloBYtes\u3001minimumRetentionDays\u53caoverFlowAction\u5c5e\u6027\u3002\u4e3a\u4e86\u907f\u514d\u540e3\u9879\u53ea\u662f\u7b80\u5355\u5730\u8f93\u51fa\u76f8\u5173\u5c5e\u6027\u53ca\u5176\u5bf9\u8c61\u540d\u79f0\uff0c\u4e3a\u6bcf\u4e2a\u53d8\u91cf\u6dfb\u52a0\u7f8e\u5143\u7b26\uff08\uff08$\uff09\u524d\u7f00\u5e76\u7528\u62ec\u53f7\u5206\u9694\uff0c\u5982$($objLog.maximumKiloBYtes)\u3002<\/p>\n<p>\u8be5\u811a\u672c\u7684\u6267\u884c\u7ed3\u679c\u5982\u56fe2\u6240\u793a\u3002<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image7.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_thumb7.png\" width=\"673\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe2\u6267\u884c\u7ed3\u679c<\/p>\n<p>\u66f4\u6539\u4e8b\u4ef6\u65e5\u5fd7\u5b58\u6863\u7b56\u7565\u65f6\u4e5f\u9700\u8981\u4f7f\u7528System.Diagnostics.Eventlog\u7c7b\u6765\u5b9e\u73b0\uff0c\u8fd8\u9700\u8981\u6307\u5b9a\u5c06\u8981\u914d\u7f6e\u7684\u662f\u5b58\u6863\u7b56\u7565\u3002ModifyOverFlowPolicy\u65b9\u6cd5\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff0c\u5373\u7b56\u7565\u540d\u548c\u8981\u4f7f\u7528\u7684\u65e5\u671f\u5929\u6570\u3002\u5728\u8bbe\u7f6eDoNotOverwrite\u6216OverwriteAsNeeded\u65f6\uff0c\u5ffd\u7565\u8be5\u65b9\u6cd5\u8c03\u7528\u7684\u7b2c2\u4e2a\u53c2\u6570\u3002<\/p>\n<p>\u521b\u5efa\u811a\u672cSetEventLogRetentionPolicy.ps1\u4fee\u6539\u65e5\u5fd7\u7684\u5b58\u6863\u7b56\u7565\uff0c\u5176\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>function DisplayLogSettings() <\/p>\n<p>{ <\/p>\n<p>Write-Host `<\/p>\n<p>&quot;<\/p>\n<p>The current settings on the $($objlog.LogDisplayName) file are: <\/p>\n<p>max kilobytes: $($objLog.maximumKiloBYtes)<\/p>\n<p>min retention days: $($objLog.minimumRetentionDays)<\/p>\n<p>overflow policy: $($objLog.overFlowAction)<\/p>\n<p>&quot;<\/p>\n<p>if (!$args) { ChangeLogSettings(&quot;help&quot;) }<\/p>\n<p>}<\/p>\n<p>function ChangeLogSettings($policy) <\/p>\n<p>{ if($policy -ne &quot;help&quot;)<\/p>\n<p>{ <\/p>\n<p>Write-Host -ForegroundColor green &quot;changing log policy &#8230;&quot;<\/p>\n<p>}<\/p>\n<p>switch($policy) <\/p>\n<p>{<\/p>\n<p>&quot;doNotOW&quot; { $objlog.modifyoverflowpolicy(&quot;DoNotOverwrite&quot;,-1) }<\/p>\n<p>&quot;owAsNeeded&quot; { $objlog.modifyoverflowpolicy(&quot;OverwriteAsNeeded&quot;,-1) }<\/p>\n<p>&quot;owOlder&quot; { $objlog.modifyoverflowpolicy(&quot;Overwriteolder&quot;,$intRetention) }<\/p>\n<p>DEFAULT { <\/p>\n<p>Write-Host -ForegroundColor red `<\/p>\n<p>&quot;<\/p>\n<p>You need to specify either of the following: `n<\/p>\n<p>doNotOW &#8211; do not overwrite logs<\/p>\n<p>owAsNeeded &#8211; overwrite as needed<\/p>\n<p>owOlder &#8211; overwrite events older than $intRetention days `n<\/p>\n<p>Example: &gt; SetEventLogRetentionPolicy.ps1 doNotOW<\/p>\n<p>Sets retention policy to Do not Overwrite<\/p>\n<p>Example: &gt; SetEventLogRetentionPolicy.ps1 owAsNeeded<\/p>\n<p>Sets retention policy to Overwrite as needed<\/p>\n<p>Example: &gt; SetEventLogRetentionPolicy.ps1 owOlder<\/p>\n<p>Sets retention policy to Overwrite older than 30 days<\/p>\n<p>Example: &gt; SetEventLogRetentionPolicy.ps1 help<\/p>\n<p>Displays this help message<\/p>\n<p>&quot; <\/p>\n<p>exit }<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>$strLog = &quot;application&quot; #modify for different log<\/p>\n<p>$intRetention = 30 #modify for different number of retention days<\/p>\n<p>$objLog = New-Object system.diagnostics.eventlog(&quot;$strLog&quot;)<\/p>\n<p>DisplayLogSettings($args)<\/p>\n<p>ChangeLogSettings($args)<\/p>\n<p>DisplayLogSettings($args)<\/p>\n<p>\u6ce8\u610f\u5728\u8bbe\u7f6e\u65e5\u5fd7\u7684\u5b58\u6863\u7b56\u7565\u65f6\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\uff1b\u5426\u5219\u4f1a\u51fa\u73b0\u9519\u8bef\uff0c\u8fd9\u662f\u4e3a\u4e86\u4fdd\u62a4\u65e5\u5fd7\u7684\u6709\u6548\u6027\u3002\u5982\u679c\u4f4e\u6743\u9650\u7684\u975e\u6cd5\u7528\u6237\u8bd5\u56fe\u6076\u610f\u4fee\u6539\u65e5\u5fd7\u5b58\u6863\u8bbe\u7f6e\u6765\u9003\u907f\u5bf9\u64cd\u4f5c\u7684\u8ffd\u67e5\uff0c\u5219\u4e0d\u53ef\u80fd\u3002\u5728\u811a\u672c\u4e2d\u521d\u59cb\u5316\u7684\u53d8\u91cf$initRetention\uff0c\u9ed8\u8ba4\u503c\u4e3a30\u610f\u5473\u7740\u4e8b\u4ef6\u65e5\u5fd7\u5b58\u6863\u7b56\u7565\u4f1a\u88ab\u8bbe\u7f6e\u4e3a30\u5929\uff0c\u5f53\u7136\u53ea\u6709\u5728\u8bbe\u7f6eOverwriteOlder\u7b56\u7565\u540e\u624d\u4f1a\u751f\u6548\u3002\u5728\u811a\u672c\u4e2d\u4f7f\u7528$objLog\u53d8\u91cf\u4fdd\u5b58System.Diagnostics.Eventlog\u7c7b\u7684\u5b9e\u4f8b\uff0c\u5e76\u5b9a\u4e49\u8981\u8bbe\u7f6e\u7684\u4e8b\u4ef6\u65e5\u5fd7\u540d\u79f0\u3002\u5728\u521b\u5efa\u5e76\u521d\u59cb\u5316\u8fd9\u4e9b\u5bf9\u8c61\u540e\uff0c\u9700\u8981\u8c03\u7528DisplayLogSettings\u51fd\u6570\u3002\u8be5\u51fd\u6570\u53ef\u4ee5\u4ee5KB\u4e3a\u5355\u4f4d\u663e\u793a\u4e8b\u4ef6\u65e5\u5fd7\u7684\u5927\u5c0f\u4e0a\u9650\uff0c\u4ee5\u5929\u4e3a\u5355\u4f4d\u663e\u793a\u6700\u5c0f\u7559\u5b58\u65f6\u95f4\u53ca\u5176\u8d85\u65f6\u540e\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u5728\u8f93\u51fa\u5f53\u524d\u65f6\u95f4\u5b58\u6863\u8bbe\u7f6e\u540e\u9000\u51fa\u51fd\u6570\uff0c\u5e76\u7ee7\u7eed\u5904\u7406\u811a\u672c\u4e2d\u5269\u4f59\u7684\u4ee3\u7801\u3002\u5982\u679c\u7528\u6237\u5728\u8fd0\u884c\u811a\u672c\u65f6\u672a\u63d0\u4f9b\u76f8\u5173\u7684\u53c2\u6570\uff0c\u5219\u4f7f\u7528help\u53c2\u6570\u8c03\u7528ChangeLogSetting\u51fd\u6570\uff0c\u4ee5\u8f93\u51fa\u8be6\u7ec6\u7684\u5e2e\u52a9\u4fe1\u606f\u5e76\u9000\u51fa\u6574\u4e2a\u811a\u672c\u7684\u6267\u884c\u3002<\/p>\n<p>\u8fd9\u4e2a\u811a\u672c\u7684\u5173\u952e\u90e8\u5206\u662fChangeLogSetting\u51fd\u6570\uff0c\u5b83\u4f7f\u7528switch\u8bed\u53e5\u63a7\u5236\u7a0b\u5e8f\u7684\u903b\u8f91\u3002\u5141\u8bb8\u7528\u6237\u8bbe\u7f6e\u4e8b\u4ef6\u65e5\u5fd7\u5b58\u6863\u7b56\u7565\u7684\u53c2\u6570\uff0c\u800c\u4e0d\u9700\u8981\u8bb0\u5fc6\u590d\u6742\u7684System.Diagnostics.Eventlog\u7c7b\u7684\u8bed\u6cd5\u3002\u8be5switch\u8bed\u53e5\u7684\u8f93\u5165\u53c2\u6570\u4fdd\u5b58\u5728\u914d\u7f6e\u6587\u4ef6$Profile\u4e2d\uff0c\u7528\u6237\u5728\u8f93\u5165\u65f6\u5206\u522b\u5339\u914d\u53c2\u6570-donotw\u3001-owasneeded\u53ca-owolder\uff0c\u5206\u522b\u5bf9\u5e94\u4e8eDoNotOverwrite\u3001OverwriteNeeded\u548cOverwriteOlder\u8fd93\u79cd\u5b58\u6863\u7b56\u7565\u3002\u5982\u679c\u7528\u6237\u7684\u8f93\u5165\u672a\u4e0e\u5176\u4e2d\u7684\u4efb\u4f55\u4e00\u4e2a\u53c2\u6570\u4e00\u81f4\uff0c\u5219\u89e6\u53d1\u9ed8\u8ba4\u64cd\u4f5c\u3002\u8fd9\u91cc\u7684\u9ed8\u8ba4\u64cd\u4f5c\u662f\u7528\u7ea2\u8272\u8f93\u51fa\u8868\u73b0\u7684\u5e2e\u52a9\u4fe1\u606f\uff0c\u8fd9\u6837\u53ef\u4ee5\u5e2e\u52a9\u4e0d\u719f\u6089\u6b64\u811a\u672c\u7684\u7528\u6237\u5728\u6267\u884c\u51fa\u9519\u540e\u6839\u636e\u8f93\u51fa\u6539\u8fdb\u81ea\u5df1\u7684\u8f93\u5165\u3002\u4e0d\u5e26\u53c2\u6570\u76f4\u63a5\u6267\u884c\u811a\u672c\u7684\u6267\u884c\u7ed3\u679c\u5982\u56fe16\u6240\u793a\uff0c\u5c06\u5b58\u6863\u7b56\u7565\u7531OverwriteOlder\u6539\u53d8\u4e3aDonotOverwrite\u540e\u7684\u6267\u884c\u7ed3\u679c\u5982\u56fe3\u6240\u793a\u3002<\/p>\n<p><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image8.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_thumb8.png\" width=\"673\" border=\"0\" \/><\/a> <\/p>\n<p>\u56fe3 \u4e0d\u5e26\u53c2\u6570\u6267\u884c\u811a\u672c\u7684\u7ed3\u679c<\/p>\n<p><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image9.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_thumb9.png\" width=\"673\" border=\"0\" \/><\/a> <\/p>\n<p>\u56fe17 \u5c06\u65e5\u5fd7\u5b58\u6863\u7b56\u7565\u6539\u4e3aDonotOverwrite\u540e\u6267\u884c\u7ed3\u679c<\/p>\n<p>2 \u521b\u5efa\u4e8b\u4ef6\u65e5\u5fd7<\/p>\n<p>\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e8b\u4ef6\u65e5\u5fd7\u6765\u8ddf\u8e2a\u5e38\u89c1\u7684\u64cd\u4f5c\u4ee5\u7ef4\u62a4\u7cfb\u7edf\uff0c\u4e3a\u6b64\u4f7f\u7528System.Diagnostics.EventLog\u7c7b\u7684CreateEventSource\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e8b\u4ef6\u65e5\u5fd7\u7684\u6765\u6e90\u548c\u540d\u79f0\u3002\u867d\u7136\u4e00\u4e2a\u4e8b\u4ef6\u65e5\u5fd7\u53ef\u4ee5\u4fdd\u5b58\u591a\u4e2a\u6765\u6e90\u7684\u4fe1\u606f\uff0c\u4f46\u662f\u4e00\u4e2a\u4e8b\u4ef6\u6765\u6e90\u53ea\u80fd\u5173\u8054\u4e00\u4e2a\u4e8b\u4ef6\u65e5\u5fd7\u3002\u4e3a\u4e86\u907f\u514d\u51fa\u9519\uff0c\u9700\u8981\u4f7f\u7528SourceExist\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u8981\u67e5\u627e\u7684\u6765\u6e90\u540d\u79f0\u3002\u5982\u679c\u6765\u6e90\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u8be5\u6765\u6e90\u548c\u5bf9\u5e94\u7684\u4e8b\u4ef6\u65e5\u5fd7\uff1b\u5982\u679c\u6765\u6e90\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u5e76\u9000\u51fa\u811a\u672c\u3002CreateEventLog.ps1\u811a\u672c\u521b\u5efa\u4e8b\u4ef6\u65e5\u5fd7\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>$source = &quot;ps_script&quot;<\/p>\n<p>$log = &quot;PS_Script_Log&quot;<\/p>\n<p>if(![system.diagnostics.eventlog]::sourceExists($source,&quot;.&quot;))<\/p>\n<p>{ <\/p>\n<p>[system.diagnostics.eventlog]::CreateEventSource($source,$log)<\/p>\n<p>}<\/p>\n<p>ELSE<\/p>\n<p>{ <\/p>\n<p>write-host &quot;$source is already registered with another event Log&quot; <\/p>\n<p>EXIT<\/p>\n<p>}<\/p>\n<p>$strLog = new-object system.diagnostics.eventlog($log,&quot;.&quot;)<\/p>\n<p>$strLog.source = $source<\/p>\n<p>$strLog.writeEntry($strProcess)<\/p>\n<p>\u5982\u679c\u4e8b\u4ef6\u6765\u6e90\u5df2\u7ecf\u6ce8\u518c\u7ed9\u4e0d\u540c\u7684\u4e8b\u4ef6\u65e5\u5fd7\u5e76\u9700\u8981\u8be5\u6765\u6e90\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u4e8b\u4ef6\u65e5\u5fd7\uff0c\u5219\u5fc5\u987b\u9996\u5148\u5220\u9664\u8be5\u4e8b\u4ef6\u6765\u6e90\uff0c\u4e3a\u6b64\u53ef\u4ee5\u4f7f\u7528system.diagnostics.eventlog\u7c7b\u7684DeleteEventSource\u65b9\u6cd5\u3002\u521b\u5efaDelectEventSource.ps1\u811a\u672c\u4f7f\u7528SourceExist\u65b9\u6cd5\u5224\u65ad\u4e8b\u4ef6\u6765\u6e90\u662f\u5426\u5df2\u7ecf\u88ab\u6ce8\u518c\uff0c\u5982\u679c\u5df2\u7ecf\u6ce8\u518c\uff0c\u5219\u4f7f\u7528LogNameFromSourceName\u65b9\u6cd5\u8f93\u51fa\u8be5\u6765\u6e90\u6240\u6307\u5411\u7684\u4e8b\u4ef6\u65e5\u5fd7\u7684\u540d\u79f0\u3002\u7136\u540e\u5220\u9664\u8be5\u4e8b\u4ef6\u6765\u6e90\uff0c\u5e76\u4f20\u9012\u5df2\u5220\u9664\u7684\u6d88\u606f\uff1b\u5982\u679c\u8be5\u4e8b\u4ef6\u6765\u6e90\u5c1a\u672a\u5728\u7cfb\u7edf\u6ce8\u518c\uff0c\u5219\u4f1a\u6536\u5230\u6765\u6e90\u672a\u6ce8\u518c\u7684\u6d88\u606f\u3002\u8be5\u811a\u672c\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>$source = &quot;ps_script&quot;<\/p>\n<p>if([system.diagnostics.eventlog]::sourceExists($source,&quot;.&quot;))<\/p>\n<p>{ <\/p>\n<p>$log = [system.diagnostics.eventlog]::LogNameFromSourceName($source,&quot;.&quot;)<\/p>\n<p>Write-Host &quot;$source is currently registered with $log log.&quot;<\/p>\n<p>Write-Host -ForegroundColor red &quot;$source will be deleted&quot;<\/p>\n<p>[system.diagnostics.eventlog]::DeleteEventSource($source)<\/p>\n<p>}<\/p>\n<p>ELSE<\/p>\n<p>{ Write-Host -ForegroundColor green &quot;$source is not regisered&quot; }<\/p>\n<p>\u6267\u884c\u4ee5\u4e0a\u4e24\u4e2a\u811a\u672c\u540e\u6dfb\u52a0\u7684\u4e8b\u4ef6\u65e5\u5fd7\u53ef\u4ee5\u901a\u8fc7\u7cfb\u7edf\u7684\u4e8b\u4ef6\u67e5\u770b\u5668\uff08\u8fd0\u884ceventvwr.exe\uff09\u770b\u5230\u3002<\/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\u521b\u5efa\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u65e5\u5fd7\u3002<\/p>\n<p>&#160;<\/p>\n<\/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: http:\/\/txj.shell.tor.hu\/<\/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-33319","post","type-post","status-publish","format-standard","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33319"}],"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=33319"}],"version-history":[{"count":1,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33319\/revisions"}],"predecessor-version":[{"id":33766,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33319\/revisions\/33766"}],"wp:attachment":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}