From 5daa9b3982ae6410e55e2d7a3f5f24a56a2061e9 Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Tue, 8 Oct 2019 13:37:33 -0700 Subject: [PATCH 1/5] add back Get-HotFix cmdlet --- ...soft.PowerShell.Commands.Management.csproj | 3 -- .../commands/management/Hotfix.cs | 36 ++++---------- .../Microsoft.PowerShell.Management.psd1 | 3 +- .../Get-HotFix.Tests.ps1 | 49 +++++++++++++++++++ .../engine/Basic/DefaultCommands.Tests.ps1 | 2 +- 5 files changed, 61 insertions(+), 32 deletions(-) create mode 100644 test/powershell/Modules/Microsoft.PowerShell.Management/Get-HotFix.Tests.ps1 diff --git a/src/Microsoft.PowerShell.Commands.Management/Microsoft.PowerShell.Commands.Management.csproj b/src/Microsoft.PowerShell.Commands.Management/Microsoft.PowerShell.Commands.Management.csproj index af023882fb5..ee25f9cb5d8 100644 --- a/src/Microsoft.PowerShell.Commands.Management/Microsoft.PowerShell.Commands.Management.csproj +++ b/src/Microsoft.PowerShell.Commands.Management/Microsoft.PowerShell.Commands.Management.csproj @@ -22,7 +22,6 @@ - @@ -37,7 +36,6 @@ - @@ -47,7 +45,6 @@ - diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Hotfix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Hotfix.cs index d0f892d3cfe..8a4d6e6f0f5 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Hotfix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Hotfix.cs @@ -2,29 +2,12 @@ // Licensed under the MIT License. using System; -using System.Collections; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Collections.Specialized; -using System.ComponentModel; // Win32Exception using System.Diagnostics.CodeAnalysis; -using System.Diagnostics; // Process class -using System.Globalization; -using System.IO; using System.Management; using System.Management.Automation; using System.Management.Automation.Internal; -using System.Net; -using System.Runtime.Serialization; -using System.Security; -using System.Security.AccessControl; -using System.Security.Permissions; using System.Security.Principal; using System.Text; -using System.Text.RegularExpressions; -using System.Threading; - -using Dbg = System.Management.Automation; namespace Microsoft.PowerShell.Commands { @@ -90,7 +73,15 @@ protected override void BeginProcessing() { bool foundRecord = false; StringBuilder QueryString = new StringBuilder(); - ConnectionOptions conOptions = ComputerWMIHelper.GetConnectionOptions(AuthenticationLevel.Packet, ImpersonationLevel.Impersonate, this.Credential); + ConnectionOptions conOptions = new ConnectionOptions(); + conOptions.Authentication = AuthenticationLevel.Packet; + conOptions.Impersonation = ImpersonationLevel.Impersonate; + if (Credential != null) + { + conOptions.Username = Credential.UserName; + conOptions.SecurePassword = Credential.Password; + } + ManagementScope scope = new ManagementScope(ComputerWMIHelper.GetScopeString(computer, ComputerWMIHelper.WMI_Path_CIM), conOptions); scope.Connect(); if (Id != null) @@ -142,15 +133,6 @@ protected override void BeginProcessing() catch (SystemException) // thrown by SecurityIdentifier.constr { } - // catch (ArgumentException) // thrown (indirectly) by SecurityIdentifier.constr (on XP only?) - // { catch not needed - this is already caught as SystemException - // } - // catch (PlatformNotSupportedException) // thrown (indirectly) by SecurityIdentifier.Translate (on Win95 only?) - // { catch not needed - this is already caught as SystemException - // } - // catch (UnauthorizedAccessException) // thrown (indirectly) by SecurityIdentifier.Translate - // { catch not needed - this is already caught as SystemException - // } } WriteObject(obj); diff --git a/src/Modules/Windows/Microsoft.PowerShell.Management/Microsoft.PowerShell.Management.psd1 b/src/Modules/Windows/Microsoft.PowerShell.Management/Microsoft.PowerShell.Management.psd1 index caea6003a72..e9c7a0a7f0a 100644 --- a/src/Modules/Windows/Microsoft.PowerShell.Management/Microsoft.PowerShell.Management.psd1 +++ b/src/Modules/Windows/Microsoft.PowerShell.Management/Microsoft.PowerShell.Management.psd1 @@ -66,5 +66,6 @@ CmdletsToExport=@("Add-Content", "Rename-Computer", "Get-ComputerInfo", "Get-TimeZone", - "Set-TimeZone") + "Set-TimeZone", + "Get-HotFix") } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-HotFix.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-HotFix.Tests.ps1 new file mode 100644 index 00000000000..76c7b457ce9 --- /dev/null +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-HotFix.Tests.ps1 @@ -0,0 +1,49 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +Describe "Get-HotFix Tests" -Tag CI { + BeforeAll { + $originalDefaultParameterValues = $PSDefaultParameterValues.Clone() + + $skip = $false + if (!$IsWindows) { + $skip = $true + } + else { + $qfe = Get-CimInstance Win32_QuickFixEngineering + if ($qfe.Count -eq 0) { + $skip = $true + } + } + + $PSDefaultParameterValues["it:skip"] = $skip + } + + AfterAll { + $global:PSDefaultParameterValues = $originalDefaultParameterValues + } + + It "Get-HotFix will enumerate all QFEs" { + $hotfix = Get-HotFix + $hotfix.Count | Should -Be $qfe.Count + } + + It "Get-HotFix can filter on -Id" { + $testQfe = $qfe[0] + + $hotfix = Get-HotFix -Id $testQfe.HotFixID + $hotfix.HotFixID | Should -BeExactly $testQfe.HotFixID + $hotfix.Description | Should -BeExactly $testQfe.Description + } + + It "Get-HotFix can filter on -Description" { + $testQfes = $qfe | Where-Object { $_.Description -eq 'Update' } + $hotfixes = Get-HotFix -Description 'Update' + $hotfixes.Count | Should -Be $testQfes.Count + } + + It "Get-HotFix can use -ComputerName" { + $hotfixes = Get-HotFix -ComputerName localhost + $hotfixes.Count | Should -Be $qfe.Count + } +} diff --git a/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 b/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 index f08f9dc54db..8225ff26590 100644 --- a/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 +++ b/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 @@ -279,7 +279,7 @@ Describe "Verify approved aliases list" -Tags "CI" { "Cmdlet", "Get-Help", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None" "Cmdlet", "Get-History", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None" "Cmdlet", "Get-Host", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None" -"Cmdlet", "Get-HotFix", "", $($FullCLR ), "", "", "" +"Cmdlet", "Get-HotFix", "", $($FullCLR -or $CoreWindows ), "", "", "" "Cmdlet", "Get-Item", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None" "Cmdlet", "Get-ItemProperty", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None" "Cmdlet", "Get-ItemPropertyValue", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None" From 008249aa3dd37bd49e35da05c1110688f85e473e Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Tue, 8 Oct 2019 16:54:50 -0700 Subject: [PATCH 2/5] fix confirmimpact test --- test/powershell/engine/Basic/DefaultCommands.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 b/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 index 8225ff26590..b2c69a648fd 100644 --- a/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 +++ b/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 @@ -279,7 +279,7 @@ Describe "Verify approved aliases list" -Tags "CI" { "Cmdlet", "Get-Help", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None" "Cmdlet", "Get-History", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None" "Cmdlet", "Get-Host", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None" -"Cmdlet", "Get-HotFix", "", $($FullCLR -or $CoreWindows ), "", "", "" +"Cmdlet", "Get-HotFix", "", $($FullCLR -or $CoreWindows ), "", "", "None" "Cmdlet", "Get-Item", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None" "Cmdlet", "Get-ItemProperty", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None" "Cmdlet", "Get-ItemPropertyValue", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None" From a35cd30bdc7b054c82460923016c07451599a927 Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Tue, 8 Oct 2019 17:08:32 -0700 Subject: [PATCH 3/5] ifdef code to only build on Windows --- .../commands/management/Hotfix.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Hotfix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Hotfix.cs index 8a4d6e6f0f5..101146332a7 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Hotfix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Hotfix.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +#if !UNIX + using System; using System.Diagnostics.CodeAnalysis; using System.Management; @@ -226,3 +228,5 @@ public void Dispose(bool disposing) } #endregion } + +#endif From c86a9273207c241aa335df96e72445629cce2e90 Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Thu, 10 Oct 2019 09:32:34 -0700 Subject: [PATCH 4/5] address Ilya's feedback --- .../commands/management/Hotfix.cs | 13 ++++++------- .../Get-HotFix.Tests.ps1 | 14 +++++++++++++- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Hotfix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Hotfix.cs index 101146332a7..5175dcc5e6c 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Hotfix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Hotfix.cs @@ -75,14 +75,13 @@ protected override void BeginProcessing() { bool foundRecord = false; StringBuilder QueryString = new StringBuilder(); - ConnectionOptions conOptions = new ConnectionOptions(); - conOptions.Authentication = AuthenticationLevel.Packet; - conOptions.Impersonation = ImpersonationLevel.Impersonate; - if (Credential != null) + ConnectionOptions conOptions = new ConnectionOptions() { - conOptions.Username = Credential.UserName; - conOptions.SecurePassword = Credential.Password; - } + Authentication = AuthenticationLevel.Packet, + Impersonation = ImpersonationLevel.Impersonate, + Username = Credential?.UserName, + SecurePassword = Credential?.Password + }; ManagementScope scope = new ManagementScope(ComputerWMIHelper.GetScopeString(computer, ComputerWMIHelper.WMI_Path_CIM), conOptions); scope.Connect(); diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-HotFix.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-HotFix.Tests.ps1 index 76c7b457ce9..8169ebc50ca 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-HotFix.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-HotFix.Tests.ps1 @@ -10,6 +10,7 @@ Describe "Get-HotFix Tests" -Tag CI { $skip = $true } else { + # skip the tests if there are no hotfixes returned $qfe = Get-CimInstance Win32_QuickFixEngineering if ($qfe.Count -eq 0) { $skip = $true @@ -38,7 +39,18 @@ Describe "Get-HotFix Tests" -Tag CI { It "Get-HotFix can filter on -Description" { $testQfes = $qfe | Where-Object { $_.Description -eq 'Update' } - $hotfixes = Get-HotFix -Description 'Update' + if ($testQfes.Count -gt 0) { + $hotfixes = Get-HotFix -Description 'Update' + } + elseif ($qfe.Count -gt 0) { + $description = $qfe[0].Description + $testQfes = $qfe | Where-Object { $_.Description -eq $description } + $hotfixes = Get-HotFix -Desscription $description + } + + # if no applicable qfes are found on test system, this test will still + # pass as both will have count 0 + $hotfixes.Count | Should -Be $testQfes.Count } From e6ad86af1396ec4ef7e75f8fa4ebc8372fc86943 Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Thu, 10 Oct 2019 12:10:08 -0700 Subject: [PATCH 5/5] address Codacy issue --- .../commands/management/Hotfix.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Hotfix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Hotfix.cs index 5175dcc5e6c..0c72d9ca646 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Hotfix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Hotfix.cs @@ -75,7 +75,7 @@ protected override void BeginProcessing() { bool foundRecord = false; StringBuilder QueryString = new StringBuilder(); - ConnectionOptions conOptions = new ConnectionOptions() + ConnectionOptions conOptions = new ConnectionOptions { Authentication = AuthenticationLevel.Packet, Impersonation = ImpersonationLevel.Impersonate,