使用Powershell Limit-Eventlog设置Windows日志最大大小

时间:2017-10-11 11:08:22

标签: powershell windows-server

旨在编写所有Windows日志的默认大小的增加脚本并更改其他一些属性。用于wevtutil,但无法在2016年使用此功能,因此切换到Powershell的Limit-Eventlog。全新的Windows Server 2016安装最新更新。

从默认日志属性开始:

PS> Get-Eventlog -List

+--------+--------+-------------------+---------+------------------------+
| Max(K) | Retain |  OverflowAction   | Entries |          Log           |
+--------+--------+-------------------+---------+------------------------+
|    300 |      0 | OverwriteAsNeeded |   2,599 | Application            |
| 20,480 |      0 | OverwriteAsNeeded |       0 | HardwareEvents         |
|    512 |      7 | OverwriteAsNeeded |       0 | Internet Explorer      |
| 20,480 |      0 | OverwriteAsNeeded |       0 | Key Management Service |
| 20,480 |      0 | OverwriteAsNeeded |  10,390 | Security               |
| 20,480 |      0 | OverwriteAsNeeded |   3,561 | System                 |
| 15,360 |      0 | OverwriteAsNeeded |     360 | Windows PowerShell     |
+--------+--------+-------------------+---------+------------------------+

一次更改一个日志,没有错误:

PS> Limit-Eventlog -Logname Application -MaximumSize 200MB -OverflowAction OverwriteAsNeeded
PS> Limit-Eventlog -Logname HardwareEvents -MaximumSize 200MB -OverflowAction OverwriteAsNeeded
PS> Limit-Eventlog -Logname "Internet Explorer" -MaximumSize 200MB -OverflowAction OverwriteAsNeeded
PS> Limit-Eventlog -Logname "Key Management Service" -MaximumSize 200MB -OverflowAction OverwriteAsNeeded
PS> Limit-Eventlog -Logname Security -MaximumSize 200MB -OverflowAction OverwriteAsNeeded
PS> Limit-Eventlog -Logname System -MaximumSize 200MB -OverflowAction OverwriteAsNeeded
PS> Limit-Eventlog -Logname "Windows Powershell" -MaximumSize 200MB -OverflowAction OverwriteAsNeeded
PS> Get-Eventlog -List

+---------+--------+-------------------+---------+------------------------+
| Max(K)  | Retain |  OverflowAction   | Entries |          Log           |
+---------+--------+-------------------+---------+------------------------+
| 204,800 |      0 | OverwriteAsNeeded |   2,599 | Application            |
| 204,800 |      0 | OverwriteAsNeeded |       0 | HardwareEvents         |
| 204,800 |      0 | OverwriteAsNeeded |       0 | Internet Explorer      |
| 204,800 |      0 | OverwriteAsNeeded |       0 | Key Management Service |
| 204,800 |      0 | OverwriteAsNeeded |  10,395 | Security               |
| 204,800 |      0 | OverwriteAsNeeded |   3,561 | System                 |
| 204,800 |      0 | OverwriteAsNeeded |     362 | Windows PowerShell     |
+---------+--------+-------------------+---------+------------------------+

我想避免破坏日志名称。从Get-Help Limit-EventLog -example可以看出,ForEach有更好的方法。但是,这样做似乎只将​​Limit-Eventlog应用于第一个日志,而剩下的6个则失败。注意我已稍微更改了值(200MB到100MB),因此很容易看看失败的地方。

$Logs = Get-Eventlog -List | Foreach {$_.log} 
 Limit-Eventlog -Logname $Logs -MaximumSize 100MB -OverflowAction OverwriteAsNeeded 
Get-Eventlog -List

