From fb135c499be46e75b9c84e3e67d5872b8f85e9ab Mon Sep 17 00:00:00 2001 From: Steve Mutungi <132555836+SteveMutungi254@users.noreply.github.com> Date: Tue, 18 Mar 2025 13:06:50 +0000 Subject: [PATCH 01/12] Initial commit - group updates --- .../Groups/Update-EntraGroup.ps1 | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 diff --git a/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 b/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 new file mode 100644 index 0000000000..461291a604 --- /dev/null +++ b/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 @@ -0,0 +1,99 @@ +function Update-EntraGroup { + [CmdletBinding(SupportsShouldProcess, + ConfirmImpact = 'Medium', + DefaultParameterSetName = 'Default')] + param ( + [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "The unique identifier for the group, such as its object ID.")] + [Alias("ObjectId", "Id")] + [ValidateNotNullOrEmpty()] + [string]$GroupId, + + [Parameter(HelpMessage = "The description of the group.")] + [string]$Description, + + [Parameter(HelpMessage = "The display name of the group.")] + [string]$DisplayName, + + [Parameter(HelpMessage = "Specifies the group types and its membership. Possible values are 'Unified' or 'DynamicMembership'.")] + [string[]]$GroupTypes, + + [Parameter(HelpMessage = "Specifies whether the group is mail-enabled.")] + [bool]$MailEnabled, + + [Parameter(HelpMessage = "The mail nickname for the group.")] + [string]$MailNickname, + + [Parameter(HelpMessage = "Specifies whether the group is a security group.")] + [bool]$SecurityEnabled, + + [Parameter(HelpMessage = "A list of SMTP proxy addresses for the group, including the primary SMTP address.")] + [string[]]$ProxyAddresses, + + [Parameter(HelpMessage = "Specifies whether the group is visible in the address list.")] + [bool]$Visibility, + + [Parameter(Mandatory = $false, HelpMessage = "A hashtable of additional group properties to update.")] + [Alias("BodyParameter", "Body", "BodyParameters")] + [hashtable]$AdditionalProperties = @{} + ) + + begin { + + # Ensure Microsoft Entra PowerShell module is available + if (-not (Get-Module -ListAvailable -Name Microsoft.Entra.Groups)) { + Write-Error "Microsoft.Entra module is required. Install it using 'Install-Module Microsoft.Entra.Groups'. See http://aka.ms/entra/ps/installation for more details." + return + } + + # Ensure connection to Microsoft Entra + if (-not (Get-EntraContext)) { + Write-Error "Not connected to Microsoft Graph. Use 'Connect-Entra -Scopes Group.ReadWrite.All' to authenticate." + return + } + + # Process business logic + + $graphUri = "https://graph.microsoft.com/v1.0/groups/$GroupId" + $GroupProperties = @{} + + $CommonParameters = @("Verbose", "Debug", "WarningAction", "WarningVariable", "ErrorAction", "ErrorVariable", "OutVariable", "OutBuffer", "WhatIf", "Confirm") + + foreach ($param in $PSBoundParameters.Keys) { + if ($param -ne "GroupId" -and $param -ne "AdditionalProperties" -and $CommonParameters -notcontains $param) { + $GroupProperties[$param] = $PSBoundParameters[$param] + } + } + + foreach ($key in $AdditionalProperties.Keys) { + $GroupProperties[$key] = $AdditionalProperties[$key] + } + + $bodyJson = $GroupProperties | ConvertTo-Json -Depth 2 + } + + process { + $customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand + + if ($GroupProperties.Count -eq 0) { + Write-Warning "No properties provided for update. Exiting." + return + } + + if ($PSCmdlet.ShouldProcess($GroupId, "Updating group properties via Microsoft Graph API")) { + try { + Invoke-MgGraphRequest -Uri $graphUri -Method PATCH -Body $bodyJson -Headers $customHeaders + Write-Verbose "Properties for group $GroupId updated successfully. Updated properties: $($GroupProperties | Out-String)" + } + catch { + $errorDetails = @{ + 'ErrorMessage' = $_.Exception.Message + 'RequestUri' = $graphUri + 'RequestBody' = $bodyJson + 'ErrorResponse' = $_.ErrorDetails.Message + } + Write-Debug "Error Details: $($errorDetails | ConvertTo-Json)" + Write-Error "Failed to update group properties: $_" + } + } + } +} From e80b5e0dab28afe04e2f0044265fa9519b79817b Mon Sep 17 00:00:00 2001 From: Steve Mutungi <132555836+SteveMutungi254@users.noreply.github.com> Date: Tue, 18 Mar 2025 13:29:56 +0000 Subject: [PATCH 02/12] Fix missing property error --- .../Microsoft.Entra/Groups/Update-EntraGroup.ps1 | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 b/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 index 461291a604..c32ee30dac 100644 --- a/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 +++ b/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 @@ -1,3 +1,8 @@ +# ------------------------------------------------------------------------------ +# Copyright (c) Microsoft Corporation. All Rights Reserved. +# Licensed under the MIT License. See License in the project root for license information. +# ------------------------------------------------------------------------------ + function Update-EntraGroup { [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium', @@ -51,8 +56,6 @@ function Update-EntraGroup { return } - # Process business logic - $graphUri = "https://graph.microsoft.com/v1.0/groups/$GroupId" $GroupProperties = @{} @@ -68,13 +71,17 @@ function Update-EntraGroup { $GroupProperties[$key] = $AdditionalProperties[$key] } + if (-not $GroupProperties) { + $GroupProperties = @{} + } + $bodyJson = $GroupProperties | ConvertTo-Json -Depth 2 } process { $customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand - if ($GroupProperties.Count -eq 0) { + if (-not $GroupProperties -or $GroupProperties.Count -eq 0) { Write-Warning "No properties provided for update. Exiting." return } From e88e0f0ef09c6e7288a112c6db20c470262079b1 Mon Sep 17 00:00:00 2001 From: Steve Mutungi <132555836+SteveMutungi254@users.noreply.github.com> Date: Tue, 18 Mar 2025 13:42:59 +0000 Subject: [PATCH 03/12] Minor updates --- .../Groups/Update-EntraGroup.ps1 | 47 +++++++++---------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 b/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 index c32ee30dac..c0304b5cea 100644 --- a/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 +++ b/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 @@ -49,58 +49,53 @@ function Update-EntraGroup { Write-Error "Microsoft.Entra module is required. Install it using 'Install-Module Microsoft.Entra.Groups'. See http://aka.ms/entra/ps/installation for more details." return } - + # Ensure connection to Microsoft Entra if (-not (Get-EntraContext)) { Write-Error "Not connected to Microsoft Graph. Use 'Connect-Entra -Scopes Group.ReadWrite.All' to authenticate." return } + # Microsoft Graph API URL for updating group properties $graphUri = "https://graph.microsoft.com/v1.0/groups/$GroupId" + + # Initialize hashtable for group properties $GroupProperties = @{} - + $CommonParameters = @("Verbose", "Debug", "WarningAction", "WarningVariable", "ErrorAction", "ErrorVariable", "OutVariable", "OutBuffer", "WhatIf", "Confirm") - + + # Merge individual parameters into GroupProperties foreach ($param in $PSBoundParameters.Keys) { if ($param -ne "GroupId" -and $param -ne "AdditionalProperties" -and $CommonParameters -notcontains $param) { $GroupProperties[$param] = $PSBoundParameters[$param] } } - + + # Merge AdditionalProperties if provided foreach ($key in $AdditionalProperties.Keys) { $GroupProperties[$key] = $AdditionalProperties[$key] } - - if (-not $GroupProperties) { - $GroupProperties = @{} - } - + + # Convert final update properties to JSON $bodyJson = $GroupProperties | ConvertTo-Json -Depth 2 } process { $customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand - - if (-not $GroupProperties -or $GroupProperties.Count -eq 0) { + if ($GroupProperties.Count -eq 0) { Write-Warning "No properties provided for update. Exiting." return } - if ($PSCmdlet.ShouldProcess($GroupId, "Updating group properties via Microsoft Graph API")) { - try { - Invoke-MgGraphRequest -Uri $graphUri -Method PATCH -Body $bodyJson -Headers $customHeaders - Write-Verbose "Properties for group $GroupId updated successfully. Updated properties: $($GroupProperties | Out-String)" - } - catch { - $errorDetails = @{ - 'ErrorMessage' = $_.Exception.Message - 'RequestUri' = $graphUri - 'RequestBody' = $bodyJson - 'ErrorResponse' = $_.ErrorDetails.Message - } - Write-Debug "Error Details: $($errorDetails | ConvertTo-Json)" - Write-Error "Failed to update group properties: $_" - } + + try { + # Invoke Microsoft Graph API Request + Invoke-MgGraphRequest -Uri $graphUri -Method PATCH -Body $bodyJson -Headers $customHeaders + Write-Verbose "Properties for group $GroupId updated successfully. Updated properties: $($GroupProperties | Out-String)" + } + catch { + Write-Debug "Error Details: $_" + Write-Error "Failed to update group properties: $_" } } } From eda526a681e431ab2b144bccd2aaa277c2f3ffab Mon Sep 17 00:00:00 2001 From: stevemutungi Date: Tue, 18 Mar 2025 16:50:09 +0300 Subject: [PATCH 04/12] Removed sub-module checks. --- module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 | 6 ------ 1 file changed, 6 deletions(-) diff --git a/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 b/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 index c0304b5cea..d0063b177a 100644 --- a/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 +++ b/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 @@ -44,12 +44,6 @@ function Update-EntraGroup { begin { - # Ensure Microsoft Entra PowerShell module is available - if (-not (Get-Module -ListAvailable -Name Microsoft.Entra.Groups)) { - Write-Error "Microsoft.Entra module is required. Install it using 'Install-Module Microsoft.Entra.Groups'. See http://aka.ms/entra/ps/installation for more details." - return - } - # Ensure connection to Microsoft Entra if (-not (Get-EntraContext)) { Write-Error "Not connected to Microsoft Graph. Use 'Connect-Entra -Scopes Group.ReadWrite.All' to authenticate." From 557a4adb40749c79d45ad676cef62753da4ae8ae Mon Sep 17 00:00:00 2001 From: Kennedy Kangethe Munga Date: Wed, 19 Mar 2025 11:26:06 +0300 Subject: [PATCH 05/12] Add tests --- test/Entra/Groups/Update-EntraGroup.Tests.ps1 | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 test/Entra/Groups/Update-EntraGroup.Tests.ps1 diff --git a/test/Entra/Groups/Update-EntraGroup.Tests.ps1 b/test/Entra/Groups/Update-EntraGroup.Tests.ps1 new file mode 100644 index 0000000000..796cd01299 --- /dev/null +++ b/test/Entra/Groups/Update-EntraGroup.Tests.ps1 @@ -0,0 +1,61 @@ +# ------------------------------------------------------------------------------ +# Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. +# ------------------------------------------------------------------------------ + +BeforeAll { + if((Get-Module -Name Microsoft.Entra.Groups) -eq $null){ + Import-Module Microsoft.Entra.Groups + } + Import-Module (Join-Path $PSScriptRoot "..\..\Common-Functions.ps1") -Force + + Mock -CommandName Invoke-MgGraphRequest -MockWith {} -ModuleName Microsoft.Entra.Groups + Mock -CommandName Get-EntraContext -MockWith { @{Scopes = @("Group.ReadWrite.All")} } -ModuleName Microsoft.Entra.Groups +} + +Describe "Update-EntraGroup" { + Context "Test for Update-EntraGroup" { + It "Should return empty object" { + $result = Update-EntraGroup -GroupId bbbbbbbb-1111-2222-3333-cccccccccccc -DisplayName "demo" -MailEnabled $false -SecurityEnabled $true -MailNickName "demoNickname" -Description "test" + $result | Should -BeNullOrEmpty + + Should -Invoke -CommandName Invoke-MgGraphRequest -ModuleName Microsoft.Entra.Groups -Times 1 + } + It "Should execute successfully with Alias" { + $result = Update-EntraGroup -Id bbbbbbbb-1111-2222-3333-cccccccccccc -DisplayName "demo" -MailEnabled $false -SecurityEnabled $true -MailNickName "demoNickname" -Description "test" + $result | Should -BeNullOrEmpty + + Should -Invoke -CommandName Invoke-MgGraphRequest -ModuleName Microsoft.Entra.Groups -Times 1 + } + It "Should fail when GroupId is invalid" { + { Update-EntraGroup -GroupId "" } | Should -Throw "Cannot validate argument on parameter 'GroupId'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again." + } + It "Should fail when GroupId is empty" { + { Update-EntraGroup -GroupId } | Should -Throw "Missing an argument for parameter 'GroupId'*" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Update-EntraGroup" + + Update-EntraGroup -Id bbbbbbbb-1111-2222-3333-cccccccccccc -DisplayName "demo" + + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Update-EntraGroup" + Should -Invoke -CommandName Invoke-MgGraphRequest -ModuleName Microsoft.Entra.Groups -Times 1 -ParameterFilter { + $Headers.'User-Agent' | Should -Be $userAgentHeaderValue + $true + } + } + It "Should execute successfully without throwing an error " { + # Disable confirmation prompts + $originalDebugPreference = $DebugPreference + $DebugPreference = 'Continue' + + try { + # Act & Assert: Ensure the function doesn't throw an exception + { Update-EntraGroup -Id bbbbbbbb-1111-2222-3333-cccccccccccc } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } + } +} + From 751c4a6a5f8662a9c848e4c1d4877f440b1c5896 Mon Sep 17 00:00:00 2001 From: stevemutungi Date: Wed, 19 Mar 2025 11:52:16 +0300 Subject: [PATCH 06/12] Adding SupportShouldSupport code block checks --- .../Groups/Update-EntraGroup.ps1 | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 b/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 index d0063b177a..7150e0eae3 100644 --- a/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 +++ b/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 @@ -4,9 +4,7 @@ # ------------------------------------------------------------------------------ function Update-EntraGroup { - [CmdletBinding(SupportsShouldProcess, - ConfirmImpact = 'Medium', - DefaultParameterSetName = 'Default')] + [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium', DefaultParameterSetName = 'Default')] param ( [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "The unique identifier for the group, such as its object ID.")] [Alias("ObjectId", "Id")] @@ -49,6 +47,11 @@ function Update-EntraGroup { Write-Error "Not connected to Microsoft Graph. Use 'Connect-Entra -Scopes Group.ReadWrite.All' to authenticate." return } + + } + + process { + $customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand # Microsoft Graph API URL for updating group properties $graphUri = "https://graph.microsoft.com/v1.0/groups/$GroupId" @@ -72,24 +75,23 @@ function Update-EntraGroup { # Convert final update properties to JSON $bodyJson = $GroupProperties | ConvertTo-Json -Depth 2 - } - process { - $customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand if ($GroupProperties.Count -eq 0) { Write-Warning "No properties provided for update. Exiting." return } - try { - # Invoke Microsoft Graph API Request - Invoke-MgGraphRequest -Uri $graphUri -Method PATCH -Body $bodyJson -Headers $customHeaders - Write-Verbose "Properties for group $GroupId updated successfully. Updated properties: $($GroupProperties | Out-String)" - } - catch { - Write-Debug "Error Details: $_" - Write-Error "Failed to update group properties: $_" + if ($PSCmdlet.ShouldProcess("Group with ID '$GroupId'", "Update the following properties: $($GroupProperties.Keys -join ', ')")) { + try { + # Invoke Microsoft Graph API Request + Invoke-MgGraphRequest -Uri $graphUri -Method PATCH -Body $bodyJson -Headers $customHeaders + Write-Verbose "Properties for group $GroupId updated successfully. Updated properties: $($GroupProperties | Out-String)" + } + catch { + Write-Debug "Error Details: $_" + Write-Error "Failed to update group properties: $_" + } } } } From 561f7cd00f0863a1d4d7d4843df744814189247c Mon Sep 17 00:00:00 2001 From: stevemutungi Date: Wed, 19 Mar 2025 12:09:59 +0300 Subject: [PATCH 07/12] Add errorAction --- module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 b/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 index 7150e0eae3..56e9553aeb 100644 --- a/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 +++ b/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 @@ -44,7 +44,8 @@ function Update-EntraGroup { # Ensure connection to Microsoft Entra if (-not (Get-EntraContext)) { - Write-Error "Not connected to Microsoft Graph. Use 'Connect-Entra -Scopes Group.ReadWrite.All' to authenticate." + $errorMessage = "Not connected to Microsoft Graph. Use 'Connect-Entra -Scopes Group.ReadWrite.All' to authenticate." + Write-Error -Message $errorMessage -ErrorAction Stop return } From ed78c0951059deb6d8713c00564e84309821aeb3 Mon Sep 17 00:00:00 2001 From: stevemutungi Date: Wed, 19 Mar 2025 21:01:49 +0300 Subject: [PATCH 08/12] Updating data type from bool to string --- module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 b/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 index 56e9553aeb..b0f0e095b8 100644 --- a/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 +++ b/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 @@ -33,7 +33,7 @@ function Update-EntraGroup { [string[]]$ProxyAddresses, [Parameter(HelpMessage = "Specifies whether the group is visible in the address list.")] - [bool]$Visibility, + [string]$Visibility, [Parameter(Mandatory = $false, HelpMessage = "A hashtable of additional group properties to update.")] [Alias("BodyParameter", "Body", "BodyParameters")] From f75376d1263bffac9cff51668132ff5d735274ad Mon Sep 17 00:00:00 2001 From: stevemutungi Date: Wed, 19 Mar 2025 21:08:31 +0300 Subject: [PATCH 09/12] Adding IsAssignableToRole param --- module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 b/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 index b0f0e095b8..da9dc2fc21 100644 --- a/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 +++ b/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 @@ -29,6 +29,9 @@ function Update-EntraGroup { [Parameter(HelpMessage = "Specifies whether the group is a security group.")] [bool]$SecurityEnabled, + [Parameter(HelpMessage = "Specifies whether the group can be assigned to a Microsoft Entra role.")] + [bool]$IsAssignableToRole, + [Parameter(HelpMessage = "A list of SMTP proxy addresses for the group, including the primary SMTP address.")] [string[]]$ProxyAddresses, From 47cee0768c8b285e93e07150324796de62a6bf22 Mon Sep 17 00:00:00 2001 From: stevemutungi Date: Wed, 19 Mar 2025 21:18:40 +0300 Subject: [PATCH 10/12] Enriching HelpMessage information --- module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 b/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 index da9dc2fc21..7c051488fb 100644 --- a/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 +++ b/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 @@ -17,7 +17,7 @@ function Update-EntraGroup { [Parameter(HelpMessage = "The display name of the group.")] [string]$DisplayName, - [Parameter(HelpMessage = "Specifies the group types and its membership. Possible values are 'Unified' or 'DynamicMembership'.")] + [Parameter(HelpMessage = "Defines the group type and membership. Possible values: 'Unified' (Microsoft 365 group) or 'DynamicMembership'. If not 'Unified', the group is a security or distribution group.")] [string[]]$GroupTypes, [Parameter(HelpMessage = "Specifies whether the group is mail-enabled.")] @@ -29,7 +29,7 @@ function Update-EntraGroup { [Parameter(HelpMessage = "Specifies whether the group is a security group.")] [bool]$SecurityEnabled, - [Parameter(HelpMessage = "Specifies whether the group can be assigned to a Microsoft Entra role.")] + [Parameter(HelpMessage = "Indicates if the group can be assigned to a Microsoft Entra role (only for security groups). This setting is fixed at creation and cannot be changed.")] [bool]$IsAssignableToRole, [Parameter(HelpMessage = "A list of SMTP proxy addresses for the group, including the primary SMTP address.")] From b2db469bffda201c415b779c748a0684f55c2428 Mon Sep 17 00:00:00 2001 From: Kennedy Kangethe Munga Date: Thu, 20 Mar 2025 10:52:58 +0300 Subject: [PATCH 11/12] Add WhatIf param test --- test/Entra/Groups/Update-EntraGroup.Tests.ps1 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/Entra/Groups/Update-EntraGroup.Tests.ps1 b/test/Entra/Groups/Update-EntraGroup.Tests.ps1 index 796cd01299..4c68da4574 100644 --- a/test/Entra/Groups/Update-EntraGroup.Tests.ps1 +++ b/test/Entra/Groups/Update-EntraGroup.Tests.ps1 @@ -31,7 +31,11 @@ Describe "Update-EntraGroup" { } It "Should fail when GroupId is empty" { { Update-EntraGroup -GroupId } | Should -Throw "Missing an argument for parameter 'GroupId'*" - } + } + It "Should execute successfully with WhatIf parameter" { + Update-EntraGroup -GroupId bbbbbbbb-1111-2222-3333-cccccccccccc -DisplayName "demo" -WhatIf + Should -Invoke -CommandName Invoke-MgGraphRequest -ModuleName Microsoft.Entra.Groups -Times 1 + } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Update-EntraGroup" From 51fb1eab4212e15b4a3a684ad5aa139aabe2dec8 Mon Sep 17 00:00:00 2001 From: Kennedy Kangethe Munga Date: Fri, 21 Mar 2025 18:26:45 +0300 Subject: [PATCH 12/12] Minor changes --- module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 | 6 +++--- test/Entra/Groups/Update-EntraGroup.Tests.ps1 | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 b/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 index 7c051488fb..836cf45149 100644 --- a/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 +++ b/module/Entra/Microsoft.Entra/Groups/Update-EntraGroup.ps1 @@ -77,14 +77,14 @@ function Update-EntraGroup { $GroupProperties[$key] = $AdditionalProperties[$key] } - # Convert final update properties to JSON - $bodyJson = $GroupProperties | ConvertTo-Json -Depth 2 - if ($GroupProperties.Count -eq 0) { Write-Warning "No properties provided for update. Exiting." return } + # Convert final update properties to JSON + $bodyJson = $GroupProperties | ConvertTo-Json -Depth 2 + if ($PSCmdlet.ShouldProcess("Group with ID '$GroupId'", "Update the following properties: $($GroupProperties.Keys -join ', ')")) { try { diff --git a/test/Entra/Groups/Update-EntraGroup.Tests.ps1 b/test/Entra/Groups/Update-EntraGroup.Tests.ps1 index 4c68da4574..3af4a266a6 100644 --- a/test/Entra/Groups/Update-EntraGroup.Tests.ps1 +++ b/test/Entra/Groups/Update-EntraGroup.Tests.ps1 @@ -32,9 +32,9 @@ Describe "Update-EntraGroup" { It "Should fail when GroupId is empty" { { Update-EntraGroup -GroupId } | Should -Throw "Missing an argument for parameter 'GroupId'*" } - It "Should execute successfully with WhatIf parameter" { + It "Should not execute Invoke-MgGraphRequest with WhatIf parameter" { Update-EntraGroup -GroupId bbbbbbbb-1111-2222-3333-cccccccccccc -DisplayName "demo" -WhatIf - Should -Invoke -CommandName Invoke-MgGraphRequest -ModuleName Microsoft.Entra.Groups -Times 1 + Should -Invoke -CommandName Invoke-MgGraphRequest -ModuleName Microsoft.Entra.Groups -Times 0 } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Update-EntraGroup"