MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe "PSDiagnostics cmdlets tests." -Tag "CI", "RequireAdminOnWindows" {5 BeforeAll {6 $LogType = 'Analytic'7 $OriginalDefaultParameterValues = $PSDefaultParameterValues.Clone()8 if (-not $IsWindows) {9 $PSDefaultParameterValues["it:skip"] = $true10 }11 else{12 $LogSettingBak = Get-LogProperties -Name PowerShellCore/$LogType13 }14 }15 AfterAll {16 if ($IsWindows) {17 Set-LogProperties -LogDetails $LogSettingBak -Force18 }19 $Global:PSDefaultParameterValues = $OriginalDefaultParameterValues20 }21 22 Context "Test for Enable-PSTrace and Disable-PSTrace cmdlets." {23 It "Should enable $LogType logs for PowerShellCore." {24 [XML]$CurrentSetting = & wevtutil gl PowerShellCore/$LogType /f:xml25 if($CurrentSetting.Channel.Enabled -eq 'true'){26 & wevtutil sl PowerShellCore/$LogType /e:false /q27 }28 29 Enable-PSTrace -Force30 31 [XML]$ExpectedOutput = & wevtutil gl PowerShellCore/$LogType /f:xml32 33 $ExpectedOutput.Channel.enabled | Should -BeExactly 'true'34 }35 36 It "Should disable $LogType logs for PowerShellCore." {37 [XML]$CurrentState = & wevtutil gl PowerShellCore/$LogType /f:xml38 if($CurrentState.channel.enabled -eq 'false'){39 & wevtutil sl PowerShellCore/$LogType /e:true /q40 }41 Disable-PSTrace42 43 [XML]$ExpectedOutput = & wevtutil gl PowerShellCore/$LogType /f:xml44 45 $ExpectedOutput.Channel.enabled | Should -Be 'false'46 }47 }48 49 Context "Test for Get-LogProperties cmdlet." {50 It "Should return properties of $LogType logs for 'PowerShellCore'." {51 [XML]$ExpectedOutput = wevtutil gl PowerShellCore/$LogType /f:xml52 53 $LogProperty = Get-LogProperties -Name PowerShellCore/$LogType54 55 $LogProperty.Name | Should -Be $ExpectedOutput.channel.Name56 $LogProperty.Enabled | Should -Be $ExpectedOutput.channel.Enabled57 $LogProperty.Retention | Should -Be $ExpectedOutput.channel.Logging.Retention58 $LogProperty.AutoBackup | Should -Be $ExpectedOutput.channel.Logging.AutoBackup59 $LogProperty.MaxLogSize | Should -Be $ExpectedOutput.channel.Logging.MaxSize60 61 #Verifying the property count. Adding 2 to count from the wevtutil output as the Enabled and Name property as the count is taken only for Logging property.62 ($LogProperty | Get-Member -MemberType Property | Measure-Object).Count |63 Should -Be (($ExpectedOutput.Channel.Logging | Get-Member -MemberType Property | Measure-Object).Count + 2)64 }65 }66 67 Context "Test for Set-LogProperties cmdlet." {68 BeforeAll {69 if ($IsWindows) {70 [XML]$WevtUtilBefore = wevtutil gl PowerShellCore/$LogType /f:xml71 $LogPropertyToSet = [Microsoft.PowerShell.Diagnostics.LogDetails]::new($WevtUtilBefore.channel.Name,72 [bool]::Parse($WevtUtilBefore.channel.Enabled),73 $LogType,74 [bool]::Parse($WevtUtilBefore.channel.Logging.Retention),75 [bool]::Parse($WevtUtilBefore.channel.Logging.AutoBackup),76 $WevtUtilBefore.channel.Logging.MaxSize -as [int]77 )78 }79 }80 81 It "Should invert AutoBackup setting of $LogType logs for 'PowerShellCore'." {82 $LogPropertyToSet.AutoBackup = -not $LogPropertyToSet.AutoBackup83 Set-LogProperties -LogDetails $LogPropertyToSet -Force84 85 [XML]$ExpectedOutput = & wevtutil gl PowerShellCore/$LogType /f:xml86 (Get-LogProperties -Name PowerShellCore/$LogType).AutoBackup | Should -Be ([bool]::Parse($ExpectedOutput.Channel.Logging.AutoBackup))87 }88 89 It "Should throw exception for invalid LogName." {90 {Set-LogProperties -LogDetails 'Foo' -Force } | Should -Throw -ErrorId 'ParameterArgumentTransformationError'91 }92 }93}94 