+---------+--------+-------------------+---------+------------------------+
| Max(K)  | Retain |  OverflowAction   | Entries |          Log           |
+---------+--------+-------------------+---------+------------------------+
| 102,400 |      0 | OverwriteAsNeeded |   2,606 | Application            |
| 204,800 |      0 | OverwriteAsNeeded |       0 | HardwareEvents         |
| 204,800 |      0 | OverwriteAsNeeded |       0 | Internet Explorer      |
| 204,800 |      0 | OverwriteAsNeeded |       0 | Key Management Service |
| 204,800 |      0 | OverwriteAsNeeded |  10,399 | Security               |
| 204,800 |      0 | OverwriteAsNeeded |   3,563 | System                 |
| 204,800 |      0 | OverwriteAsNeeded |     369 | Windows PowerShell     |
+---------+--------+-------------------+---------+------------------------+

和6个错误:

Limit-Eventlog : The value supplied for MaximumSize parameter has to be in the range of 64 KB to 4GB with an increment of 64 KB. Please enter a proper 
value and then retry.
At line:2 char:5
+     Limit-Eventlog -Logname $Logs -MaximumSize 100MB -OverflowAction  ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Limit-EventLog], Exception
    + FullyQualifiedErrorId : ValueOutofRange,Microsoft.PowerShell.Commands.LimitEventLogCommand

Limit-Eventlog : The value supplied for MaximumSize parameter has to be in the range of 64 KB to 4GB with an increment of 64 KB. Please enter a proper 
value and then retry.
At line:2 char:5
+     Limit-Eventlog -Logname $Logs -MaximumSize 100MB -OverflowAction  ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Limit-EventLog], Exception
    + FullyQualifiedErrorId : ValueOutofRange,Microsoft.PowerShell.Commands.LimitEventLogCommand

Limit-Eventlog : The value supplied for MaximumSize parameter has to be in the range of 64 KB to 4GB with an increment of 64 KB. Please enter a proper 
value and then retry.
At line:2 char:5
+     Limit-Eventlog -Logname $Logs -MaximumSize 100MB -OverflowAction  ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Limit-EventLog], Exception
    + FullyQualifiedErrorId : ValueOutofRange,Microsoft.PowerShell.Commands.LimitEventLogCommand

Limit-Eventlog : The value supplied for MaximumSize parameter has to be in the range of 64 KB to 4GB with an increment of 64 KB. Please enter a proper 
value and then retry.
At line:2 char:5
+     Limit-Eventlog -Logname $Logs -MaximumSize 100MB -OverflowAction  ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Limit-EventLog], Exception
    + FullyQualifiedErrorId : ValueOutofRange,Microsoft.PowerShell.Commands.LimitEventLogCommand

Limit-Eventlog : The value supplied for MaximumSize parameter has to be in the range of 64 KB to 4GB with an increment of 64 KB. Please enter a proper 
value and then retry.
At line:2 char:5
+     Limit-Eventlog -Logname $Logs -MaximumSize 100MB -OverflowAction  ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Limit-EventLog], Exception
    + FullyQualifiedErrorId : ValueOutofRange,Microsoft.PowerShell.Commands.LimitEventLogCommand

Limit-Eventlog : The value supplied for MaximumSize parameter has to be in the range of 64 KB to 4GB with an increment of 64 KB. Please enter a proper 
value and then retry.
At line:2 char:5
+     Limit-Eventlog -Logname $Logs -MaximumSize 100MB -OverflowAction  ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Limit-EventLog], Exception
    + FullyQualifiedErrorId : ValueOutofRange,Microsoft.PowerShell.Commands.LimitEventLogCommand

1 个答案:

答案 0 :(得分:4)

我尝试了这两种不同的方式,并且都按预期工作......两者都在做同样的事情,只是使用不同的语法。

将一组日志名称传递给Limit-Eventlog

$Logs = Get-Eventlog -List | select -ExpandProperty Log
Limit-Eventlog -Logname $Logs -MaximumSize 0.5Gb -OverflowAction OverwriteAsNeeded -WhatIf

使用foreach将每个日志名称分别传递给Limit-Eventlog

$Logs = Get-Eventlog -List | select -ExpandProperty Log
Foreach ($Log in $Logs) {
    Limit-Eventlog -Logname $Log -MaximumSize 0.5Gb -OverflowAction OverwriteAsNeeded -WhatIf
}

未测试时,您需要删除-WhatIf