在不指定Count参数的情况下运行此函数时,它会询问预期的Source,然后不返回Count开关应返回的信息,但是如果我指定Count Switch,它将返回信息I请求。
function Get-EventCounts
{
[CmdletBinding(DefaultParameterSetName = "Count")]
param (
[Parameter(ParameterSetName = "Count", Mandatory = $false)]
[Switch]$Count,
[Parameter(ParameterSetName = "Message", Mandatory = $false)]
[Switch]$Message,
[Parameter(ParameterSetName = "Message", Mandatory = $true)]
[String]$EventID,
[Parameter(ParameterSetName = "Message", Mandatory = $true)]
[Parameter(ParameterSetName = "Count", Mandatory = $true)]
[String]$Source,
[String[]]$DaysBack = ("7", "30", "60")
)
If ($Message)
{
foreach ($int in $DaysBack)
{
$EventList = Get-EventLog System -Source $Source -After (Get-Date).AddDays(- $int) | select EventID, EntryType, TimeGenerated, Message
$MessageReturn = $EventList | Sort-Object -Property EventID | Where-Object -Property EventID -EQ $EventID
}
Return $MessageReturn.Message | Select-Object -Unique
}
If ($Count)
{
foreach ($int in $DaysBack)
{
$EventList = Get-EventLog System -Source $Source -After (Get-Date).AddDays(- $int) | select EventID, EntryType, TimeGenerated, Message
$UniqueID = $EventList.EventID | Sort | Get-Unique
foreach ($Event in $UniqueID)
{
$Counting = $EventList | Where-Object -Property EventID -Like $Event
$EventArray += @(
[PSCustomObject]@{
EventID = $Event; TimeFrame = $int; Rate = $Counting.Count; PossibleMessage = $($($eventlist | where-object { $_.EventID -eq $event }))[0].Message
}
)
}
}
Return $EventArray
}
}
答案 0 :(得分:0)
似乎您正在尝试根据参数集名称而不是使用开关时执行代码。如果是这种情况,那么您可以更改if
语句条件。
function Get-EventCounts
{
[CmdletBinding(DefaultParameterSetName = "Count")]
param (
[Parameter(ParameterSetName = "Count", Mandatory = $false)]
[Switch]$Count,
[Parameter(ParameterSetName = "Message", Mandatory = $false)]
[Switch]$Message,
[Parameter(ParameterSetName = "Message", Mandatory = $true)]
[String]$EventID,
[Parameter(ParameterSetName = "Message", Mandatory = $true)]
[Parameter(ParameterSetName = "Count", Mandatory = $true)]
[String]$Source,
[String[]]$DaysBack = ("7", "30", "60")
)
If ($PSCmdlet.ParameterSetName -eq "Message")
{
foreach ($int in $DaysBack)
{
$EventList = Get-EventLog System -Source $Source -After (Get-Date).AddDays(- $int) | select EventID, EntryType, TimeGenerated, Message
$MessageReturn = $EventList | Sort-Object -Property EventID | Where-Object -Property EventID -EQ $EventID
}
Return $MessageReturn.Message | Select-Object -Unique
}
If ($PSCmdlet.ParameterSetName -eq "Count")
{
foreach ($int in $DaysBack)
{
$EventList = Get-EventLog System -Source $Source -After (Get-Date).AddDays(- $int) | select EventID, EntryType, TimeGenerated, Message
$UniqueID = $EventList.EventID | Sort | Get-Unique
foreach ($Event in $UniqueID)
{
$Counting = $EventList | Where-Object -Property EventID -Like $Event
$EventArray += @(
[PSCustomObject]@{
EventID = $Event; TimeFrame = $int; Rate = $Counting.Count; PossibleMessage = $($($eventlist | where-object { $_.EventID -eq $event }))[0].Message
}
)
}
}
Return $EventArray
}
}
如果您确实要根据这些开关做出决定,则需要为$Count
设置默认值,或者为不存在$Count
的情况添加其他逻辑。