DSCResources/MSFT_AADAuthenticationMethodPolicyQRCodeImage/MSFT_AADAuthenticationMethodPolicyQRCodeImage.psm1
function Get-TargetResource { [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param ( [Parameter(Mandatory = $true)] [System.String] $Id, [Parameter()] [ValidateSet('enabled', 'disabled')] [System.String] $State, [Parameter()] [Microsoft.Management.Infrastructure.CimInstance[]] $ExcludeTargets, [Parameter()] [Microsoft.Management.Infrastructure.CimInstance[]] $IncludeTargets, [Parameter()] [System.UInt32] $StandardQRCodeLifetimeInDays, [Parameter()] [ValidateRange(8,20)] [System.UInt32] $PinLength, [Parameter()] [ValidateSet('Present', 'Absent')] [System.String] $Ensure = 'Present', [Parameter()] [System.Management.Automation.PSCredential] $Credential, [Parameter()] [System.String] $ApplicationId, [Parameter()] [System.String] $TenantId, [Parameter()] [System.String] $CertificateThumbprint, [Parameter()] [Switch] $ManagedIdentity, [Parameter()] [System.String[]] $AccessTokens ) New-M365DSCConnection -Workload 'MicrosoftGraph' ` -InboundParameters $PSBoundParameters | Out-Null #Ensure the proper dependencies are installed in the current environment. Confirm-M365DSCDependencies #region Telemetry $ResourceName = $MyInvocation.MyCommand.ModuleName.Replace('MSFT_', '') $CommandName = $MyInvocation.MyCommand $data = Format-M365DSCTelemetryParameters -ResourceName $ResourceName ` -CommandName $CommandName ` -Parameters $PSBoundParameters Add-M365DSCTelemetryEvent -Data $data #endregion $nullResult = $PSBoundParameters $nullResult.Ensure = 'Absent' try { if ($null -ne $Script:exportedInstances -and $Script:ExportMode) { $instance = $Script:exportedInstances | Where-Object -FilterScript {$_.Id -eq $Id} } else { $uri = (Get-MSCloudLoginConnectionProfile -Workload MicrosoftGraph).ResourceUrl + "beta/policies/authenticationMethodsPolicy/authenticationMethodConfigurations/qrCodePin" $response = Invoke-MgGraphRequest -Uri $uri -Method GET $instance = $response } if ($null -eq $instance) { return $nullResult } #region resource generator code $complexExcludeTargets = @() foreach ($currentExcludeTargets in $instance.excludeTargets) { $myExcludeTargets = @{} if ($currentExcludeTargets.id -ne 'all_users') { try { $myExcludeTargetsDisplayName = Get-MgGroup -GroupId $currentExcludeTargets.id -ErrorAction Stop $myExcludeTargets.Add('Id', $myExcludeTargetsDisplayName.DisplayName) } catch { $message = "Could not find a group with id $($currentExcludeTargets.id) specified in ExcludeTargets. Skipping group!" New-M365DSCLogEntry -Message $message ` -Exception $_ ` -Source $($MyInvocation.MyCommand.Source) ` -TenantId $TenantId ` -Credential $Credential continue } } else { $myExcludeTargets.Add('Id', $currentExcludeTargets.id) } if ($null -ne $currentExcludeTargets.targetType) { $myExcludeTargets.Add('TargetType', $currentExcludeTargets.targetType.toString()) } if ($myExcludeTargets.values.Where({ $null -ne $_ }).count -gt 0) { $complexExcludeTargets += $myExcludeTargets } } #endregion $complexincludeTargets = @() foreach ($currentincludeTargets in $instance.includeTargets) { $myincludeTargets = @{} if ($currentIncludeTargets.id -ne 'all_users') { try { $myIncludeTargetsDisplayName = Get-MgGroup -GroupId $currentIncludeTargets.id -ErrorAction Stop $myIncludeTargets.Add('Id', $myIncludeTargetsDisplayName.DisplayName) } catch { $message = "Could not find a group with id $($currentIncludeTargets.id) specified in IncludeTargets. Skipping group!" New-M365DSCLogEntry -Message $message ` -Exception $_ ` -Source $($MyInvocation.MyCommand.Source) ` -TenantId $TenantId ` -Credential $Credential continue } } else { $myIncludeTargets.Add('Id', $currentIncludeTargets.id) } if ($null -ne $currentincludeTargets.targetType) { $myincludeTargets.Add('TargetType', $currentincludeTargets.targetType.toString()) } if ($myincludeTargets.values.Where({ $null -ne $_ }).count -gt 0) { $complexincludeTargets += $myincludeTargets } } $results = @{ Id = $response.Id State = $response.State ExcludeTargets = $complexExcludeTargets IncludeTargets = $complexincludeTargets StandardQRCodeLifetimeInDays = $response.StandardQRCodeLifetimeInDays PinLength = $response.PinLength Ensure = 'Present' Credential = $Credential ApplicationId = $ApplicationId TenantId = $TenantId CertificateThumbprint = $CertificateThumbprint ManagedIdentity = $ManagedIdentity.IsPresent AccessTokens = $AccessTokens } return [System.Collections.Hashtable] $results } catch { Write-Verbose -Message $_ New-M365DSCLogEntry -Message 'Error retrieving data:' ` -Exception $_ ` -Source $($MyInvocation.MyCommand.Source) ` -TenantId $TenantId ` -Credential $Credential return $nullResult } } function Set-TargetResource { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [System.String] $Id, [Parameter()] [ValidateSet('enabled', 'disabled')] [System.String] $State, [Parameter()] [Microsoft.Management.Infrastructure.CimInstance[]] $ExcludeTargets, [Parameter()] [Microsoft.Management.Infrastructure.CimInstance[]] $IncludeTargets, [Parameter()] [System.UInt32] $StandardQRCodeLifetimeInDays, [Parameter()] [ValidateRange(8,20)] [System.UInt32] $PinLength, [Parameter()] [ValidateSet('Present', 'Absent')] [System.String] $Ensure = 'Present', [Parameter()] [System.Management.Automation.PSCredential] $Credential, [Parameter()] [System.String] $ApplicationId, [Parameter()] [System.String] $TenantId, [Parameter()] [System.String] $CertificateThumbprint, [Parameter()] [Switch] $ManagedIdentity, [Parameter()] [System.String[]] $AccessTokens ) #Ensure the proper dependencies are installed in the current environment. Confirm-M365DSCDependencies #region Telemetry $ResourceName = $MyInvocation.MyCommand.ModuleName.Replace('MSFT_', '') $CommandName = $MyInvocation.MyCommand $data = Format-M365DSCTelemetryParameters -ResourceName $ResourceName ` -CommandName $CommandName ` -Parameters $PSBoundParameters Add-M365DSCTelemetryEvent -Data $data #endregion $currentInstance = Get-TargetResource @PSBoundParameters $BoundParameters = Remove-M365DSCAuthenticationParameter -BoundParameters $PSBoundParameters if ($Ensure -eq 'Present') { Write-Verbose -Message "Updating the Azure AD Authentication Method Policy QR Code Image with Id {$($currentInstance.Id)}" $UpdateParameters = ([Hashtable]$BoundParameters).clone() $UpdateParameters = Rename-M365DSCCimInstanceParameter -Properties $UpdateParameters $UpdateParameters.Remove('Id') | Out-Null $keys = (([Hashtable]$UpdateParameters).clone()).Keys foreach ($key in $keys) { if ($null -ne $UpdateParameters.$key -and $UpdateParameters.$key.getType().Name -like '*cimInstance*') { $UpdateParameters.$key = Convert-M365DSCDRGComplexTypeToHashtable -ComplexObject $UpdateParameters.$key } if ($key -eq 'IncludeTargets') { $i = 0 foreach ($entry in $UpdateParameters.$key) { if ($entry.id -notmatch '^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$|all_users') { $Filter = "Displayname eq '$($entry.id)'" | Out-String $UpdateParameters.$key[$i].foreach('id', (Get-MgGroup -Filter $Filter).id.ToString()) } $i++ } } if ($key -eq 'ExcludeTargets') { $i = 0 foreach ($entry in $UpdateParameters.$key) { if ($entry.id -notmatch '^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$|all_users') { $Filter = "Displayname eq '$($entry.id)'" | Out-String $UpdateParameters.$key[$i].foreach('id', (Get-MgGroup -Filter $Filter).id.ToString()) } $i++ } } } #region resource generator code $UpdateParameters.Add('@odata.type', '#microsoft.graph.qrCodePinAuthenticationMethodConfiguration') Update-MgBetaPolicyAuthenticationMethodPolicyAuthenticationMethodConfiguration ` -AuthenticationMethodConfigurationId $currentInstance.Id ` -BodyParameter $UpdateParameters #endregion } elseif ($Ensure -eq 'Absent' -and $currentInstance.Ensure -eq 'Present') { Write-Verbose -Message "Removing the Azure AD Authentication Method Policy QR Code Image with Id {$($currentInstance.Id)}" #region resource generator code Remove-MgBetaPolicyAuthenticationMethodPolicyAuthenticationMethodConfiguration -AuthenticationMethodConfigurationId $currentInstance.Id #endregion } } function Test-TargetResource { [CmdletBinding()] [OutputType([System.Boolean])] param ( [Parameter(Mandatory = $true)] [System.String] $Id, [Parameter()] [ValidateSet('enabled', 'disabled')] [System.String] $State, [Parameter()] [Microsoft.Management.Infrastructure.CimInstance[]] $ExcludeTargets, [Parameter()] [Microsoft.Management.Infrastructure.CimInstance[]] $IncludeTargets, [Parameter()] [System.UInt32] $StandardQRCodeLifetimeInDays, [Parameter()] [ValidateRange(8,20)] [System.UInt32] $PinLength, [Parameter()] [ValidateSet('Present', 'Absent')] [System.String] $Ensure = 'Present', [Parameter()] [System.Management.Automation.PSCredential] $Credential, [Parameter()] [System.String] $ApplicationId, [Parameter()] [System.String] $TenantId, [Parameter()] [System.String] $CertificateThumbprint, [Parameter()] [Switch] $ManagedIdentity, [Parameter()] [System.String[]] $AccessTokens ) #Ensure the proper dependencies are installed in the current environment. Confirm-M365DSCDependencies #region Telemetry $ResourceName = $MyInvocation.MyCommand.ModuleName.Replace('MSFT_', '') $CommandName = $MyInvocation.MyCommand $data = Format-M365DSCTelemetryParameters -ResourceName $ResourceName ` -CommandName $CommandName ` -Parameters $PSBoundParameters Add-M365DSCTelemetryEvent -Data $data #endregion $CurrentValues = Get-TargetResource @PSBoundParameters $ValuesToCheck = ([Hashtable]$PSBoundParameters).Clone() Write-Verbose -Message "Current Values: $(Convert-M365DscHashtableToString -Hashtable $CurrentValues)" Write-Verbose -Message "Target Values: $(Convert-M365DscHashtableToString -Hashtable $ValuesToCheck)" $testResult = Test-M365DSCParameterState -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()] [System.Management.Automation.PSCredential] $Credential, [Parameter()] [System.String] $ApplicationId, [Parameter()] [System.String] $TenantId, [Parameter()] [System.Management.Automation.PSCredential] $ApplicationSecret, [Parameter()] [System.String] $CertificateThumbprint, [Parameter()] [Switch] $ManagedIdentity, [Parameter()] [System.String[]] $AccessTokens ) $ConnectionMode = New-M365DSCConnection -Workload 'MicrosoftGraph' ` -InboundParameters $PSBoundParameters #Ensure the proper dependencies are installed in the current environment. Confirm-M365DSCDependencies #region Telemetry $ResourceName = $MyInvocation.MyCommand.ModuleName.Replace('MSFT_', '') $CommandName = $MyInvocation.MyCommand $data = Format-M365DSCTelemetryParameters -ResourceName $ResourceName ` -CommandName $CommandName ` -Parameters $PSBoundParameters Add-M365DSCTelemetryEvent -Data $data #endregion try { $Script:ExportMode = $true $uri = (Get-MSCloudLoginConnectionProfile -Workload MicrosoftGraph).ResourceUrl + "beta/policies/authenticationMethodsPolicy/authenticationMethodConfigurations/qrCodePin" $response = Invoke-MgGraphRequest -Uri $uri -Method GET [array] $Script:exportedInstances = $response $i = 1 $dscContent = '' if ($Script:exportedInstances.Length -eq 0) { Write-Host $Global:M365DSCEmojiGreenCheckMark } else { Write-Host "`r`n" -NoNewline } foreach ($config in $Script:exportedInstances) { if ($null -ne $Global:M365DSCExportResourceInstancesCount) { $Global:M365DSCExportResourceInstancesCount++ } $displayedKey = $config.Id Write-Host " |---[$i/$($Script:exportedInstances.Count)] $displayedKey" -NoNewline $params = @{ Id = $response.Id Credential = $Credential ApplicationId = $ApplicationId TenantId = $TenantId CertificateThumbprint = $CertificateThumbprint ManagedIdentity = $ManagedIdentity.IsPresent AccessTokens = $AccessTokens } $Results = Get-TargetResource @Params if ($null -ne $Results.ExcludeTargets) { $complexTypeStringResult = Get-M365DSCDRGComplexTypeToString ` -ComplexObject $Results.ExcludeTargets ` -CIMInstanceName 'AADAuthenticationMethodPolicyQRCodeImageExcludeTarget' if (-Not [String]::IsNullOrWhiteSpace($complexTypeStringResult)) { $Results.ExcludeTargets = $complexTypeStringResult } else { $Results.Remove('ExcludeTargets') | Out-Null } } if ($null -ne $Results.IncludeTargets) { $complexTypeStringResult = Get-M365DSCDRGComplexTypeToString ` -ComplexObject $Results.IncludeTargets ` -CIMInstanceName 'AADAuthenticationMethodPolicyQRCodeImageIncludeTarget' if (-Not [String]::IsNullOrWhiteSpace($complexTypeStringResult)) { $Results.IncludeTargets = $complexTypeStringResult } else { $Results.Remove('IncludeTargets') | Out-Null } } $currentDSCBlock = Get-M365DSCExportContentForResource -ResourceName $ResourceName ` -ConnectionMode $ConnectionMode ` -ModulePath $PSScriptRoot ` -Results $Results ` -Credential $Credential ` -NoEscape @('ExcludeTargets', 'IncludeTargets') $dscContent += $currentDSCBlock Save-M365DSCPartialExport -Content $currentDSCBlock ` -FileName $Global:PartialExportFileName $i++ Write-Host $Global:M365DSCEmojiGreenCheckMark } return $dscContent } catch { Write-Host $Global:M365DSCEmojiRedX New-M365DSCLogEntry -Message 'Error during Export:' ` -Exception $_ ` -Source $($MyInvocation.MyCommand.Source) ` -TenantId $TenantId ` -Credential $Credential return '' } } Export-ModuleMember -Function *-TargetResource |