diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b914e4..9ee7d07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ - Enabled PSSA rule violations to fail build - Fixes [Issue #27](https://github.com/PlagueHO/iSCSIDsc/issues/27). - Updated tests to meet Pester v4 standard. - Added Open Code of Conduct. +- Simplify Initiator code layout by moving exposed DSC functions into own files +- Added Module manifest +- Changed Get-WMIObject for Get-CimInstance within DSR_iSCSIServerTarget.psm1. - Fixes [Issue #18](https://github.com/PlagueHO/iSCSIDsc/issues/18) ## 1.5.0.0 diff --git a/Modules/iSCSIDsc/DSCResources/DSR_iSCSIInitiator/DSR_iSCSIInitiator.psd1 b/Modules/iSCSIDsc/DSCResources/DSR_iSCSIInitiator/DSR_iSCSIInitiator.psd1 new file mode 100644 index 0000000..eae4ee2 --- /dev/null +++ b/Modules/iSCSIDsc/DSCResources/DSR_iSCSIInitiator/DSR_iSCSIInitiator.psd1 @@ -0,0 +1,48 @@ +# +# Module manifest for module 'DSR_iSCSIInitiator' +# +# Generated by: Someone24 +# +# Generated on: 03/06/2018 +# + +@{ + +# Script module or binary module file associated with this manifest. +RootModule = "$PSScriptRoot\DSR_iSCSIInitiator.psm1" + +# Version number of this module. +ModuleVersion = '1.3.0.0' + +# ID used to uniquely identify this module +GUID = '434ba23b-ba95-4a0e-bd76-f11e8d9eee31' + +# Author of this module +Author = 'PlagueHO' + +# Copyright statement for this module +Copyright = '(c) 2018 PlagueHO. All rights reserved.' + +# Description of the functionality provided by this module +Description = 'Desired State Configuration resource for the management of iSCSI initiator and target configuration' + +# Minimum version of the Windows PowerShell engine required by this module +PowerShellVersion = '5.1' + +# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. +FunctionsToExport = 'Get-TargetResource', 'Set-TargetResource', 'Test-TargetResource' + +# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. +CmdletsToExport = '' + +# Variables to export from this module +VariablesToExport = '' + +# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. +AliasesToExport = '' + +# DSC resources to export from this module +DscResourcesToExport = 'DSR_iSCSIInitiator' + +} + diff --git a/Modules/iSCSIDsc/DSCResources/DSR_iSCSIInitiator/DSR_iSCSIInitiator.psm1 b/Modules/iSCSIDsc/DSCResources/DSR_iSCSIInitiator/DSR_iSCSIInitiator.psm1 index 6c54ec2..5d8fb94 100644 --- a/Modules/iSCSIDsc/DSCResources/DSR_iSCSIInitiator/DSR_iSCSIInitiator.psm1 +++ b/Modules/iSCSIDsc/DSCResources/DSR_iSCSIInitiator/DSR_iSCSIInitiator.psm1 @@ -1,3 +1,6 @@ +# Dot Source DSC Resource Files +Get-ChildItem -Path (Join-Path (Join-Path $PSScriptRoot "Public") "*.ps1") | ForEach-Object { . $_.FullName } + $modulePath = Join-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -ChildPath 'Modules' # Import the Networking Resource Helper Module @@ -10,1136 +13,6 @@ $LocalizedData = Get-LocalizedData ` -ResourceName 'DSR_iSCSIInitiator' ` -ResourcePath (Split-Path -Parent $Script:MyInvocation.MyCommand.Path) -<# - .SYNOPSIS - Returns the current state of the specified iSCSI Initiator. - - .PARAMETER NodeAddress - Represents the IQN of the discovered target. - - .PARAMETER TargetPortalAddress - Represents the IP address or DNS name of the target portal. - - .PARAMETER Ensure - Ensures that Target is Absent or Present. - - .PARAMETER InitiatorPortalAddress - Specifies the IP address associated with the portal. -#> -function Get-TargetResource -{ - [CmdletBinding()] - [OutputType([System.Collections.Hashtable])] - param - ( - [Parameter(Mandatory = $true)] - [ValidateNotNullOrEmpty()] - [System.String] - $NodeAddress, - - [Parameter(Mandatory = $true)] - [ValidateNotNullOrEmpty()] - [System.String] - $TargetPortalAddress, - - [Parameter()] - [ValidateSet('Present', 'Absent')] - [System.String] - $Ensure = 'Present', - - [Parameter()] - [System.String] - $InitiatorPortalAddress - ) - - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.GettingiSCSIInitiatorMessage) ` - -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress - ) -join '' ) - - $returnValue = @{ - NodeAddress = $NodeAddress - TargetPortalAddress = $TargetPortalAddress - InitiatorPortalAddress = $InitiatorPortalAddress - Ensure = 'Absent' - } - - # Lookup the Target Portal - $targetPortal = Get-TargetPortal ` - -TargetPortalAddress $TargetPortalAddress ` - -InitiatorPortalAddress $InitiatorPortalAddress - - if ($targetPortal) - { - $returnValue.TargetPortalAddress = $targetPortal.TargetPortalAddress - $returnValue.InitiatorPortalAddress = $targetPortal.InitiatorPortalAddress - $returnValue.TargetPortalPortNumber = $targetPortal.TargetPortalPortNumber - $returnValue.InitiatorInstanceName = $targetPortal.InitiatorInstanceName - $returnValue.IsDataDigest = $targetPortal.IsDataDigest - $returnValue.IsHeaderDigest = $targetPortal.IsHeaderDigest - - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSITargetPortalExistsMessage) ` - -f $TargetPortalAddress, $InitiatorPortalAddress - ) -join '' ) - } - else - { - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSITargetPortalDoesNotExistMessage) ` - -f $TargetPortalAddress, $InitiatorPortalAddress - ) -join '' ) - } # if - - # Lookup the Target - $target = Get-Target ` - -NodeAddress $NodeAddress - - if ($target) - { - $returnValue.NodeAddress = $target.NodeAddress - $returnValue.IsConnected = $target.IsConnected - - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSITargetExistsMessage) ` - -f $NodeAddress - ) -join '' ) - } - else - { - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSITargetDoesNotExistMessage) ` - -f $NodeAddress - ) -join '' ) - } # if - - # The rest of the properties can only be populated if the Target is connected. - if ($target.IsConnected) - { - # Lookup the Connection - $connection = Get-Connection ` - -Target $Target - - $returnValue.Ensure = 'Present' - - if ($connection) - { - $returnValue.TargetPortalAddress = $connection.TargetAddress - $returnValue.InitiatorPortalAddress = $connection.InitiatorAddress - $returnValue.TargetPortalPortNumber = $connection.TargetPortNumber - $returnValue.ConnectionIdentifier = $connection.ConnectionIdentifier - - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSIConnectionExistsMessage) ` - -f $NodeAddress - ) -join '' ) - } - else - { - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSIConnectionDoesNotExistMessage) ` - -f $NodeAddress - ) -join '' ) - } # if - - # Lookup the Session - $session = Get-Session ` - -Target $target - - if ($session) - { - $returnValue.AuthenticationType = $session.AuthenticationType - $returnValue.InitiatorInstanceName = $session.InitiatorInstanceName - $returnValue.InitiatorPortalAddress = $session.InitiatorPortalAddress - $returnValue.IsConnected = $session.IsConnected - $returnValue.IsDataDigest = $session.IsDataDigest - $returnValue.IsDiscovered = $session.IsDiscovered - $returnValue.IsHeaderDigest = $session.IsHeaderDigest - $returnValue.IsPersistent = $session.IsPersistent - $returnValue.SessionIdentifier = $session.SessionIdentifier - - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSISessionExistsMessage) ` - -f $NodeAddress - ) -join '' ) - } - else - { - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSISessionDoesNotExistMessage) ` - -f $NodeAddress - ) -join '' ) - } # if - } # if - - # Get the iSNS Server - $iSNSServerCurrent = Get-WmiObject ` - -Class MSiSCSIInitiator_iSNSServerClass ` - -Namespace root\wmi - if ($iSNSServerCurrent) - { - $returnValue += @{ - iSNSServer = $iSNSServerCurrent.iSNSServerAddress - } - } - - $returnValue -} # Get-TargetResource - -<# - .SYNOPSIS - Creates, updates or removes an iSCSI Initiator. - - .PARAMETER NodeAddress - Represents the IQN of the discovered target. - - .PARAMETER TargetPortalAddress - Represents the IP address or DNS name of the target portal. - - .PARAMETER Ensure - Ensures that Target is Absent or Present. - - .PARAMETER InitiatorPortalAddress - Specifies the IP address associated with the portal. - - .PARAMETER TargetPortalPortNumber - Specifies the TCP/IP port number for the target portal. - - .PARAMETER InitiatorInstanceName - The name of the initiator instance that the iSCSI initiator service uses to send SendTargets - requests to the target portal. If no instance name is specified, the iSCSI initiator service - chooses the initiator instance. - - .PARAMETER AuthenticationType - Specifies the type of authentication to use when logging into the target. - - .PARAMETER ChapUsername - Specifies the user name to use when establishing a connection authenticated by using Mutual - CHAP. - - .PARAMETER ChapSecret - Specifies the CHAP secret to use when establishing a connection authenticated by using CHAP. - - .PARAMETER IsDataDigest - Enables data digest when the initiator logs into the target portal. - - .PARAMETER IsHeaderDigest - Enables header digest when the initiator logs into the target portal. By not specifying this - parameter, the digest setting is determined by the initiator kernel mode driver. - - .PARAMETER IsMultipathEnabled - Indicates that the initiator has enabled Multipath I/O (MPIO) and it will be used when logging - into the target portal. - - .PARAMETER IsPersistent - Specifies that the session is to be automatically connected after each restart. - - .PARAMETER ReportToPnP - Specifies that the operation is reported to PNP. - - .PARAMETER iSNSServer - Specifies an iSNS Server to register this Initiator with. -#> -function Set-TargetResource -{ - [CmdletBinding()] - param - ( - [Parameter(Mandatory = $true)] - [ValidateNotNullOrEmpty()] - [System.String] - $NodeAddress, - - [Parameter(Mandatory = $true)] - [ValidateNotNullOrEmpty()] - [System.String] - $TargetPortalAddress, - - [Parameter()] - [ValidateSet('Present', 'Absent')] - [System.String] - $Ensure = 'Present', - - [Parameter()] - [System.String] - $InitiatorPortalAddress, - - [Parameter()] - [System.Uint16] - $TargetPortalPortNumber, - - [Parameter()] - [System.String] - $InitiatorInstanceName, - - [Parameter()] - [ValidateSet('None', 'OneWayCHAP', 'MutualCHAP')] - [System.String] - $AuthenticationType, - - [Parameter()] - [System.String] - $ChapUsername, - - [Parameter()] - [System.String] - $ChapSecret, - - [Parameter()] - [System.Boolean] - $IsDataDigest, - - [Parameter()] - [System.Boolean] - $IsHeaderDigest, - - [Parameter()] - [System.Boolean] - $IsMultipathEnabled, - - [Parameter()] - [System.Boolean] - $IsPersistent, - - [Parameter()] - [System.Boolean] - $ReportToPNP, - - [Parameter()] - [System.String] - $iSNSServer - ) - - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.SettingiSCSIInitiatorMessage) ` - -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress - ) -join '' ) - - # Remove any parameters that can't be splatted. - $null = $PSBoundParameters.Remove('Ensure') - - $targetSplat = @{ TargetPortalAddress = $TargetPortalAddress } - if ($PSBoundParameters.ContainsKey('InitiatorPortalAddress')) - { - $targetSplat += @{ InitiatorPortalAddress = $InitiatorPortalAddress } - } - - # Lookup the existing iSCSI Target Portal - $targetPortal = Get-TargetPortal @TargetSplat - - # Get the iSNS Server - $iSNSServerCurrent = Get-WmiObject ` - -Class MSiSCSIInitiator_iSNSServerClass ` - -Namespace root\wmi - - $returnValue += @{ - iSNSServer = $iSNSServerCurrent.iSNSServerAddress - } - - if ($Ensure -eq 'Present') - { - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.EnsureiSCSITargetPortalExistsMessage) ` - -f $TargetPortalAddress, $InitiatorPortalAddress - ) -join '' ) - - [Boolean] $create = $false - - if ($targetPortal) - { - # The iSCSI Target Portal exists - check the parameters - if (($TargetPortalPortNumber) ` - -and ($targetPortal.TargetPortalPortNumber -ne $TargetPortalPortNumber)) - { - $create = $true - } # if - if (($InitiatorInstanceName) ` - -and ($targetPortal.InitiatorInstanceName -ne $InitiatorInstanceName)) - { - $create = $true - } # if - if (($null -ne $IsDataDigest) ` - -and ($targetPortal.IsDataDigest -ne $IsDataDigest)) - { - $create = $true - } # if - if (($null -ne $IsHeaderDigest) ` - -and ($targetPortal.IsHeaderDigest -ne $IsHeaderDigest)) - { - $create = $true - } # if - - if ($create) - { - # The Target Portal exists but has different parameters - # so it has to be removed and recreated - Remove-iSCSITargetPortal @TargetSplat -ErrorAction Stop - - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSITargetPortalRemovedForRecreateMessage) ` - -f $TargetPortalAddress, $InitiatorPortalAddress - ) -join '' ) - } # if - } - else - { - $create = $true - } # if - - if ($create) - { - # Create the iSCSI Target Portal using a splat - [PSObject] $splat = [PSObject]@{} + $PSBoundParameters - $splat.Remove('NodeAddress') - $splat.Remove('IsMultipathEnabled') - $splat.Remove('IsPersistent') - $splat.Remove('ReportToPNP') - $splat.Remove('iSNSServer') - New-iSCSITargetPortal ` - @splat ` - -ErrorAction Stop - - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSITargetPortalCreatedMessage) ` - -f $TargetPortalAddress, $InitiatorPortalAddress - ) -join '' ) - } - - # Lookup the Target - $target = Get-Target ` - -NodeAddress $NodeAddress - - # Check the Target is connected - [Boolean] $connect = $false - - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.EnsureiSCSITargetIsConnectedMessage) ` - -f $NodeAddress - ) -join '' ) - - if ($target) - { - # Lookup the Connection - $connection = Get-Connection ` - -Target $target - - # Lookup the Session - $session = Get-Session ` - -Target $target - - if ($connection -and $session) - { - # Check that the session and connection parameters are correct - - # The Connection.TargetAddress will always be an IP Address - # even if the TargetPortalAddress was specified as a Hostname - try - { - $targetPortalIP = @( - ([System.Net.IPAddress]$TargetPortalAddress).IPAddressToString - ) - } - catch - { - # This is a TargetPortalAddress is a Hostname so resolve it to IP addresses - $targetPortalIP = @( - (Resolve-DNSName -Name $TargetPortalAddress -Type A).IPAddress - ) - } # try - - if ($connection.TargetAddress -notin $targetPortalIP) - { - $connect = $true - } # if - if ($connection.InitiatorAddress -ne $InitiatorPortalAddress) - { - $connect = $true - } # if - if (($TargetPortalPortNumber) ` - -and ($connection.TargetPortNumber -ne $TargetPortalPortNumber)) - { - $connect = $true - } # if - if (($AuthenticationType) ` - -and ($session.AuthenticationType -ne $AuthenticationType)) - { - $connect = $true - } # if - if (($InitiatorInstanceName) ` - -and ($session.InitiatorInstanceName -ne $InitiatorInstanceName)) - { - $connect = $true - } # if - if ($PSBoundParameters.ContainsKey('InitiatorPortalAddress') ` - -and ($session.InitiatorPortalAddress -ne $InitiatorPortalAddress)) - { - $connect = $true - } # if - if (($null -ne $IsDataDigest) ` - -and ($session.IsDataDigest -ne $IsDataDigest)) - { - $connect = $true - } # if - if (($null -ne $IsHeaderDigest) ` - -and ($session.IsHeaderDigest -ne $IsHeaderDigest)) - { - $connect = $true - } # if - - if ($connect) - { - # The Target/Session/Connection has different parameters - # So disconnect everything so it can be reconnected - Disconnect-IscsiTarget ` - -NodeAddress $NodeAddress ` - -Confirm:$False ` - -ErrorAction Stop - - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSITargetDisconnectedMessage) ` - -f $NodeAddress - ) -join '' ) - - } # if - } - else - { - # Either the session or connection doesn't exist - # so reconnect or the target is not connected - $connect = $true - } # if - } - else - { - $connect = $true - } # if - - if ($connect) - { - [PSObject] $splat = [PSObject]@{} + $PSBoundParameters - $splat.Remove('IsMultipathEnabled') - $splat.Remove('iSNSServer') - - $Session = Connect-IscsiTarget ` - @splat ` - -ErrorAction Stop - - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSITargetConnectedMessage) ` - -f $NodeAddress - ) -join '' ) - } # if - - if (($PSBoundParameters.ContainsKey('IsPersistent')) ` - -and ($IsPersistent -ne $session.IsPersistent)) - { - if ($IsPersistent -eq $true) - { - # Ensure session is persistent - $session | Register-IscsiSession ` - -IsMultipathEnabled $IsMultipathEnabled ` - -ErrorAction Stop - - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSISessionSetPersistentMessage) ` - -f $NodeAddress - ) -join '' ) - } - else - { - # Ensure session is not persistent - $session | Unregister-IscsiSession ` - -ErrorAction Stop - - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSISessionRemovedPersistentMessage) ` - -f $NodeAddress - ) -join '' ) - } - } - - # Check the iSNS Server setting - if ($PSBoundParameters.ContainsKey('iSNSServer')) - { - if ([String]::IsNullOrEmpty($iSNSServer)) - { - if ($iSNSServerCurrent) - { - # The iSNS Server is set but should not be - remove it - Remove-WmiObject ` - -Path $iSNSServerCurrent.Path ` - -ErrorAction Stop - - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSNSServerRemovedMessage) - ) -join '' ) - } # if - } - else - { - try - { - Set-WmiInstance ` - -Namespace root\wmi ` - -Class WT_iSNSServer ` - -Arguments @{ServerName = $iSNSServer} ` - -ErrorAction Stop - - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSNSServerUpdatedMessage) ` - -f $iSNSServer - ) -join '' ) - } - catch - { - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSNSServerUpdateErrorMessage) ` - -f $iSNSServer - ) -join '' ) - } - } # if - } # if - } - else - { - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.EnsureiSCSITargetIsDisconnectedMessage) ` - -f $NodeAddress - ) -join '' ) - - # Lookup the Target - $target = Get-Target ` - -NodeAddress $NodeAddress - - if ($target) - { - if ($target.IsConnected) - { - Disconnect-IscsiTarget ` - -NodeAddress $NodeAddress ` - -Confirm:$false ` - -ErrorAction Stop - - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSITargetDisconnectedMessage) ` - -f $NodeAddress - ) -join '' ) - } - } - - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.EnsureiSCSITargetPortalDoesNotExistMessage) ` - -f $TargetPortalAddress, $InitiatorPortalAddress - ) -join '' ) - - if ($targetPortal) - { - # The iSCSI Target Portal shouldn't exist - remove it - Remove-iSCSITargetPortal ` - @TargetSplat ` - -Confirm:$False ` - -ErrorAction Stop - - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSITargetPortalRemovedMessage) ` - -f $TargetPortalAddress, $InitiatorPortalAddress - ) -join '' ) - } # if - - if ($iSNSServerCurrent) - { - # The iSNS Server is set but should not be - remove it - Remove-WmiObject ` - -Path $iSNSServerCurrent.Path ` - -ErrorAction Stop - - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSIServerTargetiSNSRemovedMessage) ` - -f $TargetName - ) -join '' ) - } # if - } # if -} # Set-TargetResource - -<# - .SYNOPSIS - Tests if an iSCSI Initiator needs to be created, updated or removed. - .PARAMETER NodeAddress - Represents the IQN of the discovered target. - - .PARAMETER TargetPortalAddress - Represents the IP address or DNS name of the target portal. - - .PARAMETER Ensure - Ensures that Target is Absent or Present. - - .PARAMETER InitiatorPortalAddress - Specifies the IP address associated with the portal. - - .PARAMETER TargetPortalPortNumber - Specifies the TCP/IP port number for the target portal. - - .PARAMETER InitiatorInstanceName - The name of the initiator instance that the iSCSI initiator service uses to send SendTargets - requests to the target portal. If no instance name is specified, the iSCSI initiator service - chooses the initiator instance. - - .PARAMETER AuthenticationType - Specifies the type of authentication to use when logging into the target. - - .PARAMETER ChapUsername - Specifies the user name to use when establishing a connection authenticated by using Mutual - CHAP. - - .PARAMETER ChapSecret - Specifies the CHAP secret to use when establishing a connection authenticated by using CHAP. - - .PARAMETER IsDataDigest - Enables data digest when the initiator logs into the target portal. - - .PARAMETER IsHeaderDigest - Enables header digest when the initiator logs into the target portal. By not specifying this - parameter, the digest setting is determined by the initiator kernel mode driver. - - .PARAMETER IsMultipathEnabled - Indicates that the initiator has enabled Multipath I/O (MPIO) and it will be used when logging - into the target portal. - - .PARAMETER IsPersistent - Specifies that the session is to be automatically connected after each restart. - - .PARAMETER ReportToPnP - Specifies that the operation is reported to PNP. - - .PARAMETER iSNSServer - Specifies an iSNS Server to register this Initiator with. -#> -function Test-TargetResource -{ - [CmdletBinding()] - [OutputType([System.Boolean])] - param - ( - [Parameter(Mandatory = $true)] - [ValidateNotNullOrEmpty()] - [System.String] - $NodeAddress, - - [Parameter(Mandatory = $true)] - [ValidateNotNullOrEmpty()] - [System.String] - $TargetPortalAddress, - - [Parameter()] - [ValidateSet('Present', 'Absent')] - [System.String] - $Ensure = 'Present', - - [Parameter()] - [System.String] - $InitiatorPortalAddress, - - [Parameter()] - [System.Uint16] - $TargetPortalPortNumber, - - [Parameter()] - [System.String] - $InitiatorInstanceName, - - [Parameter()] - [ValidateSet('None', 'OneWayCHAP', 'MutualCHAP')] - [System.String] - $AuthenticationType, - - [Parameter()] - [System.String] - $ChapUsername, - - [Parameter()] - [System.String] - $ChapSecret, - - [Parameter()] - [System.Boolean] - $IsDataDigest, - - [Parameter()] - [System.Boolean] - $IsHeaderDigest, - - [Parameter()] - [System.Boolean] - $IsMultipathEnabled, - - [Parameter()] - [System.Boolean] - $IsPersistent, - - [Parameter()] - [System.Boolean] - $ReportToPNP, - - [Parameter()] - [System.String] - $iSNSServer - ) - - # Flag to signal whether settings are correct - [Boolean] $desiredConfigurationMatch = $true - - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.TestingiSCSIInitiatorMessage) ` - -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress - ) -join '' ) - - $targetSplat = @{ TargetPortalAddress = $TargetPortalAddress } - if ($PSBoundParameters.ContainsKey('InitiatorPortalAddress')) - { - $targetSplat += @{ InitiatorPortalAddress = $InitiatorPortalAddress } - } - - # Lookup the existing iSCSI Target Portal - $targetPortal = Get-TargetPortal @TargetSplat - - # Get the iSNS Server - $iSNSServerCurrent = Get-WmiObject ` - -Class MSiSCSIInitiator_iSNSServerClass ` - -Namespace root\wmi - - $returnValue += @{ - iSNSServer = $iSNSServerCurrent.iSNSServerAddress - } - - if ($Ensure -eq 'Present') - { - # The iSCSI Target Portal should exist - if ($targetPortal) - { - # The iSCSI Target Portal exists already - check the parameters - if (($TargetPortalPortNumber) ` - -and ($targetPortal.TargetPortalPortNumber -ne $TargetPortalPortNumber)) - { - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` - -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'TargetPortal', 'TargetPortalPortNumber' - ) -join '' ) - $desiredConfigurationMatch = $false - } # if - - if (($InitiatorInstanceName) ` - -and ($targetPortal.InitiatorInstanceName -ne $InitiatorInstanceName)) - { - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` - -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'TargetPortal', 'InitiatorInstanceName' - ) -join '' ) - $desiredConfigurationMatch = $false - } # if - - if (($null -ne $IsDataDigest) ` - -and ($targetPortal.IsDataDigest -ne $IsDataDigest)) - { - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` - -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'TargetPortal', 'IsDataDigest' - ) -join '' ) - $desiredConfigurationMatch = $false - } # if - - if (($null -ne $IsHeaderDigest) ` - -and ($targetPortal.IsHeaderDigest -ne $IsHeaderDigest)) - { - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` - -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'TargetPortal', 'IsHeaderDigest' - ) -join '' ) - $desiredConfigurationMatch = $false - } # if - - # Lookup the Target - $target = Get-Target ` - -NodeAddress $NodeAddress - - if (! $target) - { - # Ths iSCSI Target doesn't exist but should - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSITargetDoesNotExistButShouldMessage) ` - -f $NodeAddress - ) -join '' ) - $desiredConfigurationMatch = $false - return $desiredConfigurationMatch - } # if - - if (-not $target.IsConnected) - { - # Ths iSCSI Target exists but is not connected - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSITargetNotConnectedMessage) ` - -f $NodeAddress - ) -join '' ) - $desiredConfigurationMatch = $false - return $desiredConfigurationMatch - } # if - - # Lookup the Connection - $connection = Get-Connection ` - -Target $target - - if (-not $connection) - { - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSIConnectionDoesNotExistButShouldMessage) ` - -f $NodeAddress - ) -join '' ) - $desiredConfigurationMatch = $false - return $desiredConfigurationMatch - } # if - - <# - Check the Connection parameters are correct - The Connection.TargetAddress will always be an IP Address - even if the TargetPortalAddress was specified as a Hostname - #> - try - { - $targetPortalIP = @( - ([System.Net.IPAddress]$TargetPortalAddress).IPAddressToString - ) - } - catch - { - # This is a TargetPortalAddress is a Hostname so resolve it to IP addresses - $targetPortalIP = @( - (Resolve-DNSName -Name $TargetPortalAddress -Type A).IPAddress - ) - } # try - - if ($connection.TargetAddress -notin $targetPortalIP) - { - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` - -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'Connection', 'TargetAddress' - ) -join '' ) - $desiredConfigurationMatch = $false - } - - if ($PSBoundParameters.ContainsKey('InitiatorPortalAddress') ` - -and ($connection.InitiatorAddress -ne $InitiatorPortalAddress)) - { - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` - -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'Connection', 'InitiatorAddress' - ) -join '' ) - $desiredConfigurationMatch = $false - } # if - - if (($TargetPortalPortNumber) ` - -and ($connection.TargetPortNumber -ne $TargetPortalPortNumber)) - { - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` - -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'Connection', 'TargetPortNumber' - ) -join '' ) - $desiredConfigurationMatch = $false - } # if - - # Lookup the Session - $session = Get-Session ` - -Target $target - - if (-not $session) - { - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSISessionDoesNotExistButShouldMessage) ` - -f $NodeAddress - ) -join '' ) - $desiredConfigurationMatch = $false - return $desiredConfigurationMatch - } # if - - # Check the Session parameters are correct - if (($AuthenticationType) ` - -and ($session.AuthenticationType -ne $AuthenticationType)) - { - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` - -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'Session', 'AuthenticationType' - ) -join '' ) - $desiredConfigurationMatch = $false - } # if - - if (($InitiatorInstanceName) ` - -and ($session.InitiatorInstanceName -ne $InitiatorInstanceName)) - { - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` - -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'Session', 'InitiatorInstanceName' - ) -join '' ) - $desiredConfigurationMatch = $false - } # if - - if ($PSBoundParameters.ContainsKey('InitiatorPortalAddress') ` - -and ($session.InitiatorPortalAddress -ne $InitiatorPortalAddress)) - { - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` - -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'Session', 'InitiatorAddress' - ) -join '' ) - $desiredConfigurationMatch = $false - } # if - - if (($null -ne $IsDataDigest) ` - -and ($session.IsDataDigest -ne $IsDataDigest)) - { - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` - -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'Session', 'IsDataDigest' - ) -join '' ) - $desiredConfigurationMatch = $false - } # if - - if (($null -ne $IsHeaderDigest) ` - -and ($session.IsHeaderDigest -ne $IsHeaderDigest)) - { - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` - -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'Session', 'IsHeaderDigest' - ) -join '' ) - $desiredConfigurationMatch = $false - } # if - - if (($null -ne $IsPersistent) ` - -and ($session.IsPersistent -ne $IsPersistent)) - { - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` - -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'Session', 'IsPersistent' - ) -join '' ) - $desiredConfigurationMatch = $false - } # if - } - else - { - # Ths iSCSI Target Portal doesn't exist but should - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSITargetPortalDoesNotExistButShouldMessage) ` - -f $TargetPortalAddress, $InitiatorPortalAddress - ) -join '' ) - $desiredConfigurationMatch = $false - } # if - - # Check the iSNS Server setting - if ($PSBoundParameters.ContainsKey('iSNSServer') ` - -and ($iSNSServerCurrent.iSNSServerAddress -ne $iSNSServer)) - { - # The iSNS Server is different so needs update - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSNSServerNeedsUpdateMessage) ` - -f $iSNSServerCurrent.iSNSServerAddress, $iSNSServer - ) -join '' ) - $desiredConfigurationMatch = $false - } # if - } - else - { - # Lookup the Target - $target = Get-Target ` - -NodeAddress $NodeAddress - - if ($target.IsConnected) - { - # The iSCSI Target exists and is connected - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSITargetExistsButShouldNotMessage) ` - -f $NodeAddress - ) -join '' ) - $desiredConfigurationMatch = $false - } # if - - # The iSCSI Target Portal should not exist - if ($targetPortal) - { - # The iSCSI Target Portal exists but should not - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSITargetPortalExistsButShouldNotMessage) ` - -f $TargetPortalAddress, $InitiatorPortalAddress - ) -join '' ) - $desiredConfigurationMatch = $false - } - else - { - # The iSCSI Target Portal does not exist and should not - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSCSITargetPortalDoesNotExistAndShouldNotMessage) ` - -f $TargetPortalAddress, $InitiatorPortalAddress - ) -join '' ) - } # if - - # Check the iSNS Server setting - if ($iSNSServerCurrent) - { - # The iSNS Server is set but should not be - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($LocalizedData.iSNSServerIsSetButShouldNotBeMessage) - ) -join '' ) - $desiredConfigurationMatch = $false - } # if - } # if - return $desiredConfigurationMatch -} # Test-TargetResource - # Helper Functions <# .SYNOPSIS @@ -1275,5 +148,3 @@ function Get-Connection } return $Connection } # Get-Connection - -Export-ModuleMember -function *-TargetResource diff --git a/Modules/iSCSIDsc/DSCResources/DSR_iSCSIInitiator/Public/Get-TargetResource.ps1 b/Modules/iSCSIDsc/DSCResources/DSR_iSCSIInitiator/Public/Get-TargetResource.ps1 new file mode 100644 index 0000000..37f3013 --- /dev/null +++ b/Modules/iSCSIDsc/DSCResources/DSR_iSCSIInitiator/Public/Get-TargetResource.ps1 @@ -0,0 +1,185 @@ +<# + .SYNOPSIS + Returns the current state of the specified iSCSI Initiator. + + .PARAMETER NodeAddress + Represents the IQN of the discovered target. + + .PARAMETER TargetPortalAddress + Represents the IP address or DNS name of the target portal. + + .PARAMETER Ensure + Ensures that Target is Absent or Present. + + .PARAMETER InitiatorPortalAddress + Specifies the IP address associated with the portal. +#> + +function Get-TargetResource +{ + [CmdletBinding()] + [OutputType([System.Collections.Hashtable])] + param + ( + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [System.String] + $NodeAddress, + + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [System.String] + $TargetPortalAddress, + + [Parameter()] + [ValidateSet('Present', 'Absent')] + [System.String] + $Ensure = 'Present', + + [Parameter()] + [System.String] + $InitiatorPortalAddress + ) + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.GettingiSCSIInitiatorMessage) ` + -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress + ) -join '' ) + + $returnValue = @{ + NodeAddress = $NodeAddress + TargetPortalAddress = $TargetPortalAddress + InitiatorPortalAddress = $InitiatorPortalAddress + Ensure = 'Absent' + } + + # Lookup the Target Portal + $targetPortal = Get-TargetPortal ` + -TargetPortalAddress $TargetPortalAddress ` + -InitiatorPortalAddress $InitiatorPortalAddress + + if ($targetPortal) + { + $returnValue.TargetPortalAddress = $targetPortal.TargetPortalAddress + $returnValue.InitiatorPortalAddress = $targetPortal.InitiatorPortalAddress + $returnValue.TargetPortalPortNumber = $targetPortal.TargetPortalPortNumber + $returnValue.InitiatorInstanceName = $targetPortal.InitiatorInstanceName + $returnValue.IsDataDigest = $targetPortal.IsDataDigest + $returnValue.IsHeaderDigest = $targetPortal.IsHeaderDigest + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSITargetPortalExistsMessage) ` + -f $TargetPortalAddress, $InitiatorPortalAddress + ) -join '' ) + } + else + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSITargetPortalDoesNotExistMessage) ` + -f $TargetPortalAddress, $InitiatorPortalAddress + ) -join '' ) + } # if + + # Lookup the Target + $target = Get-Target ` + -NodeAddress $NodeAddress + + if ($target) + { + $returnValue.NodeAddress = $target.NodeAddress + $returnValue.IsConnected = $target.IsConnected + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSITargetExistsMessage) ` + -f $NodeAddress + ) -join '' ) + } + else + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSITargetDoesNotExistMessage) ` + -f $NodeAddress + ) -join '' ) + } # if + + # The rest of the properties can only be populated if the Target is connected. + if ($target.IsConnected) + { + # Lookup the Connection + $connection = Get-Connection ` + -Target $Target + + $returnValue.Ensure = 'Present' + + if ($connection) + { + $returnValue.TargetPortalAddress = $connection.TargetAddress + $returnValue.InitiatorPortalAddress = $connection.InitiatorAddress + $returnValue.TargetPortalPortNumber = $connection.TargetPortNumber + $returnValue.ConnectionIdentifier = $connection.ConnectionIdentifier + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSIConnectionExistsMessage) ` + -f $NodeAddress + ) -join '' ) + } + else + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSIConnectionDoesNotExistMessage) ` + -f $NodeAddress + ) -join '' ) + } # if + + # Lookup the Session + $session = Get-Session ` + -Target $target + + if ($session) + { + $returnValue.AuthenticationType = $session.AuthenticationType + $returnValue.InitiatorInstanceName = $session.InitiatorInstanceName + $returnValue.InitiatorPortalAddress = $session.InitiatorPortalAddress + $returnValue.IsConnected = $session.IsConnected + $returnValue.IsDataDigest = $session.IsDataDigest + $returnValue.IsDiscovered = $session.IsDiscovered + $returnValue.IsHeaderDigest = $session.IsHeaderDigest + $returnValue.IsPersistent = $session.IsPersistent + $returnValue.SessionIdentifier = $session.SessionIdentifier + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSISessionExistsMessage) ` + -f $NodeAddress + ) -join '' ) + } + else + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSISessionDoesNotExistMessage) ` + -f $NodeAddress + ) -join '' ) + } # if + } # if + + # Get the iSNS Server + $iSNSServerCurrent = Get-CimInstance ` + -Class MSiSCSIInitiator_iSNSServerClass ` + -Namespace root\wmi + if ($iSNSServerCurrent) + { + $returnValue += @{ + iSNSServer = $iSNSServerCurrent.iSNSServerAddress + } + } + + $returnValue +} # Get-TargetResource diff --git a/Modules/iSCSIDsc/DSCResources/DSR_iSCSIInitiator/Public/Set-TargetResource.ps1 b/Modules/iSCSIDsc/DSCResources/DSR_iSCSIInitiator/Public/Set-TargetResource.ps1 new file mode 100644 index 0000000..9eab6a5 --- /dev/null +++ b/Modules/iSCSIDsc/DSCResources/DSR_iSCSIInitiator/Public/Set-TargetResource.ps1 @@ -0,0 +1,492 @@ +<# + .SYNOPSIS + Creates, updates or removes an iSCSI Initiator. + + .PARAMETER NodeAddress + Represents the IQN of the discovered target. + + .PARAMETER TargetPortalAddress + Represents the IP address or DNS name of the target portal. + + .PARAMETER Ensure + Ensures that Target is Absent or Present. + + .PARAMETER InitiatorPortalAddress + Specifies the IP address associated with the portal. + + .PARAMETER TargetPortalPortNumber + Specifies the TCP/IP port number for the target portal. + + .PARAMETER InitiatorInstanceName + The name of the initiator instance that the iSCSI initiator service uses to send SendTargets + requests to the target portal. If no instance name is specified, the iSCSI initiator service + chooses the initiator instance. + + .PARAMETER AuthenticationType + Specifies the type of authentication to use when logging into the target. + + .PARAMETER ChapUsername + Specifies the user name to use when establishing a connection authenticated by using Mutual + CHAP. + + .PARAMETER ChapSecret + Specifies the CHAP secret to use when establishing a connection authenticated by using CHAP. + + .PARAMETER IsDataDigest + Enables data digest when the initiator logs into the target portal. + + .PARAMETER IsHeaderDigest + Enables header digest when the initiator logs into the target portal. By not specifying this + parameter, the digest setting is determined by the initiator kernel mode driver. + + .PARAMETER IsMultipathEnabled + Indicates that the initiator has enabled Multipath I/O (MPIO) and it will be used when logging + into the target portal. + + .PARAMETER IsPersistent + Specifies that the session is to be automatically connected after each restart. + + .PARAMETER ReportToPnP + Specifies that the operation is reported to PNP. + + .PARAMETER iSNSServer + Specifies an iSNS Server to register this Initiator with. +#> + +function Set-TargetResource +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [System.String] + $NodeAddress, + + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [System.String] + $TargetPortalAddress, + + [Parameter()] + [ValidateSet('Present', 'Absent')] + [System.String] + $Ensure = 'Present', + + [Parameter()] + [System.String] + $InitiatorPortalAddress, + + [Parameter()] + [System.Uint16] + $TargetPortalPortNumber, + + [Parameter()] + [System.String] + $InitiatorInstanceName, + + [Parameter()] + [ValidateSet('None', 'OneWayCHAP', 'MutualCHAP')] + [System.String] + $AuthenticationType, + + [Parameter()] + [System.String] + $ChapUsername, + + [Parameter()] + [System.String] + $ChapSecret, + + [Parameter()] + [System.Boolean] + $IsDataDigest, + + [Parameter()] + [System.Boolean] + $IsHeaderDigest, + + [Parameter()] + [System.Boolean] + $IsMultipathEnabled, + + [Parameter()] + [System.Boolean] + $IsPersistent, + + [Parameter()] + [System.Boolean] + $ReportToPNP, + + [Parameter()] + [System.String] + $iSNSServer + ) + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.SettingiSCSIInitiatorMessage) ` + -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress + ) -join '' ) + + # Remove any parameters that can't be splatted. + $null = $PSBoundParameters.Remove('Ensure') + + $targetSplat = @{ TargetPortalAddress = $TargetPortalAddress } + if ($PSBoundParameters.ContainsKey('InitiatorPortalAddress')) + { + $targetSplat += @{ InitiatorPortalAddress = $InitiatorPortalAddress } + } + + # Lookup the existing iSCSI Target Portal + $targetPortal = Get-TargetPortal @TargetSplat + + # Get the iSNS Server + $iSNSServerCurrent = Get-CimInstance ` + -Class MSiSCSIInitiator_iSNSServerClass ` + -Namespace root\wmi + + $returnValue += @{ + iSNSServer = $iSNSServerCurrent.iSNSServerAddress + } + + if ($Ensure -eq 'Present') + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.EnsureiSCSITargetPortalExistsMessage) ` + -f $TargetPortalAddress, $InitiatorPortalAddress + ) -join '' ) + + [Boolean] $create = $false + + if ($targetPortal) + { + # The iSCSI Target Portal exists - check the parameters + if (($TargetPortalPortNumber) ` + -and ($targetPortal.TargetPortalPortNumber -ne $TargetPortalPortNumber)) + { + $create = $true + } # if + if (($InitiatorInstanceName) ` + -and ($targetPortal.InitiatorInstanceName -ne $InitiatorInstanceName)) + { + $create = $true + } # if + if (($null -ne $IsDataDigest) ` + -and ($targetPortal.IsDataDigest -ne $IsDataDigest)) + { + $create = $true + } # if + if (($null -ne $IsHeaderDigest) ` + -and ($targetPortal.IsHeaderDigest -ne $IsHeaderDigest)) + { + $create = $true + } # if + + if ($create) + { + # The Target Portal exists but has different parameters + # so it has to be removed and recreated + Remove-iSCSITargetPortal @TargetSplat -ErrorAction Stop + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSITargetPortalRemovedForRecreateMessage) ` + -f $TargetPortalAddress, $InitiatorPortalAddress + ) -join '' ) + } # if + } + else + { + $create = $true + } # if + + if ($create) + { + # Create the iSCSI Target Portal using a splat + [PSObject] $splat = [PSObject]@{} + $PSBoundParameters + $splat.Remove('NodeAddress') + $splat.Remove('IsMultipathEnabled') + $splat.Remove('IsPersistent') + $splat.Remove('ReportToPNP') + $splat.Remove('iSNSServer') + New-iSCSITargetPortal ` + @splat ` + -ErrorAction Stop + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSITargetPortalCreatedMessage) ` + -f $TargetPortalAddress, $InitiatorPortalAddress + ) -join '' ) + } + + # Lookup the Target + $target = Get-Target ` + -NodeAddress $NodeAddress + + # Check the Target is connected + [Boolean] $connect = $false + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.EnsureiSCSITargetIsConnectedMessage) ` + -f $NodeAddress + ) -join '' ) + + if ($target) + { + # Lookup the Connection + $connection = Get-Connection ` + -Target $target + + # Lookup the Session + $session = Get-Session ` + -Target $target + + if ($connection -and $session) + { + # Check that the session and connection parameters are correct + + # The Connection.TargetAddress will always be an IP Address + # even if the TargetPortalAddress was specified as a Hostname + try + { + $targetPortalIP = @( + ([System.Net.IPAddress]$TargetPortalAddress).IPAddressToString + ) + } + catch + { + # This is a TargetPortalAddress is a Hostname so resolve it to IP addresses + $targetPortalIP = @( + (Resolve-DNSName -Name $TargetPortalAddress -Type A).IPAddress + ) + } # try + + if ($connection.TargetAddress -notin $targetPortalIP) + { + $connect = $true + } # if + if ($connection.InitiatorAddress -ne $InitiatorPortalAddress) + { + $connect = $true + } # if + if (($TargetPortalPortNumber) ` + -and ($connection.TargetPortNumber -ne $TargetPortalPortNumber)) + { + $connect = $true + } # if + if (($AuthenticationType) ` + -and ($session.AuthenticationType -ne $AuthenticationType)) + { + $connect = $true + } # if + if (($InitiatorInstanceName) ` + -and ($session.InitiatorInstanceName -ne $InitiatorInstanceName)) + { + $connect = $true + } # if + if ($PSBoundParameters.ContainsKey('InitiatorPortalAddress') ` + -and ($session.InitiatorPortalAddress -ne $InitiatorPortalAddress)) + { + $connect = $true + } # if + if (($null -ne $IsDataDigest) ` + -and ($session.IsDataDigest -ne $IsDataDigest)) + { + $connect = $true + } # if + if (($null -ne $IsHeaderDigest) ` + -and ($session.IsHeaderDigest -ne $IsHeaderDigest)) + { + $connect = $true + } # if + + if ($connect) + { + # The Target/Session/Connection has different parameters + # So disconnect everything so it can be reconnected + Disconnect-IscsiTarget ` + -NodeAddress $NodeAddress ` + -Confirm:$False ` + -ErrorAction Stop + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSITargetDisconnectedMessage) ` + -f $NodeAddress + ) -join '' ) + + } # if + } + else + { + # Either the session or connection doesn't exist + # so reconnect or the target is not connected + $connect = $true + } # if + } + else + { + $connect = $true + } # if + + if ($connect) + { + [PSObject] $splat = [PSObject]@{} + $PSBoundParameters + $splat.Remove('IsMultipathEnabled') + $splat.Remove('iSNSServer') + + $Session = Connect-IscsiTarget ` + @splat ` + -ErrorAction Stop + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSITargetConnectedMessage) ` + -f $NodeAddress + ) -join '' ) + } # if + + if (($PSBoundParameters.ContainsKey('IsPersistent')) ` + -and ($IsPersistent -ne $session.IsPersistent)) + { + if ($IsPersistent -eq $true) + { + # Ensure session is persistent + $session | Register-IscsiSession ` + -IsMultipathEnabled $IsMultipathEnabled ` + -ErrorAction Stop + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSISessionSetPersistentMessage) ` + -f $NodeAddress + ) -join '' ) + } + else + { + # Ensure session is not persistent + $session | Unregister-IscsiSession ` + -ErrorAction Stop + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSISessionRemovedPersistentMessage) ` + -f $NodeAddress + ) -join '' ) + } + } + + # Check the iSNS Server setting + if ($PSBoundParameters.ContainsKey('iSNSServer')) + { + if ([String]::IsNullOrEmpty($iSNSServer)) + { + if ($iSNSServerCurrent) + { + # The iSNS Server is set but should not be - remove it + Remove-WmiObject ` + -Path $iSNSServerCurrent.Path ` + -ErrorAction Stop + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSNSServerRemovedMessage) + ) -join '' ) + } # if + } + else + { + try + { + Set-WmiInstance ` + -Namespace root\wmi ` + -Class WT_iSNSServer ` + -Arguments @{ServerName = $iSNSServer} ` + -ErrorAction Stop + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSNSServerUpdatedMessage) ` + -f $iSNSServer + ) -join '' ) + } + catch + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSNSServerUpdateErrorMessage) ` + -f $iSNSServer + ) -join '' ) + } + } # if + } # if + } + else + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.EnsureiSCSITargetIsDisconnectedMessage) ` + -f $NodeAddress + ) -join '' ) + + # Lookup the Target + $target = Get-Target ` + -NodeAddress $NodeAddress + + if ($target) + { + if ($target.IsConnected) + { + Disconnect-IscsiTarget ` + -NodeAddress $NodeAddress ` + -Confirm:$false ` + -ErrorAction Stop + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSITargetDisconnectedMessage) ` + -f $NodeAddress + ) -join '' ) + } + } + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.EnsureiSCSITargetPortalDoesNotExistMessage) ` + -f $TargetPortalAddress, $InitiatorPortalAddress + ) -join '' ) + + if ($targetPortal) + { + # The iSCSI Target Portal shouldn't exist - remove it + Remove-iSCSITargetPortal ` + @TargetSplat ` + -Confirm:$False ` + -ErrorAction Stop + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSITargetPortalRemovedMessage) ` + -f $TargetPortalAddress, $InitiatorPortalAddress + ) -join '' ) + } # if + + if ($iSNSServerCurrent) + { + # The iSNS Server is set but should not be - remove it + Remove-WmiObject ` + -Path $iSNSServerCurrent.Path ` + -ErrorAction Stop + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSIServerTargetiSNSRemovedMessage) ` + -f $TargetName + ) -join '' ) + } # if + } # if +} # Set-TargetResource diff --git a/Modules/iSCSIDsc/DSCResources/DSR_iSCSIInitiator/Public/Test-TargetResource.ps1 b/Modules/iSCSIDsc/DSCResources/DSR_iSCSIInitiator/Public/Test-TargetResource.ps1 new file mode 100644 index 0000000..33dd961 --- /dev/null +++ b/Modules/iSCSIDsc/DSCResources/DSR_iSCSIInitiator/Public/Test-TargetResource.ps1 @@ -0,0 +1,452 @@ +<# + .SYNOPSIS + Tests if an iSCSI Initiator needs to be created, updated or removed. + .PARAMETER NodeAddress + Represents the IQN of the discovered target. + + .PARAMETER TargetPortalAddress + Represents the IP address or DNS name of the target portal. + + .PARAMETER Ensure + Ensures that Target is Absent or Present. + + .PARAMETER InitiatorPortalAddress + Specifies the IP address associated with the portal. + + .PARAMETER TargetPortalPortNumber + Specifies the TCP/IP port number for the target portal. + + .PARAMETER InitiatorInstanceName + The name of the initiator instance that the iSCSI initiator service uses to send SendTargets + requests to the target portal. If no instance name is specified, the iSCSI initiator service + chooses the initiator instance. + + .PARAMETER AuthenticationType + Specifies the type of authentication to use when logging into the target. + + .PARAMETER ChapUsername + Specifies the user name to use when establishing a connection authenticated by using Mutual + CHAP. + + .PARAMETER ChapSecret + Specifies the CHAP secret to use when establishing a connection authenticated by using CHAP. + + .PARAMETER IsDataDigest + Enables data digest when the initiator logs into the target portal. + + .PARAMETER IsHeaderDigest + Enables header digest when the initiator logs into the target portal. By not specifying this + parameter, the digest setting is determined by the initiator kernel mode driver. + + .PARAMETER IsMultipathEnabled + Indicates that the initiator has enabled Multipath I/O (MPIO) and it will be used when logging + into the target portal. + + .PARAMETER IsPersistent + Specifies that the session is to be automatically connected after each restart. + + .PARAMETER ReportToPnP + Specifies that the operation is reported to PNP. + + .PARAMETER iSNSServer + Specifies an iSNS Server to register this Initiator with. +#> +function Test-TargetResource +{ + [CmdletBinding()] + [OutputType([System.Boolean])] + param + ( + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [System.String] + $NodeAddress, + + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [System.String] + $TargetPortalAddress, + + [Parameter()] + [ValidateSet('Present', 'Absent')] + [System.String] + $Ensure = 'Present', + + [Parameter()] + [System.String] + $InitiatorPortalAddress, + + [Parameter()] + [System.Uint16] + $TargetPortalPortNumber, + + [Parameter()] + [System.String] + $InitiatorInstanceName, + + [Parameter()] + [ValidateSet('None', 'OneWayCHAP', 'MutualCHAP')] + [System.String] + $AuthenticationType, + + [Parameter()] + [System.String] + $ChapUsername, + + [Parameter()] + [System.String] + $ChapSecret, + + [Parameter()] + [System.Boolean] + $IsDataDigest, + + [Parameter()] + [System.Boolean] + $IsHeaderDigest, + + [Parameter()] + [System.Boolean] + $IsMultipathEnabled, + + [Parameter()] + [System.Boolean] + $IsPersistent, + + [Parameter()] + [System.Boolean] + $ReportToPNP, + + [Parameter()] + [System.String] + $iSNSServer + ) + + # Flag to signal whether settings are correct + [Boolean] $desiredConfigurationMatch = $true + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.TestingiSCSIInitiatorMessage) ` + -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress + ) -join '' ) + + $targetSplat = @{ TargetPortalAddress = $TargetPortalAddress } + if ($PSBoundParameters.ContainsKey('InitiatorPortalAddress')) + { + $targetSplat += @{ InitiatorPortalAddress = $InitiatorPortalAddress } + } + + # Lookup the existing iSCSI Target Portal + $targetPortal = Get-TargetPortal @TargetSplat + + # Get the iSNS Server + $iSNSServerCurrent = Get-CimInstance ` + -Class MSiSCSIInitiator_iSNSServerClass ` + -Namespace root\wmi + + $returnValue += @{ + iSNSServer = $iSNSServerCurrent.iSNSServerAddress + } + + if ($Ensure -eq 'Present') + { + # The iSCSI Target Portal should exist + if ($targetPortal) + { + # The iSCSI Target Portal exists already - check the parameters + if (($TargetPortalPortNumber) ` + -and ($targetPortal.TargetPortalPortNumber -ne $TargetPortalPortNumber)) + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` + -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'TargetPortal', 'TargetPortalPortNumber' + ) -join '' ) + $desiredConfigurationMatch = $false + } # if + + if (($InitiatorInstanceName) ` + -and ($targetPortal.InitiatorInstanceName -ne $InitiatorInstanceName)) + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` + -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'TargetPortal', 'InitiatorInstanceName' + ) -join '' ) + $desiredConfigurationMatch = $false + } # if + + if (($null -ne $IsDataDigest) ` + -and ($targetPortal.IsDataDigest -ne $IsDataDigest)) + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` + -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'TargetPortal', 'IsDataDigest' + ) -join '' ) + $desiredConfigurationMatch = $false + } # if + + if (($null -ne $IsHeaderDigest) ` + -and ($targetPortal.IsHeaderDigest -ne $IsHeaderDigest)) + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` + -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'TargetPortal', 'IsHeaderDigest' + ) -join '' ) + $desiredConfigurationMatch = $false + } # if + + # Lookup the Target + $target = Get-Target ` + -NodeAddress $NodeAddress + + if (! $target) + { + # Ths iSCSI Target doesn't exist but should + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSITargetDoesNotExistButShouldMessage) ` + -f $NodeAddress + ) -join '' ) + $desiredConfigurationMatch = $false + return $desiredConfigurationMatch + } # if + + if (-not $target.IsConnected) + { + # Ths iSCSI Target exists but is not connected + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSITargetNotConnectedMessage) ` + -f $NodeAddress + ) -join '' ) + $desiredConfigurationMatch = $false + return $desiredConfigurationMatch + } # if + + # Lookup the Connection + $connection = Get-Connection ` + -Target $target + + if (-not $connection) + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSIConnectionDoesNotExistButShouldMessage) ` + -f $NodeAddress + ) -join '' ) + $desiredConfigurationMatch = $false + return $desiredConfigurationMatch + } # if + + <# + Check the Connection parameters are correct + The Connection.TargetAddress will always be an IP Address + even if the TargetPortalAddress was specified as a Hostname + #> + try + { + $targetPortalIP = @( + ([System.Net.IPAddress]$TargetPortalAddress).IPAddressToString + ) + } + catch + { + # This is a TargetPortalAddress is a Hostname so resolve it to IP addresses + $targetPortalIP = @( + (Resolve-DNSName -Name $TargetPortalAddress -Type A).IPAddress + ) + } # try + + if ($connection.TargetAddress -notin $targetPortalIP) + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` + -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'Connection', 'TargetAddress' + ) -join '' ) + $desiredConfigurationMatch = $false + } + + if ($PSBoundParameters.ContainsKey('InitiatorPortalAddress') ` + -and ($connection.InitiatorAddress -ne $InitiatorPortalAddress)) + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` + -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'Connection', 'InitiatorAddress' + ) -join '' ) + $desiredConfigurationMatch = $false + } # if + + if (($TargetPortalPortNumber) ` + -and ($connection.TargetPortNumber -ne $TargetPortalPortNumber)) + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` + -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'Connection', 'TargetPortNumber' + ) -join '' ) + $desiredConfigurationMatch = $false + } # if + + # Lookup the Session + $session = Get-Session ` + -Target $target + + if (-not $session) + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSISessionDoesNotExistButShouldMessage) ` + -f $NodeAddress + ) -join '' ) + $desiredConfigurationMatch = $false + return $desiredConfigurationMatch + } # if + + # Check the Session parameters are correct + if (($AuthenticationType) ` + -and ($session.AuthenticationType -ne $AuthenticationType)) + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` + -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'Session', 'AuthenticationType' + ) -join '' ) + $desiredConfigurationMatch = $false + } # if + + if (($InitiatorInstanceName) ` + -and ($session.InitiatorInstanceName -ne $InitiatorInstanceName)) + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` + -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'Session', 'InitiatorInstanceName' + ) -join '' ) + $desiredConfigurationMatch = $false + } # if + + if ($PSBoundParameters.ContainsKey('InitiatorPortalAddress') ` + -and ($session.InitiatorPortalAddress -ne $InitiatorPortalAddress)) + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` + -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'Session', 'InitiatorAddress' + ) -join '' ) + $desiredConfigurationMatch = $false + } # if + + if (($null -ne $IsDataDigest) ` + -and ($session.IsDataDigest -ne $IsDataDigest)) + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` + -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'Session', 'IsDataDigest' + ) -join '' ) + $desiredConfigurationMatch = $false + } # if + + if (($null -ne $IsHeaderDigest) ` + -and ($session.IsHeaderDigest -ne $IsHeaderDigest)) + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` + -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'Session', 'IsHeaderDigest' + ) -join '' ) + $desiredConfigurationMatch = $false + } # if + + if (($null -ne $IsPersistent) ` + -and ($session.IsPersistent -ne $IsPersistent)) + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSIInitiatorParameterNeedsUpdateMessage) ` + -f $NodeAddress, $TargetPortalAddress, $InitiatorPortalAddress, 'Session', 'IsPersistent' + ) -join '' ) + $desiredConfigurationMatch = $false + } # if + } + else + { + # Ths iSCSI Target Portal doesn't exist but should + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSITargetPortalDoesNotExistButShouldMessage) ` + -f $TargetPortalAddress, $InitiatorPortalAddress + ) -join '' ) + $desiredConfigurationMatch = $false + } # if + + # Check the iSNS Server setting + if ($PSBoundParameters.ContainsKey('iSNSServer') ` + -and ($iSNSServerCurrent.iSNSServerAddress -ne $iSNSServer)) + { + # The iSNS Server is different so needs update + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSNSServerNeedsUpdateMessage) ` + -f $iSNSServerCurrent.iSNSServerAddress, $iSNSServer + ) -join '' ) + $desiredConfigurationMatch = $false + } # if + } + else + { + # Lookup the Target + $target = Get-Target ` + -NodeAddress $NodeAddress + + if ($target.IsConnected) + { + # The iSCSI Target exists and is connected + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSITargetExistsButShouldNotMessage) ` + -f $NodeAddress + ) -join '' ) + $desiredConfigurationMatch = $false + } # if + + # The iSCSI Target Portal should not exist + if ($targetPortal) + { + # The iSCSI Target Portal exists but should not + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSITargetPortalExistsButShouldNotMessage) ` + -f $TargetPortalAddress, $InitiatorPortalAddress + ) -join '' ) + $desiredConfigurationMatch = $false + } + else + { + # The iSCSI Target Portal does not exist and should not + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSCSITargetPortalDoesNotExistAndShouldNotMessage) ` + -f $TargetPortalAddress, $InitiatorPortalAddress + ) -join '' ) + } # if + + # Check the iSNS Server setting + if ($iSNSServerCurrent) + { + # The iSNS Server is set but should not be + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($LocalizedData.iSNSServerIsSetButShouldNotBeMessage) + ) -join '' ) + $desiredConfigurationMatch = $false + } # if + } # if + return $desiredConfigurationMatch +} # Test-TargetResource diff --git a/Modules/iSCSIDsc/DSCResources/DSR_iSCSIServerTarget/DSR_iSCSIServerTarget.psm1 b/Modules/iSCSIDsc/DSCResources/DSR_iSCSIServerTarget/DSR_iSCSIServerTarget.psm1 index 1a70543..6e3fed5 100644 --- a/Modules/iSCSIDsc/DSCResources/DSR_iSCSIServerTarget/DSR_iSCSIServerTarget.psm1 +++ b/Modules/iSCSIDsc/DSCResources/DSR_iSCSIServerTarget/DSR_iSCSIServerTarget.psm1 @@ -83,7 +83,7 @@ function Get-TargetResource } # if # Get the iSNS Server - $iSNSServerCurrent = Get-WmiObject ` + $iSNSServerCurrent = Get-CimInstance ` -Class WT_iSNSServer ` -Namespace root\wmi if ($iSNSServerCurrent) @@ -154,7 +154,7 @@ function Set-TargetResource $serverTarget = Get-ServerTarget -TargetName $TargetName # Get the iSNS Server - $iSNSServerCurrent = Get-WmiObject ` + $iSNSServerCurrent = Get-CimInstance ` -Class WT_iSNSServer ` -Namespace root\wmi @@ -384,7 +384,7 @@ function Test-TargetResource $serverTarget = Get-ServerTarget -TargetName $TargetName # Get the iSNS Server - $iSNSServerCurrent = Get-WmiObject ` + $iSNSServerCurrent = Get-CimInstance ` -Class WT_iSNSServer ` -Namespace root\wmi diff --git a/Tests/Integration/DSR_iSCSIInitiator.Integration.Tests.ps1 b/Tests/Integration/DSR_iSCSIInitiator.Integration.Tests.ps1 index eb00ae1..4645515 100644 --- a/Tests/Integration/DSR_iSCSIInitiator.Integration.Tests.ps1 +++ b/Tests/Integration/DSR_iSCSIInitiator.Integration.Tests.ps1 @@ -124,7 +124,7 @@ try $Initiator.IsDiscovered | Should -Be $ConnectionNew.IsDiscovered $Initiator.IsHeaderDigest | Should -Be $ConnectionNew.IsHeaderDigest $Initiator.IsPersistent | Should -Be $ConnectionNew.IsPersistent - $iSNSServerNew = Get-WmiObject -Class MSiSCSIInitiator_iSNSServerClass -Namespace root\wmi + $iSNSServerNew = Get-CimInstance -Class MSiSCSIInitiator_iSNSServerClass -Namespace root\wmi # The iSNS Server is not usually accessible so won't be able to be set # $Initiator.iSNSServer | Should Be $iSNSServerNew.iSNSServerAddress } diff --git a/Tests/Unit/DSR_iSCSIInitiator.Tests.ps1 b/Tests/Unit/DSR_iSCSIInitiator.Tests.ps1 index d29c242..5a2d3f7 100644 --- a/Tests/Unit/DSR_iSCSIInitiator.Tests.ps1 +++ b/Tests/Unit/DSR_iSCSIInitiator.Tests.ps1 @@ -188,7 +188,7 @@ try Mock Get-Target Mock Get-Connection Mock Get-Session - Mock Get-WMIObject + Mock Get-CimInstance It 'should return absent' { $Result = Get-TargetResource ` -NodeAddress $TestInitiator.NodeAddress ` @@ -201,7 +201,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 0 Assert-MockCalled -commandName Get-Session -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -210,7 +210,7 @@ try Mock Get-Target Mock Get-Connection Mock Get-Session - Mock Get-WMIObject + Mock Get-CimInstance It 'should return absent but with Target Portal data' { $Result = Get-TargetResource ` -NodeAddress $TestInitiator.NodeAddress ` @@ -229,7 +229,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 0 Assert-MockCalled -commandName Get-Session -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -238,7 +238,7 @@ try Mock Get-Target -MockWith { return @($MockTargetNotConnected) } Mock Get-Connection Mock Get-Session - Mock Get-WMIObject + Mock Get-CimInstance It 'should return absent but with Target Portal data' { $Result = Get-TargetResource ` -NodeAddress $TestInitiator.NodeAddress ` @@ -257,7 +257,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 0 Assert-MockCalled -commandName Get-Session -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -266,7 +266,7 @@ try Mock Get-Target -MockWith { return @($MockTargetNotConnected) } Mock Get-Connection Mock Get-Session - Mock Get-WMIObject + Mock Get-CimInstance It 'should return absent but with Target Portal data' { $Result = Get-TargetResource ` -NodeAddress $TestInitiatorWithoutInitiatorAddress.NodeAddress ` @@ -284,7 +284,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 0 Assert-MockCalled -commandName Get-Session -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -293,7 +293,7 @@ try Mock Get-Target -MockWith { return @($MockTarget) } Mock Get-Connection -MockWith { return @($MockConnection) } Mock Get-Session -MockWith { return @($MockSession) } - Mock Get-WMIObject + Mock Get-CimInstance It 'should return correct data' { $Result = Get-TargetResource ` -NodeAddress $TestInitiator.NodeAddress ` @@ -319,7 +319,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 1 Assert-MockCalled -commandName Get-Session -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -328,7 +328,7 @@ try Mock Get-Target -MockWith { return @($MockTarget) } Mock Get-Connection -MockWith { return @($MockConnection) } Mock Get-Session -MockWith { return @($MockSession) } - Mock Get-WMIObject + Mock Get-CimInstance It 'should return correct data' { $Result = Get-TargetResource ` -NodeAddress $TestInitiatorWithoutInitiatorAddress.NodeAddress ` @@ -353,7 +353,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 1 Assert-MockCalled -commandName Get-Session -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -362,7 +362,7 @@ try Mock Get-Target -MockWith { return @($MockTarget) } Mock Get-Connection -MockWith { return @($MockConnection) } Mock Get-Session -MockWith { return @($MockSession) } - Mock Get-WMIObject -MockWith { return @($MockiSNSSrver) } + Mock Get-CimInstance -MockWith { return @($MockiSNSSrver) } It 'should return correct data' { $Result = Get-TargetResource ` -NodeAddress $TestInitiator.NodeAddress ` @@ -389,7 +389,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 1 Assert-MockCalled -commandName Get-Session -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } } @@ -406,7 +406,7 @@ try Mock Disconnect-IscsiTarget Mock Register-IscsiSession Mock Unregister-IscsiSession - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject It 'should not throw error' { @@ -426,7 +426,7 @@ try Assert-MockCalled -commandName Disconnect-IscsiTarget -Exactly 0 Assert-MockCalled -commandName Register-IscsiSession -Exactly 1 Assert-MockCalled -commandName Unregister-IscsiSession -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -443,7 +443,7 @@ try Mock Disconnect-IscsiTarget Mock Register-IscsiSession Mock Unregister-IscsiSession - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject It 'should not throw error' { @@ -463,7 +463,7 @@ try Assert-MockCalled -commandName Disconnect-IscsiTarget -Exactly 0 Assert-MockCalled -commandName Register-IscsiSession -Exactly 1 Assert-MockCalled -commandName Unregister-IscsiSession -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -480,7 +480,7 @@ try Mock Disconnect-IscsiTarget Mock Register-IscsiSession Mock Unregister-IscsiSession - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject It 'should not throw error' { @@ -500,7 +500,7 @@ try Assert-MockCalled -commandName Disconnect-IscsiTarget -Exactly 0 Assert-MockCalled -commandName Register-IscsiSession -Exactly 1 Assert-MockCalled -commandName Unregister-IscsiSession -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -517,7 +517,7 @@ try Mock Disconnect-IscsiTarget Mock Register-IscsiSession Mock Unregister-IscsiSession - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject It 'should not throw error' { @@ -537,7 +537,7 @@ try Assert-MockCalled -commandName Disconnect-IscsiTarget -Exactly 0 Assert-MockCalled -commandName Register-IscsiSession -Exactly 1 Assert-MockCalled -commandName Unregister-IscsiSession -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -553,7 +553,7 @@ try Mock Disconnect-IscsiTarget Mock Register-IscsiSession Mock Unregister-IscsiSession - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject @@ -576,7 +576,7 @@ try Assert-MockCalled -commandName Disconnect-IscsiTarget -Exactly 0 Assert-MockCalled -commandName Register-IscsiSession -Exactly 1 Assert-MockCalled -commandName Unregister-IscsiSession -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -601,7 +601,7 @@ try Assert-MockCalled -commandName Disconnect-IscsiTarget -Exactly 0 Assert-MockCalled -commandName Register-IscsiSession -Exactly 1 Assert-MockCalled -commandName Unregister-IscsiSession -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -626,7 +626,7 @@ try Assert-MockCalled -commandName Disconnect-IscsiTarget -Exactly 0 Assert-MockCalled -commandName Register-IscsiSession -Exactly 1 Assert-MockCalled -commandName Unregister-IscsiSession -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -651,7 +651,7 @@ try Assert-MockCalled -commandName Disconnect-IscsiTarget -Exactly 0 Assert-MockCalled -commandName Register-IscsiSession -Exactly 1 Assert-MockCalled -commandName Unregister-IscsiSession -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -668,7 +668,7 @@ try Mock Disconnect-IscsiTarget Mock Register-IscsiSession Mock Unregister-IscsiSession - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject It 'should not throw error' { @@ -688,7 +688,7 @@ try Assert-MockCalled -commandName Disconnect-IscsiTarget -Exactly 0 Assert-MockCalled -commandName Register-IscsiSession -Exactly 1 Assert-MockCalled -commandName Unregister-IscsiSession -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -705,7 +705,7 @@ try Mock Disconnect-IscsiTarget Mock Register-IscsiSession Mock Unregister-IscsiSession - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject Mock Resolve-DNSName -MockWith { @@ -728,7 +728,7 @@ try Assert-MockCalled -commandName Disconnect-IscsiTarget -Exactly 0 Assert-MockCalled -commandName Register-IscsiSession -Exactly 1 Assert-MockCalled -commandName Unregister-IscsiSession -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 Assert-MockCalled -commandName Resolve-DNSName -Exactly 1 @@ -746,7 +746,7 @@ try Mock Disconnect-IscsiTarget Mock Register-IscsiSession Mock Unregister-IscsiSession - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject Mock Resolve-DNSName -MockWith { @@ -769,7 +769,7 @@ try Assert-MockCalled -commandName Disconnect-IscsiTarget -Exactly 1 Assert-MockCalled -commandName Register-IscsiSession -Exactly 1 Assert-MockCalled -commandName Unregister-IscsiSession -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 Assert-MockCalled -commandName Resolve-DNSName -Exactly 1 @@ -787,7 +787,7 @@ try Mock Disconnect-IscsiTarget Mock Register-IscsiSession Mock Unregister-IscsiSession - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject It 'should not throw error' { @@ -808,7 +808,7 @@ try Assert-MockCalled -commandName Disconnect-IscsiTarget -Exactly 1 Assert-MockCalled -commandName Register-IscsiSession -Exactly 1 Assert-MockCalled -commandName Unregister-IscsiSession -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -825,7 +825,7 @@ try Mock Disconnect-IscsiTarget Mock Register-IscsiSession Mock Unregister-IscsiSession - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject It 'should not throw error' { @@ -845,7 +845,7 @@ try Assert-MockCalled -commandName Disconnect-IscsiTarget -Exactly 0 Assert-MockCalled -commandName Register-IscsiSession -Exactly 0 Assert-MockCalled -commandName Unregister-IscsiSession -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -862,7 +862,7 @@ try Mock Disconnect-IscsiTarget Mock Register-IscsiSession Mock Unregister-IscsiSession - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject It 'should not throw error' { @@ -883,7 +883,7 @@ try Assert-MockCalled -commandName Disconnect-IscsiTarget -Exactly 0 Assert-MockCalled -commandName Register-IscsiSession -Exactly 0 Assert-MockCalled -commandName Unregister-IscsiSession -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -900,7 +900,7 @@ try Mock Disconnect-IscsiTarget Mock Register-IscsiSession Mock Unregister-IscsiSession - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject It 'should not throw error' { @@ -921,7 +921,7 @@ try Assert-MockCalled -commandName Disconnect-IscsiTarget -Exactly 1 Assert-MockCalled -commandName Register-IscsiSession -Exactly 0 Assert-MockCalled -commandName Unregister-IscsiSession -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -938,7 +938,7 @@ try Mock Disconnect-IscsiTarget Mock Register-IscsiSession Mock Unregister-IscsiSession - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject It 'should not throw error' { @@ -959,7 +959,7 @@ try Assert-MockCalled -commandName Disconnect-IscsiTarget -Exactly 0 Assert-MockCalled -commandName Register-IscsiSession -Exactly 0 Assert-MockCalled -commandName Unregister-IscsiSession -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -976,7 +976,7 @@ try Mock Disconnect-IscsiTarget Mock Register-IscsiSession Mock Unregister-IscsiSession - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject It 'should not throw error' { @@ -997,7 +997,7 @@ try Assert-MockCalled -commandName Disconnect-IscsiTarget -Exactly 1 Assert-MockCalled -commandName Register-IscsiSession -Exactly 0 Assert-MockCalled -commandName Unregister-IscsiSession -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -1014,7 +1014,7 @@ try Mock Disconnect-IscsiTarget Mock Register-IscsiSession Mock Unregister-IscsiSession - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject It 'should not throw error' { @@ -1035,7 +1035,7 @@ try Assert-MockCalled -commandName Disconnect-IscsiTarget -Exactly 0 Assert-MockCalled -commandName Register-IscsiSession -Exactly 0 Assert-MockCalled -commandName Unregister-IscsiSession -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -1052,7 +1052,7 @@ try Mock Disconnect-IscsiTarget Mock Register-IscsiSession Mock Unregister-IscsiSession - Mock Get-WMIObject -MockWith { return @($MockiSNSSrver) } + Mock Get-CimInstance -MockWith { return @($MockiSNSSrver) } Mock Set-WMIInstance Mock Remove-WMIObject It 'should not throw error' { @@ -1073,7 +1073,7 @@ try Assert-MockCalled -commandName Disconnect-IscsiTarget -Exactly 0 Assert-MockCalled -commandName Register-IscsiSession -Exactly 0 Assert-MockCalled -commandName Unregister-IscsiSession -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 1 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -1090,7 +1090,7 @@ try Mock Disconnect-IscsiTarget Mock Register-IscsiSession Mock Unregister-IscsiSession - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject It 'should not throw error' { @@ -1110,7 +1110,7 @@ try Assert-MockCalled -commandName Disconnect-IscsiTarget -Exactly 0 Assert-MockCalled -commandName Register-IscsiSession -Exactly 0 Assert-MockCalled -commandName Unregister-IscsiSession -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 1 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -1127,7 +1127,7 @@ try Mock Disconnect-IscsiTarget Mock Register-IscsiSession Mock Unregister-IscsiSession - Mock Get-WMIObject -MockWith { return @($MockiSNSSrver) } + Mock Get-CimInstance -MockWith { return @($MockiSNSSrver) } Mock Set-WMIInstance Mock Remove-WMIObject It 'should not throw error' { @@ -1148,7 +1148,7 @@ try Assert-MockCalled -commandName Disconnect-IscsiTarget -Exactly 0 Assert-MockCalled -commandName Register-IscsiSession -Exactly 0 Assert-MockCalled -commandName Unregister-IscsiSession -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 1 } @@ -1165,7 +1165,7 @@ try Mock Disconnect-IscsiTarget Mock Register-IscsiSession Mock Unregister-IscsiSession - Mock Get-WMIObject -MockWith { return @($MockiSNSSrver) } + Mock Get-CimInstance -MockWith { return @($MockiSNSSrver) } Mock Set-WMIInstance Mock Remove-WMIObject It 'should not throw error' { @@ -1186,7 +1186,7 @@ try Assert-MockCalled -commandName Disconnect-IscsiTarget -Exactly 0 Assert-MockCalled -commandName Register-IscsiSession -Exactly 0 Assert-MockCalled -commandName Unregister-IscsiSession -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 1 } @@ -1199,7 +1199,7 @@ try Mock Get-Target Mock Get-Connection Mock Get-Session - Mock Get-WMIObject + Mock Get-CimInstance It 'should return false' { $Splat = $TestInitiator.Clone() Test-TargetResource @Splat | Should -Be $False @@ -1209,7 +1209,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 0 Assert-MockCalled -commandName Get-Connection -Exactly 0 Assert-MockCalled -commandName Get-Session -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -1218,7 +1218,7 @@ try Mock Get-Target Mock Get-Connection Mock Get-Session - Mock Get-WMIObject + Mock Get-CimInstance It 'should return false' { $Splat = $TestInitiatorWithoutInitiatorAddress.Clone() Test-TargetResource @Splat | Should -Be $False @@ -1228,7 +1228,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 0 Assert-MockCalled -commandName Get-Connection -Exactly 0 Assert-MockCalled -commandName Get-Session -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -1237,7 +1237,7 @@ try Mock Get-Target Mock Get-Connection Mock Get-Session - Mock Get-WMIObject + Mock Get-CimInstance It 'should return false' { $Splat = $TestInitiator.Clone() Test-TargetResource @Splat | Should -Be $False @@ -1247,7 +1247,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 0 Assert-MockCalled -commandName Get-Session -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -1256,7 +1256,7 @@ try Mock Get-Target -MockWith { return @($MockTargetNotConnected) } Mock Get-Connection Mock Get-Session - Mock Get-WMIObject + Mock Get-CimInstance It 'should return false' { $Splat = $TestInitiator.Clone() Test-TargetResource @Splat | Should -Be $False @@ -1266,7 +1266,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 0 Assert-MockCalled -commandName Get-Session -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -1275,7 +1275,7 @@ try Mock Get-Target -MockWith { return @($MockTargetNotConnected) } Mock Get-Connection Mock Get-Session - Mock Get-WMIObject + Mock Get-CimInstance It 'should return false' { $Splat = $TestInitiatorWithoutInitiatorAddress.Clone() Test-TargetResource @Splat | Should -Be $False @@ -1285,7 +1285,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 0 Assert-MockCalled -commandName Get-Session -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -1293,7 +1293,7 @@ try Mock Get-Target -MockWith { return @($MockTarget) } Mock Get-Connection Mock Get-Session - Mock Get-WMIObject + Mock Get-CimInstance Context 'Target Portal does exist and should but TargetPortalPortNumber is different' { It 'should return false' { @@ -1306,7 +1306,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 1 Assert-MockCalled -commandName Get-Session -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -1321,7 +1321,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 1 Assert-MockCalled -commandName Get-Session -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -1336,7 +1336,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 1 Assert-MockCalled -commandName Get-Session -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -1351,7 +1351,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 1 Assert-MockCalled -commandName Get-Session -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -1360,7 +1360,7 @@ try Mock Get-Target -MockWith { return @($MockTarget) } Mock Get-Connection -MockWith { return @($MockConnection) } Mock Get-Session -MockWith { return @($MockSessionPersistent) } - Mock Get-WMIObject + Mock Get-CimInstance Mock Resolve-DNSName -MockWith { return @( @{ IPAddress = $MockConnection.TargetAddress } ) } @@ -1373,7 +1373,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 1 Assert-MockCalled -commandName Get-Session -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Resolve-DNSName -Exactly 1 } } @@ -1383,7 +1383,7 @@ try Mock Get-Target -MockWith { return @($MockTarget) } Mock Get-Connection -MockWith { return @($MockConnection) } Mock Get-Session -MockWith { return @($MockSessionPersistent) } - Mock Get-WMIObject + Mock Get-CimInstance Mock Resolve-DNSName -MockWith { return @( @{ IPAddress = '1.1.1.1' } ) } @@ -1396,7 +1396,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 1 Assert-MockCalled -commandName Get-Session -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Resolve-DNSName -Exactly 1 } } @@ -1406,7 +1406,7 @@ try Mock Get-Target -MockWith { return @($MockTarget) } Mock Get-Connection -MockWith { return @($MockConnection) } Mock Get-Session -MockWith { return @($MockSession) } - Mock Get-WMIObject + Mock Get-CimInstance It 'should return false' { $Splat = $TestInitiator.Clone() $Splat.AuthenticationType = 'None' @@ -1417,7 +1417,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 1 Assert-MockCalled -commandName Get-Session -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -1426,7 +1426,7 @@ try Mock Get-Target -MockWith { return @($MockTarget) } Mock Get-Connection -MockWith { return @($MockConnection) } Mock Get-Session -MockWith { return @($MockSessionPersistent) } - Mock Get-WMIObject + Mock Get-CimInstance It 'should return true' { $Splat = $TestInitiator.Clone() Test-TargetResource @Splat | Should -Be $True @@ -1436,7 +1436,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 1 Assert-MockCalled -commandName Get-Session -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -1445,7 +1445,7 @@ try Mock Get-Target -MockWith { return @($MockTarget) } Mock Get-Connection -MockWith { return @($MockConnection) } Mock Get-Session -MockWith { return @($MockSessionPersistent) } - Mock Get-WMIObject + Mock Get-CimInstance It 'should return false' { $Splat = $TestInitiator.Clone() $Splat.IsPersistent = $False @@ -1456,7 +1456,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 1 Assert-MockCalled -commandName Get-Session -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -1465,7 +1465,7 @@ try Mock Get-Target -MockWith { return @($MockTarget) } Mock Get-Connection Mock Get-Session - Mock Get-WMIObject + Mock Get-CimInstance It 'should return false' { $Splat = $TestInitiator.Clone() $Splat.Ensure = 'Absent' @@ -1476,7 +1476,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 0 Assert-MockCalled -commandName Get-Session -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -1485,7 +1485,7 @@ try Mock Get-Target -MockWith { return @($MockTargetNotConnected) } Mock Get-Connection Mock Get-Session - Mock Get-WMIObject + Mock Get-CimInstance It 'should return false' { $Splat = $TestInitiator.Clone() $Splat.Ensure = 'Absent' @@ -1496,7 +1496,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 0 Assert-MockCalled -commandName Get-Session -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -1505,7 +1505,7 @@ try Mock Get-Target -MockWith { return @($MockTarget) } Mock Get-Connection Mock Get-Session - Mock Get-WMIObject + Mock Get-CimInstance It 'should return false' { $Splat = $TestInitiator.Clone() $Splat.Ensure = 'Absent' @@ -1516,7 +1516,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 0 Assert-MockCalled -commandName Get-Session -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -1525,7 +1525,7 @@ try Mock Get-Target Mock Get-Connection Mock Get-Session - Mock Get-WMIObject + Mock Get-CimInstance It 'should return true' { $Splat = $TestInitiator.Clone() $Splat.Ensure = 'Absent' @@ -1536,7 +1536,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 0 Assert-MockCalled -commandName Get-Session -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -1545,7 +1545,7 @@ try Mock Get-Target -MockWith { return @($MockTarget) } Mock Get-Connection -MockWith { return @($MockConnection) } Mock Get-Session -MockWith { return @($MockSessionPersistent) } - Mock Get-WMIObject + Mock Get-CimInstance It 'should return false' { $Splat = $TestInitiatorWithiSNS.Clone() Test-TargetResource @Splat | Should -Be $False @@ -1555,7 +1555,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 1 Assert-MockCalled -commandName Get-Session -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -1564,7 +1564,7 @@ try Mock Get-Target -MockWith { return @($MockTarget) } Mock Get-Connection -MockWith { return @($MockConnection) } Mock Get-Session -MockWith { return @($MockSessionPersistent) } - Mock Get-WMIObject -MockWith { return @($MockiSNSSrver) } + Mock Get-CimInstance -MockWith { return @($MockiSNSSrver) } It 'should return false' { $Splat = $TestInitiatorWithiSNS.Clone() $Splat.iSNSServer = 'different.contoso.com' @@ -1575,7 +1575,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 1 Assert-MockCalled -commandName Get-Session -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -1584,7 +1584,7 @@ try Mock Get-Target -MockWith { return @($MockTarget) } Mock Get-Connection -MockWith { return @($MockConnection) } Mock Get-Session -MockWith { return @($MockSessionPersistent) } - Mock Get-WMIObject -MockWith { return @($MockiSNSSrver) } + Mock Get-CimInstance -MockWith { return @($MockiSNSSrver) } It 'should return false' { $Splat = $TestInitiatorWithiSNS.Clone() $Splat.iSNSServer = '' @@ -1595,7 +1595,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 1 Assert-MockCalled -commandName Get-Session -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } @@ -1604,7 +1604,7 @@ try Mock Get-Target Mock Get-Connection Mock Get-Session - Mock Get-WMIObject -MockWith { return @($MockiSNSSrver) } + Mock Get-CimInstance -MockWith { return @($MockiSNSSrver) } It 'should return false' { $Splat = $TestInitiator.Clone() $Splat.Ensure = 'Absent' @@ -1615,7 +1615,7 @@ try Assert-MockCalled -commandName Get-Target -Exactly 1 Assert-MockCalled -commandName Get-Connection -Exactly 0 Assert-MockCalled -commandName Get-Session -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } } diff --git a/Tests/Unit/DSR_iSCSIServerTarget.Tests.ps1 b/Tests/Unit/DSR_iSCSIServerTarget.Tests.ps1 index 434e779..4afcd9e 100644 --- a/Tests/Unit/DSR_iSCSIServerTarget.Tests.ps1 +++ b/Tests/Unit/DSR_iSCSIServerTarget.Tests.ps1 @@ -97,7 +97,7 @@ try Context 'Server Target does not exist' { Mock Get-iSCSIServerTarget - Mock Get-WMIObject + Mock Get-CimInstance It 'should return absent Server Target' { $Result = Get-TargetResource ` @@ -109,14 +109,14 @@ try } It 'should call the expected mocks' { Assert-MockCalled -commandName Get-iSCSIServerTarget -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } Context 'Server Target exists and iSNS Server not set' { Mock Get-iSCSIServerTarget -MockWith { return @($MockServerTarget) } - Mock Get-WMIObject + Mock Get-CimInstance It 'should return correct Server Target' { $Result = Get-TargetResource ` @@ -130,14 +130,14 @@ try } It 'should call the expected mocks' { Assert-MockCalled -commandName Get-iSCSIServerTarget -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } Context 'Server Target exists and iSNS Server set' { Mock Get-iSCSIServerTarget -MockWith { return @($MockServerTarget) } - Mock Get-WMIObject -MockWith { return @($MockiSNSSrver) } + Mock Get-CimInstance -MockWith { return @($MockiSNSSrver) } It 'should return correct Server Target' { $Result = Get-TargetResource ` @@ -151,7 +151,7 @@ try } It 'should call the expected mocks' { Assert-MockCalled -commandName Get-iSCSIServerTarget -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } } @@ -166,7 +166,7 @@ try Mock Remove-iSCSIServerTarget Mock Add-IscsiVirtualDiskTargetMapping Mock Remove-IscsiVirtualDiskTargetMapping - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject @@ -183,7 +183,7 @@ try Assert-MockCalled -commandName Remove-iSCSIServerTarget -Exactly 0 Assert-MockCalled -commandName Add-IscsiVirtualDiskTargetMapping -Exactly 1 Assert-MockCalled -commandName Remove-IscsiVirtualDiskTargetMapping -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -197,7 +197,7 @@ try Mock Remove-iSCSIServerTarget Mock Add-IscsiVirtualDiskTargetMapping Mock Remove-IscsiVirtualDiskTargetMapping - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject @@ -215,7 +215,7 @@ try Assert-MockCalled -commandName Remove-iSCSIServerTarget -Exactly 0 Assert-MockCalled -commandName Add-IscsiVirtualDiskTargetMapping -Exactly 1 Assert-MockCalled -commandName Remove-IscsiVirtualDiskTargetMapping -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -229,7 +229,7 @@ try Mock Remove-iSCSIServerTarget Mock Add-IscsiVirtualDiskTargetMapping Mock Remove-IscsiVirtualDiskTargetMapping - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject @@ -247,7 +247,7 @@ try Assert-MockCalled -commandName Remove-iSCSIServerTarget -Exactly 0 Assert-MockCalled -commandName Add-IscsiVirtualDiskTargetMapping -Exactly 1 Assert-MockCalled -commandName Remove-IscsiVirtualDiskTargetMapping -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -261,7 +261,7 @@ try Mock Remove-iSCSIServerTarget Mock Add-IscsiVirtualDiskTargetMapping Mock Remove-IscsiVirtualDiskTargetMapping - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject @@ -279,7 +279,7 @@ try Assert-MockCalled -commandName Remove-iSCSIServerTarget -Exactly 0 Assert-MockCalled -commandName Add-IscsiVirtualDiskTargetMapping -Exactly 0 Assert-MockCalled -commandName Remove-IscsiVirtualDiskTargetMapping -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -293,7 +293,7 @@ try Mock Remove-iSCSIServerTarget Mock Add-IscsiVirtualDiskTargetMapping Mock Remove-IscsiVirtualDiskTargetMapping - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject @@ -311,7 +311,7 @@ try Assert-MockCalled -commandName Remove-iSCSIServerTarget -Exactly 1 Assert-MockCalled -commandName Add-IscsiVirtualDiskTargetMapping -Exactly 0 Assert-MockCalled -commandName Remove-IscsiVirtualDiskTargetMapping -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -325,7 +325,7 @@ try Mock Remove-iSCSIServerTarget Mock Add-IscsiVirtualDiskTargetMapping Mock Remove-IscsiVirtualDiskTargetMapping - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject @@ -343,7 +343,7 @@ try Assert-MockCalled -commandName Remove-iSCSIServerTarget -Exactly 0 Assert-MockCalled -commandName Add-IscsiVirtualDiskTargetMapping -Exactly 0 Assert-MockCalled -commandName Remove-IscsiVirtualDiskTargetMapping -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -357,7 +357,7 @@ try Mock Remove-iSCSIServerTarget Mock Add-IscsiVirtualDiskTargetMapping Mock Remove-IscsiVirtualDiskTargetMapping - Mock Get-WMIObject -MockWith { return @($MockiSNSSrver) } + Mock Get-CimInstance -MockWith { return @($MockiSNSSrver) } Mock Set-WMIInstance Mock Remove-WMIObject @@ -375,7 +375,7 @@ try Assert-MockCalled -commandName Remove-iSCSIServerTarget -Exactly 0 Assert-MockCalled -commandName Add-IscsiVirtualDiskTargetMapping -Exactly 0 Assert-MockCalled -commandName Remove-IscsiVirtualDiskTargetMapping -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 1 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -389,7 +389,7 @@ try Mock Remove-iSCSIServerTarget Mock Add-IscsiVirtualDiskTargetMapping Mock Remove-IscsiVirtualDiskTargetMapping - Mock Get-WMIObject + Mock Get-CimInstance Mock Set-WMIInstance Mock Remove-WMIObject @@ -406,7 +406,7 @@ try Assert-MockCalled -commandName Remove-iSCSIServerTarget -Exactly 0 Assert-MockCalled -commandName Add-IscsiVirtualDiskTargetMapping -Exactly 0 Assert-MockCalled -commandName Remove-IscsiVirtualDiskTargetMapping -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 1 Assert-MockCalled -commandName Remove-WMIObject -Exactly 0 } @@ -420,7 +420,7 @@ try Mock Remove-iSCSIServerTarget Mock Add-IscsiVirtualDiskTargetMapping Mock Remove-IscsiVirtualDiskTargetMapping - Mock Get-WMIObject -MockWith { return @($MockiSNSSrver) } + Mock Get-CimInstance -MockWith { return @($MockiSNSSrver) } Mock Set-WMIInstance Mock Remove-WMIObject @@ -438,7 +438,7 @@ try Assert-MockCalled -commandName Remove-iSCSIServerTarget -Exactly 0 Assert-MockCalled -commandName Add-IscsiVirtualDiskTargetMapping -Exactly 0 Assert-MockCalled -commandName Remove-IscsiVirtualDiskTargetMapping -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 1 } @@ -452,7 +452,7 @@ try Mock Remove-iSCSIServerTarget Mock Add-IscsiVirtualDiskTargetMapping Mock Remove-IscsiVirtualDiskTargetMapping - Mock Get-WMIObject -MockWith { return @($MockiSNSSrver) } + Mock Get-CimInstance -MockWith { return @($MockiSNSSrver) } Mock Set-WMIInstance Mock Remove-WMIObject @@ -470,7 +470,7 @@ try Assert-MockCalled -commandName Remove-iSCSIServerTarget -Exactly 0 Assert-MockCalled -commandName Add-IscsiVirtualDiskTargetMapping -Exactly 0 Assert-MockCalled -commandName Remove-IscsiVirtualDiskTargetMapping -Exactly 0 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 Assert-MockCalled -commandName Set-WMIInstance -Exactly 0 Assert-MockCalled -commandName Remove-WMIObject -Exactly 1 } @@ -482,7 +482,7 @@ try Context 'Server Target does not exist but should' { Mock Get-iSCSIServerTarget - Mock Get-WMIObject + Mock Get-CimInstance It 'should return false' { $Splat = $TestServerTarget.Clone() @@ -490,14 +490,14 @@ try } It 'should call expected Mocks' { Assert-MockCalled -commandName Get-iSCSIServerTarget -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } Context 'Server Target exists and should but has a different Paths' { Mock Get-iSCSIServerTarget -MockWith { return @($MockServerTarget) } - Mock Get-WMIObject + Mock Get-CimInstance It 'should return false' { $Splat = $TestServerTarget.Clone() @@ -506,14 +506,14 @@ try } It 'should call expected Mocks' { Assert-MockCalled -commandName Get-iSCSIServerTarget -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } Context 'Server Target exists and should but has a different InitiatorIds' { Mock Get-iSCSIServerTarget -MockWith { return @($MockServerTarget) } - Mock Get-WMIObject + Mock Get-CimInstance It 'should return false' { $Splat = $TestServerTarget.Clone() @@ -522,14 +522,14 @@ try } It 'should call expected Mocks' { Assert-MockCalled -commandName Get-iSCSIServerTarget -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } Context 'Server Target exists and should and all parameters match' { Mock Get-iSCSIServerTarget -MockWith { return @($MockServerTarget) } - Mock Get-WMIObject + Mock Get-CimInstance It 'should return true' { $Splat = $TestServerTarget.Clone() @@ -537,14 +537,14 @@ try } It 'should call expected Mocks' { Assert-MockCalled -commandName Get-iSCSIServerTarget -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } Context 'Server Target exists but should not' { Mock Get-iSCSIServerTarget -MockWith { return @($MockServerTarget) } - Mock Get-WMIObject + Mock Get-CimInstance It 'should return false' { $Splat = $TestServerTarget.Clone() @@ -553,14 +553,14 @@ try } It 'should call expected Mocks' { Assert-MockCalled -commandName Get-iSCSIServerTarget -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } Context 'Server Target does not exist and should not' { Mock Get-iSCSIServerTarget - Mock Get-WMIObject + Mock Get-CimInstance It 'should return true' { $Splat = $TestServerTarget.Clone() @@ -569,14 +569,14 @@ try } It 'should call expected Mocks' { Assert-MockCalled -commandName Get-iSCSIServerTarget -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } Context 'Server Target exists and should and iSNS Server is not set' { Mock Get-iSCSIServerTarget -MockWith { return @($MockServerTarget) } - Mock Get-WMIObject + Mock Get-CimInstance It 'should return false' { $Splat = $TestServerTargetWithiSNS.Clone() @@ -584,14 +584,14 @@ try } It 'should call expected Mocks' { Assert-MockCalled -commandName Get-iSCSIServerTarget -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } Context 'Server Target exists and should and iSNS Server is different' { Mock Get-iSCSIServerTarget -MockWith { return @($MockServerTarget) } - Mock Get-WMIObject -MockWith { return @($MockiSNSSrver) } + Mock Get-CimInstance -MockWith { return @($MockiSNSSrver) } It 'should return false' { $Splat = $TestServerTargetWithiSNS.Clone() @@ -600,14 +600,14 @@ try } It 'should call expected Mocks' { Assert-MockCalled -commandName Get-iSCSIServerTarget -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } Context 'Server Target exists and should and iSNS Server should be cleared' { Mock Get-iSCSIServerTarget -MockWith { return @($MockServerTarget) } - Mock Get-WMIObject -MockWith { return @($MockiSNSSrver) } + Mock Get-CimInstance -MockWith { return @($MockiSNSSrver) } It 'should return false' { $Splat = $TestServerTargetWithiSNS.Clone() @@ -616,14 +616,14 @@ try } It 'should call expected Mocks' { Assert-MockCalled -commandName Get-iSCSIServerTarget -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } Context 'Server Target does not exist and should not but iSNS Server is set' { Mock Get-iSCSIServerTarget - Mock Get-WMIObject -MockWith { return @($MockiSNSSrver) } + Mock Get-CimInstance -MockWith { return @($MockiSNSSrver) } It 'should return false' { $Splat = $TestServerTarget.Clone() @@ -632,7 +632,7 @@ try } It 'should call expected Mocks' { Assert-MockCalled -commandName Get-iSCSIServerTarget -Exactly 1 - Assert-MockCalled -commandName Get-WMIObject -Exactly 1 + Assert-MockCalled -commandName Get-CimInstance -Exactly 1 } } }