DSCResources/MSFT_SCCaseHoldRule/MSFT_SCCaseHoldRule.psm1
function Get-TargetResource { [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param ( [Parameter(Mandatory = $true)] [System.String] $Name, [Parameter(Mandatory = $true)] [System.String] $Policy, [Parameter()] [System.String] $Comment, [Parameter()] [System.String] $ContentMatchQuery, [Parameter()] [System.Boolean] $Disabled = $false, [Parameter()] [ValidateSet('Present', 'Absent')] [System.String] $Ensure = 'Present', [Parameter(Mandatory = $true)] [System.Management.Automation.PSCredential] $GlobalAdminAccount ) Write-Verbose -Message "Getting configuration of SCCaseHoldRule for $Name" #region Telemetry $data = [System.Collections.Generic.Dictionary[[String], [String]]]::new() $data.Add("Resource", $MyInvocation.MyCommand.ModuleName) $data.Add("Method", $MyInvocation.MyCommand) Add-M365DSCTelemetryEvent -Data $data #endregion Test-MSCloudLogin -CloudCredential $GlobalAdminAccount ` -Platform SecurityComplianceCenter $Rules = Get-CaseHoldRule -Policy $Policy -ErrorAction 'SilentlyContinue' $Rule = $Rules | Where-Object { $_.Name -eq $Name } if ($null -eq $Rule) { Write-Verbose -Message "SCCaseHoldRule $($Name) does not exist." $result = $PSBoundParameters $result.Ensure = 'Absent' return $result } else { Write-Verbose "Found existing SCCaseHoldRule $($Name)" $result = @{ Name = $Rule.Name Policy = $Policy Comment = $Rule.Comment Disabled = $Rule.Disabled ContentMatchQuery = $Rule.ContentMatchQuery GlobalAdminAccount = $GlobalAdminAccount Ensure = 'Present' } Write-Verbose -Message "Get-TargetResource Result: `n $(Convert-M365DscHashtableToString -Hashtable $result)" return $result } } function Set-TargetResource { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [System.String] $Name, [Parameter(Mandatory = $true)] [System.String] $Policy, [Parameter()] [System.String] $Comment, [Parameter()] [System.String] $ContentMatchQuery, [Parameter()] [System.Boolean] $Disabled = $false, [Parameter()] [ValidateSet('Present', 'Absent')] [System.String] $Ensure = 'Present', [Parameter(Mandatory = $true)] [System.Management.Automation.PSCredential] $GlobalAdminAccount ) Write-Verbose -Message "Setting configuration of SCCaseHoldRule for $Name" #region Telemetry $data = [System.Collections.Generic.Dictionary[[String], [String]]]::new() $data.Add("Resource", $MyInvocation.MyCommand.ModuleName) $data.Add("Method", $MyInvocation.MyCommand) Add-M365DSCTelemetryEvent -Data $data #endregion Test-MSCloudLogin -CloudCredential $GlobalAdminAccount ` -Platform SecurityComplianceCenter $CurrentRule = Get-TargetResource @PSBoundParameters if (('Present' -eq $Ensure) -and ('Absent' -eq $CurrentRule.Ensure)) { $CreationParams = $PSBoundParameters $CreationParams.Remove("GlobalAdminAccount") $CreationParams.Remove("Ensure") Write-Verbose "Creating new Case Hold Rule $Name calling the New-CaseHoldRule cmdlet." New-CaseHoldRule @CreationParams } # Compliance Case exists and it should. Update it. elseif (('Present' -eq $Ensure) -and ('Present' -eq $CurrentRule.Ensure)) { $UpdateParams = @{ Identity = $Name Comment = $Comment Disabled = $Disabled ContentMatchQuery = $ContentMatchQuery } Write-Verbose "Updating Case Hold Rule $Name by calling the Set-CaseHoldRule cmdlet." Set-CaseHoldRule @UpdateParams } # Compliance Case exists but it shouldn't. Remove it. elseif (('Absent' -eq $Ensure) -and ('Present' -eq $CurrentTag.Ensure)) { Remove-CaseHoldRule -Identity $Name -Confirm:$false } } function Test-TargetResource { [CmdletBinding()] [OutputType([System.Boolean])] param ( [Parameter(Mandatory = $true)] [System.String] $Name, [Parameter(Mandatory = $true)] [System.String] $Policy, [Parameter()] [System.String] $Comment, [Parameter()] [System.String] $ContentMatchQuery, [Parameter()] [System.Boolean] $Disabled = $false, [Parameter()] [ValidateSet('Present', 'Absent')] [System.String] $Ensure = 'Present', [Parameter(Mandatory = $true)] [System.Management.Automation.PSCredential] $GlobalAdminAccount ) Write-Verbose -Message "Testing configuration of SCCaseHoldRule for $Name" $CurrentValues = Get-TargetResource @PSBoundParameters Write-Verbose -Message "Target Values: $(Convert-M365DscHashtableToString -Hashtable $PSBoundParameters)" $ValuesToCheck = $PSBoundParameters $ValuesToCheck.Remove('GlobalAdminAccount') | Out-Null $TestResult = Test-Microsoft365DSCParameterState -CurrentValues $CurrentValues ` -Source $($MyInvocation.MyCommand.Source) ` -DesiredValues $PSBoundParameters ` -ValuesToCheck $ValuesToCheck.Keys Write-Verbose -Message "Test-TargetResource returned $TestResult" return $TestResult } function Export-TargetResource { [CmdletBinding()] [OutputType([System.String])] param ( [Parameter(Mandatory = $true)] [System.Management.Automation.PSCredential] $GlobalAdminAccount ) $InformationPreference = "Continue" #region Telemetry $data = [System.Collections.Generic.Dictionary[[String], [String]]]::new() $data.Add("Resource", $MyInvocation.MyCommand.ModuleName) $data.Add("Method", $MyInvocation.MyCommand) Add-M365DSCTelemetryEvent -Data $data #endregion Test-MSCloudLogin -CloudCredential $GlobalAdminAccount ` -Platform SecurityComplianceCenter [array]$Rules = Get-CaseHoldRule $dscContent = "" $i = 1 foreach ($Rule in $Rules) { Write-Information " [$i/$($Rules.Count)] $($Rule.Name)" try { $policy = Get-CaseHoldPolicy -Identity $Rule.Policy -ErrorAction Stop $params = @{ Name = $Rule.Name Policy = $policy.Name GlobalAdminAccount = $GlobalAdminAccount } $result = Get-TargetResource @params $result.GlobalAdminAccount = Resolve-Credentials -UserName "globaladmin" $dscContent += " SCCaseHoldRule " + (New-GUID).ToString() + "`r`n" $dscContent += " {`r`n" $currentDSCBlock = Get-DSCBlock -Params $result -ModulePath $PSScriptRoot $partialContent = Convert-DSCStringParamToVariable -DSCBlock $currentDSCBlock -ParameterName "GlobalAdminAccount" $partialContent += " }`r`n" $dscContent += $partialContent } catch { Write-Information "You are not authorized to access Case Hold Policy {$($Rule.Policy)}" } $i++ } return $dscContent } Export-ModuleMember -Function *-TargetResource |