From d51f44c46eb93cdfda5cd58f5ed71d769b9428cd Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sun, 9 Jul 2017 09:44:11 +0200 Subject: [PATCH] Changes to xCluster Resolved Script Analyzer rule warnings by changing Get-WmiObject to Get-CimInstance (issue #49). --- CHANGELOG.md | 2 ++ DSCResources/MSFT_xCluster/MSFT_xCluster.psm1 | 6 ++--- Tests/Unit/MSFT_xCluster.Tests.ps1 | 24 +++++++++---------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24f9454a..361e4116 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,8 @@ (issue #72). - Set-TargetResource now correctly throws and error if domain name cannot be evaluated (issue #71). + - Resolved Script Analyzer rule warnings by changing Get-WmiObject to + Get-CimInstance (issue #49). - Changes to xWaitForCluster - Added example - 1-WaitForFailoverClusterToBePresent.ps1 diff --git a/DSCResources/MSFT_xCluster/MSFT_xCluster.psm1 b/DSCResources/MSFT_xCluster/MSFT_xCluster.psm1 index abf55029..82f7984b 100644 --- a/DSCResources/MSFT_xCluster/MSFT_xCluster.psm1 +++ b/DSCResources/MSFT_xCluster/MSFT_xCluster.psm1 @@ -29,7 +29,7 @@ function Get-TargetResource $DomainAdministratorCredential ) - $computerInformation = Get-WmiObject -Class Win32_ComputerSystem + $computerInformation = Get-CimInstance -ClassName Win32_ComputerSystem if (($null -eq $computerInformation) -or ($null -eq $computerInformation.Domain)) { throw 'Can''t find machine''s domain name' @@ -107,7 +107,7 @@ function Set-TargetResource Write-Verbose -Message "Checking if Cluster $Name is present ..." - $computerInformation = Get-WmiObject -Class Win32_ComputerSystem + $computerInformation = Get-CimInstance -ClassName Win32_ComputerSystem if (($null -eq $computerInformation) -or ($null -eq $computerInformation.Domain)) { throw 'Can''t find machine''s domain name' @@ -228,7 +228,7 @@ function Test-TargetResource Write-Verbose -Message "Checking if Cluster $Name is present ..." - $ComputerInfo = Get-WmiObject -Class Win32_ComputerSystem + $ComputerInfo = Get-CimInstance -ClassName Win32_ComputerSystem if (($null -eq $ComputerInfo) -or ($null -eq $ComputerInfo.Domain)) { throw "Can't find machine's domain name" diff --git a/Tests/Unit/MSFT_xCluster.Tests.ps1 b/Tests/Unit/MSFT_xCluster.Tests.ps1 index 8aa5cd71..a6a7d676 100644 --- a/Tests/Unit/MSFT_xCluster.Tests.ps1 +++ b/Tests/Unit/MSFT_xCluster.Tests.ps1 @@ -45,15 +45,15 @@ try $mockClusterName = 'CLUSTER001' $mockStaticIpAddress = '192.168.10.10' - $mockGetWmiObject = { + $mockGetCimInstance = { return [PSCustomObject] @{ Domain = $mockDynamicDomainName Name = $mockDynamicServerName } } - $mockGetWmiObject_ParameterFilter = { - $Class -eq 'Win32_ComputerSystem' + $mockGetCimInstance_ParameterFilter = { + $ClassName -eq 'Win32_ComputerSystem' } $mockGetCluster = { @@ -108,7 +108,7 @@ try $mockDynamicDomainName = $null $mockDynamicServerName = $mockServerName - Mock -CommandName Get-WmiObject -MockWith $mockGetWmiObject -ParameterFilter $mockGetWmiObject_ParameterFilter -Verifiable + Mock -CommandName Get-CimInstance -MockWith $mockGetCimInstance -ParameterFilter $mockGetCimInstance_ParameterFilter -Verifiable { Get-TargetResource @mockDefaultParameters } | Should -Throw "Can't find machine's domain name" } @@ -120,7 +120,7 @@ try $mockDynamicServerName = $mockServerName Mock -CommandName Get-Cluster -Verifiable - Mock -CommandName Get-WmiObject -MockWith $mockGetWmiObject -ParameterFilter $mockGetWmiObject_ParameterFilter -Verifiable + Mock -CommandName Get-CimInstance -MockWith $mockGetCimInstance -ParameterFilter $mockGetCimInstance_ParameterFilter -Verifiable { Get-TargetResource @mockDefaultParameters } | Should -Throw ("Can't find the cluster {0}" -f $mockClusterName) } @@ -128,7 +128,7 @@ try Context 'When the system is not in the desired state' { BeforeEach { - Mock -CommandName Get-WmiObject -MockWith $mockGetWmiObject -ParameterFilter $mockGetWmiObject_ParameterFilter -Verifiable + Mock -CommandName Get-CimInstance -MockWith $mockGetCimInstance -ParameterFilter $mockGetCimInstance_ParameterFilter -Verifiable Mock -CommandName Get-Cluster -MockWith $mockGetCluster -ParameterFilter $mockGetCluster_ParameterFilter -Verifiable Mock -CommandName Get-ClusterGroup -MockWith $mockGetClusterGroup -ParameterFilter $mockGetClusterGroup_ParameterFilter -Verifiable Mock -CommandName Get-ClusterParameter -MockWith $mockGetClusterParameter -Verifiable @@ -158,7 +158,7 @@ try $mockDynamicDomainName = $null $mockDynamicServerName = $mockServerName - Mock -CommandName Get-WmiObject -MockWith $mockGetWmiObject -ParameterFilter $mockGetWmiObject_ParameterFilter -Verifiable + Mock -CommandName Get-CimInstance -MockWith $mockGetCimInstance -ParameterFilter $mockGetCimInstance_ParameterFilter -Verifiable { Set-TargetResource @mockDefaultParameters } | Should -Throw "Can't find machine's domain name" } @@ -169,7 +169,7 @@ try Mock -CommandName New-Cluster -Verifiable Mock -CommandName Remove-ClusterNode -Verifiable Mock -CommandName Add-ClusterNode -Verifiable - Mock -CommandName Get-WmiObject -MockWith $mockGetWmiObject -ParameterFilter $mockGetWmiObject_ParameterFilter -Verifiable + Mock -CommandName Get-CimInstance -MockWith $mockGetCimInstance -ParameterFilter $mockGetCimInstance_ParameterFilter -Verifiable } $mockDynamicDomainName = $mockDomainName @@ -261,7 +261,7 @@ try Mock -CommandName New-Cluster -Verifiable Mock -CommandName Remove-ClusterNode -Verifiable Mock -CommandName Add-ClusterNode -Verifiable - Mock -CommandName Get-WmiObject -MockWith $mockGetWmiObject -ParameterFilter $mockGetWmiObject_ParameterFilter -Verifiable + Mock -CommandName Get-CimInstance -MockWith $mockGetCimInstance -ParameterFilter $mockGetCimInstance_ParameterFilter -Verifiable Mock -CommandName Get-Cluster -MockWith $mockGetCluster -ParameterFilter $mockGetCluster_ParameterFilter -Verifiable Mock -CommandName Get-ClusterParameter -MockWith $mockGetClusterParameter -Verifiable @@ -301,7 +301,7 @@ try $mockDynamicDomainName = $null $mockDynamicServerName = $mockServerName - Mock -CommandName Get-WmiObject -MockWith $mockGetWmiObject -ParameterFilter $mockGetWmiObject_ParameterFilter -Verifiable + Mock -CommandName Get-CimInstance -MockWith $mockGetCimInstance -ParameterFilter $mockGetCimInstance_ParameterFilter -Verifiable { Test-TargetResource @mockDefaultParameters } | Should -Throw "Can't find machine's domain name" } @@ -309,7 +309,7 @@ try Context 'When the system is not in the desired state' { BeforeEach { - Mock -CommandName Get-WmiObject -MockWith $mockGetWmiObject -ParameterFilter $mockGetWmiObject_ParameterFilter -Verifiable + Mock -CommandName Get-CimInstance -MockWith $mockGetCimInstance -ParameterFilter $mockGetCimInstance_ParameterFilter -Verifiable } $mockDynamicDomainName = $mockDomainName @@ -374,7 +374,7 @@ try Context 'When the system is in the desired state' { BeforeEach { Mock -CommandName Get-ClusterNode -MockWith $mockGetClusterNode -Verifiable - Mock -CommandName Get-WmiObject -MockWith $mockGetWmiObject -ParameterFilter $mockGetWmiObject_ParameterFilter -Verifiable + Mock -CommandName Get-CimInstance -MockWith $mockGetCimInstance -ParameterFilter $mockGetCimInstance_ParameterFilter -Verifiable Mock -CommandName Get-Cluster -MockWith $mockGetCluster -ParameterFilter $mockGetCluster_ParameterFilter -Verifiable }