From 4809414af5601fc97d8d0548977ec97f5eda2f5b Mon Sep 17 00:00:00 2001 From: JamesWTruher Date: Wed, 28 Sep 2016 12:21:56 -0700 Subject: [PATCH 1/3] Add new tests for Get-WinEvent --- .../Get-WinEvent.Tests.ps1 | 63 ++++++++++++++++++- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Get-WinEvent.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Get-WinEvent.Tests.ps1 index 0e5b663f4d..6efb35a611 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Get-WinEvent.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Get-WinEvent.Tests.ps1 @@ -1,7 +1,66 @@ Describe 'Get-WinEvent' -Tags "CI" { + BeforeAll { + if ( ! $IsWindows ) + { + $origDefaults = $PSDefaultParameterValues.Clone() + $PSDefaultParameterValues['it:skip'] = $true + } + } + AfterAll { + if ( ! $IsWindows ){ + $global:PSDefaultParameterValues = $origDefaults + } + } + Context "Get-WinEvent ListProvider parameter" { + It 'Get-WinEvent can list the providers' { + $result = Get-WinEvent -listprovider * -erroraction ignore + $result | should not BeNullOrEmpty + } + It 'Get-WinEvent can get a provider by name' { + $providers = Get-WinEvent -listprovider * -erroraction ignore + $result = Get-WinEvent -listprovider ($providers[0].name) + $result | should not BeNullOrEmpty + } + } + Context "Get-WinEvent can retrieve events" { + # for this set of tests we need to have a provider which has multiple events + BeforeAll { + $foundEvents = $false + $providers = Get-WinEvent -listprovider * -erroraction ignore + foreach($provider in $providers) { + $events = Get-WinEvent -provider $provider.name -erroraction ignore + if ( $events.Count -gt 2 ) { + $providerForTests = $provider + $foundEvents = $true + break + } + } + } + It 'Get-WinEvent can get events from a provider' { + # we sample the first 20 results, as this could be very large + $results = Get-WinEvent -provider $providerForTests.Name -max 20 + foreach($event in $results ) { + $event.providername | should be $providerForTests.name + } + } + It 'Get-WinEvent can get events via logname' { + $results = get-winevent -logname $providerForTests.LogLinks.LogName -MaxEvents 10 + $results | should not BeNullOrEmpty + } + It 'Get-WinEvent can use the simplest of filters' { + $filter = @{ ProviderName = $providerForTests.Name } + $testEvents = Get-WinEvent -filterhashtable $filter + $testEvents.Count | should be $events.Count + } + It 'Get-WinEvent can use a filter which includes two items' { + $filter = @{ ProviderName = $providerForTests.Name; Id = $events[0].Id} + $results = Get-WinEvent -filterHashtable $filter + $results | Should not BeNullOrEmpty + } + } # Get-WinEvent works only on windows - It 'can query a System log' -Skip:(-not $IsWindows) { - Get-WinEvent -LogName System -MaxEvents 1 | Should Not Be $null + It 'can query a System log' { + Get-WinEvent -LogName System -MaxEvents 1 | Should Not BeNullOrEmpty } } From f1c854176ddbee419c12a1e2e8ab373af6eacd52 Mon Sep 17 00:00:00 2001 From: JamesWTruher Date: Wed, 28 Sep 2016 14:23:20 -0700 Subject: [PATCH 2/3] Add additional tests for get-winevent for the xpath, xml and hashtable filters Still left to do is handling the non-System queries (such as UserData) --- .../Get-WinEvent.Tests.ps1 | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Get-WinEvent.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Get-WinEvent.Tests.ps1 index 6efb35a611..08757ce47c 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Get-WinEvent.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Get-WinEvent.Tests.ps1 @@ -58,6 +58,20 @@ Describe 'Get-WinEvent' -Tags "CI" { $results = Get-WinEvent -filterHashtable $filter $results | Should not BeNullOrEmpty } + It 'Get-WinEvent can retrieve event via XmlQuery' { + $level = $events[0].Level + $logname = $providerForTests.loglinks.logname + $filter = "" + $results = Get-WinEvent -filterXml $filter -max 3 + $results | should Not BeNullOrEmpty + } + It 'Get-WinEvent can retrieve event via XPath' { + $level = $events[0].Level + $logname = $providerForTests.loglinks.logname + $xpathFilter = "*[System[Level=$level]]" + $results = Get-WinEvent -logname $logname -filterXPath $xpathFilter -max 3 + $results | should Not BeNullOrEmpty + } } # Get-WinEvent works only on windows It 'can query a System log' { From bfac47f3f14a5605499cf35b2a6a98106395435f Mon Sep 17 00:00:00 2001 From: JamesWTruher Date: Wed, 28 Sep 2016 14:29:47 -0700 Subject: [PATCH 3/3] Add return in BeforeAll to avoid running get-winevent cmdlet on non-Windows platforms --- .../Microsoft.PowerShell.Diagnostics/Get-WinEvent.Tests.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Get-WinEvent.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Get-WinEvent.Tests.ps1 index 08757ce47c..8841f7181a 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Get-WinEvent.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Get-WinEvent.Tests.ps1 @@ -26,6 +26,7 @@ Describe 'Get-WinEvent' -Tags "CI" { Context "Get-WinEvent can retrieve events" { # for this set of tests we need to have a provider which has multiple events BeforeAll { + if ( ! $IsWindows ) { return } $foundEvents = $false $providers = Get-WinEvent -listprovider * -erroraction ignore foreach($provider in $providers) {