日本好好热aⅴ|国产99视频精品免费观看|日本成人aV在线|久热香蕉国产在线

  • <cite id="ikgdy"><table id="ikgdy"></table></cite>
    1. 西西軟件園多重安全檢測下載網(wǎng)站、值得信賴的軟件下載站!
      軟件
      軟件
      文章
      搜索

      首頁編程開發(fā)其它知識 → PowerShell中怎么通過Get-EventLog cmdlet處理事件日志代碼

      PowerShell中怎么通過Get-EventLog cmdlet處理事件日志代碼

      相關(guān)軟件相關(guān)文章發(fā)表評論 來源:本站整理時間:2010/12/17 8:50:39字體大。A-A+

      作者:佚名點擊:221次評論:0次標簽: PowerShell 事件處理

      • 類型:編程輔助大小:1.7M語言:中文 評分:10.0
      • 標簽:
      立即下載

      管理員能夠獲取信息的主要來源是事件日志,PowerShell中有專門的Get-EventLog cmdlet處理事件日志。為了獲取已存在的事件日志,需要使用-list參數(shù)以返回System.Diagnostics.EventLog類型的對象集合。獲取這些對象后即可實現(xiàn)任何與系統(tǒng)日志相關(guān)聯(lián)的操作,如下所示:

      從下例的輸出能夠看到當前系統(tǒng)中存在的日志條數(shù):

      PS C:\PowerShell\AppendixB> get-eventlog -list

      Max(K) Retain OverflowAction Entries Name

      ------ ------ -------------- ------- ----

      512 7 OverwriteOlder 486 Application

      512 7 OverwriteOlder 0 Internet Explorer

      512 7 OverwriteOlder 1 Security

      512 7 OverwriteOlder 2,166 System

      15,360 0 OverwriteAsNeeded 2,148 Windows PowerShell

      一、獲取特定的事件日志

      首先獲取關(guān)于PowerShell系統(tǒng)日志的日志對象:

      PS C:\PowerShell\AppendixB> $log = get-eventlog -list |

      >> ? { $_.logdisplayname -like "*Pow*" }

      >>

      接下來檢查獲取的日志是否正常:

      PS C:\PowerShell\AppendixB> $log.LogDisplayName

      Windows PowerShell

      隨后查看最近發(fā)生的5條系統(tǒng)日志:

      PS C:\PowerShell\AppendixB> get-eventlog $log.LogDisplayName -newest 5

      Index Time EntryType Source InstanceID Message

      ----- ---- --------- ------ ---------- -------

      2148 九月 20 10:06 Information PowerShell 400 Engine state is changed fro...

      2147 九月 20 10:06 Information PowerShell 600 Provider "Certificate" is S...

      2146 九月 20 10:06 Information PowerShell 600 Provider "Variable" is Star...

      2145 九月 20 10:06 Information PowerShell 600 Provider "Registry" is Star...

      2144 九月 20 10:06 Information PowerShell 600 Provider "Function" is Star...

      查看系統(tǒng)日志最大的容量:

      PS C:\PowerShell\AppendixB> $log.MaximumKilobytes

      15360

      從中能夠看到是15 MB,然后加倍系統(tǒng)允許的最大日志大。

      PS C:\PowerShell\AppendixB> $log.MaximumKilobytes *= 2

      PS C:\PowerShell\AppendixB> $log.MaximumKilobytes

      30720

      二、將事件日志作為實時對象

      EventLog對象的主要特點是其實時性,即一旦獲取這個對象,則可不斷地檢查它,以查看是否發(fā)生了新的事件。例如,可以查看保存在$log變量中的PowerShell日志:

      PS C:\PowerShell\AppendixB> $log

      Max(K) Retain OverflowAction Entries Name

      ------ ------ -------------- ------- ----

      30,720 0 OverwriteAsNeeded 2,148 Windows PowerShell

      能夠看到當前的日志條數(shù)是2 148條。下面增加啟動多個PowerShell實例增加多條日志,這里向PowerShell實例傳遞了exit命令,每個新實例在啟動之后立即退出:

      PS C:\PowerShell\AppendixB> powershell exit

      PS C:\PowerShell\AppendixB> powershell exit

      PS C:\PowerShell\AppendixB> powershell exit

      下面再次查看$log的屬性:

      PS C:\PowerShell\AppendixB> $log

      Max(K) Retain OverflowAction Entries Name

      ------ ------ -------------- ------- ----

      30,720 0 OverwriteAsNeeded 2,187 Windows PowerShell

      可以看到日志中已經(jīng)添加了多條新紀錄。接下來清理已經(jīng)添加的日志,執(zhí)行此操作通過單獨啟動PowerShell實例清除現(xiàn)有PowerShell的日志,命令如下:

      PS C:\PowerShell\AppendixB> powershell {

      >> (get-eventlog -list |

      >> ?{$_.LogDisplayName -like "*Pow*"}).Clear()

      >> }

      >>

      其中的命令傳遞腳本塊給一個新的PowerShell進程,這個腳本塊獲取PowerShell EventLog對象并調(diào)用Clear()方法清除已有的日志,在子進程結(jié)束之后查看當前的日志:

      PS C:\PowerShell\AppendixB> $log

      Max(K) Retain OverflowAction Entries Name

      ------ ------ -------------- ------- ----

      30,720 0 OverwriteAsNeeded 1 Windows PowerShell

      可以看到PowerShell的日志已經(jīng)被清空。

      三、保存事件日志

      可以通過PowerShell的Export-Clixml cmdlet保存事件日志以便于后期處理,導(dǎo)出日志的命令如下所示:

      PS C:\PowerShell\AppendixB> $log.Entries | Export-Clixml c:\log.xml

      這里通過命令將數(shù)據(jù)再次讀出日志:

      PS C:\PowerShell\AppendixB> $date = Import-Clixml C:\log.xml

      為了對比從實時對象讀取的日志,以及從外部讀入的日志的不同,下面輸出實施日志的信息:

      PS C:\PowerShell\AppendixB> $log.Entries[0..3] |

      >> ft -auto Index,Time,EventID,Message

      >>

      Index Time EventID Message

      ----- ---- ------- -------

      1 403 Engine state is changed from Available to StoppeD- ...

      2 600 Provider "WSMan" is StarteD- ...

      3 600 Provider "Alias" is StarteD- ...

      4 600 Provider "Environment" is StarteD- ...

      從外部再次讀入的數(shù)據(jù)記錄如下:

      PS C:\> $data[0..3] |

      >> ft -auto Index,Time,EventID,Message

      >>

      Index Time EventID Message

      ----- ---- ------- -------

      1 403 Engine state is changed from Available to StoppeD- ...

      2 600 Provider "WSMan" is StarteD- ...

      3 600 Provider "Alias" is StarteD- ...

      4 600 Provider "Environment" is StarteD- ...

      兩次輸出的內(nèi)容或多或少相同。當然讀入的數(shù)據(jù)與實時對象有所不同,它不再是實時對象。沒有任何方法,對其屬性的修改也不會反作用于系統(tǒng)。

      Get-MachinesMissingHotfix.ps1腳本的代碼如下所示:

      get-process | foreach {$processes = @{}} {

      $processes[$_.processname] = $_}

      get-service |

      where {$_.Status -match "running" –and

      $_.ServiceType -eq "Win32OwnProcess" } |

      foreach {

      new-object psobject |

      add-member -pass NoteProperty Name $_.Name |

      add-member -pass NoteProperty PID $processes[$_.Name].Id |

      add-member -pass NoteProperty WS $processes[$_.Name].WS |

      add-member -pass NoteProperty Description $_.DisplayName |

      add-member -pass NoteProperty FileName `

      $processes[$_.Name].MainModule.FileName

      } |

      export-csv -notype ./service_datA.csv

      作者: 付海軍
      出處:http://fuhj02.cnblogs.com
      版權(quán):本文版權(quán)歸作者和博客園共有
      轉(zhuǎn)載:歡迎轉(zhuǎn)載,為了保存作者的創(chuàng)作熱情,請按要求【轉(zhuǎn)載】,謝謝

        相關(guān)評論

        閱讀本文后您有什么感想? 已有人給出評價!

        • 8 喜歡喜歡
        • 3 頂
        • 1 難過難過
        • 5 囧
        • 3 圍觀圍觀
        • 2 無聊無聊

        熱門評論

        最新評論

        發(fā)表評論 查看所有評論(0)

        昵稱:
        表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
        字數(shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)