From 25e79e4cebc8a7246943277320289c508c17b73f Mon Sep 17 00:00:00 2001 From: Emmanuel Ng'ang'a <60355631+emmanuel-karanja@users.noreply.github.com> Date: Thu, 31 Oct 2024 09:33:52 +0300 Subject: [PATCH 01/10] Updated Usability params for beta --- .../Set-EntraBetaUserLicense.ps1 | 15 +++++++--- .../Set-EntraBetaUserPassword.ps1 | 18 +++++++++--- .../Set-EntraBetaUserExtension.md | 24 ++++++++-------- .../Set-EntraBetaUserLicense.md | 28 +++++++++---------- .../Set-EntraBetaUserPassword.md | 16 +++++------ 5 files changed, 59 insertions(+), 42 deletions(-) diff --git a/module/EntraBeta/customizations/Set-EntraBetaUserLicense.ps1 b/module/EntraBeta/customizations/Set-EntraBetaUserLicense.ps1 index 0616b2383c..33ee30320c 100644 --- a/module/EntraBeta/customizations/Set-EntraBetaUserLicense.ps1 +++ b/module/EntraBeta/customizations/Set-EntraBetaUserLicense.ps1 @@ -7,13 +7,20 @@ Parameters = $null Outputs = $null CustomScript = @' + param ( + [Alias("ObjectId")] + [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] + [System.String] $UserId, + [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] + [Microsoft.Open.AzureAD.Model.AssignedLicenses] $AssignedLicenses + ) PROCESS { $params = @{} $customHeaders = New-EntraBetaCustomHeaders -Command $MyInvocation.MyCommand - if($null -ne $PSBoundParameters["ObjectId"]) + if($null -ne $PSBoundParameters["UserId"]) { - $params["UserId"] = $PSBoundParameters["ObjectId"] - $UserId = $PSBoundParameters["ObjectId"] + $params["UserId"] = $PSBoundParameters["UserId"] + $UserId = $PSBoundParameters["UserId"] } $jsonBody = @{ addLicenses = @(if ($PSBoundParameters.AssignedLicenses.AddLicenses) { @@ -39,7 +46,7 @@ $response | ForEach-Object { if($null -ne $_) { - Add-Member -InputObject $_ -MemberType AliasProperty -Name ObjectId -Value Id + Add-Member -InputObject $_ -MemberType AliasProperty -Name UserId -Value Id } } $response diff --git a/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 b/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 index 3ee7710748..95db4b4381 100644 --- a/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 +++ b/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 @@ -7,13 +7,23 @@ Parameters = $null Outputs = $null CustomScript = @' + param ( + [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] + [System.Boolean] $ForceChangePasswordNextLogin, + [Alias("ObjectId")] + [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] + [System.String] $UserId, + [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] + [System.Security.SecureString] $Password, + [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] + [System.Boolean] $EnforceChangePasswordPolicy + ) PROCESS { $params = @{} - $customHeaders = New-EntraBetaCustomHeaders -Command $MyInvocation.MyCommand - - if($null -ne $PSBoundParameters["ObjectId"]) + $customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand + if($null -ne $PSBoundParameters["UserId"]) { - $userId = $PSBoundParameters["ObjectId"] + $userId = $PSBoundParameters["UserId"] } if($PSBoundParameters.ContainsKey("Verbose")) { diff --git a/module/docs/entra-powershell-beta/Microsoft.Graph.Entra.Beta/Set-EntraBetaUserExtension.md b/module/docs/entra-powershell-beta/Microsoft.Graph.Entra.Beta/Set-EntraBetaUserExtension.md index c232117708..94c788cd60 100644 --- a/module/docs/entra-powershell-beta/Microsoft.Graph.Entra.Beta/Set-EntraBetaUserExtension.md +++ b/module/docs/entra-powershell-beta/Microsoft.Graph.Entra.Beta/Set-EntraBetaUserExtension.md @@ -28,8 +28,8 @@ Sets a user extension. ```powershell Set-EntraBetaUserExtension - -ExtensionName - -ObjectId + -ExtensionId + -UserId -ExtensionValue [] ``` @@ -38,7 +38,7 @@ Set-EntraBetaUserExtension ```powershell Set-EntraBetaUserExtension - -ObjectId + -UserId -ExtensionNameValues [] ``` @@ -54,8 +54,8 @@ The `Set-EntraBetaUserExtension` cmdlet updates a user extension in Microsoft En ```powershell Connect-Entra -Scopes 'User.ReadWrite.All' $params = @{ - ObjectId = 'SawyerM@contoso.com' - ExtensionName = 'extension_e5e29b8a85d941eab8d12162bd004528_extensionAttribute8' + UserId = 'SawyerM@contoso.com' + ExtensionId = 'extension_e5e29b8a85d941eab8d12162bd004528_extensionAttribute8' ExtensionValue = 'New Value' } Set-EntraBetaUserExtension @params @@ -63,15 +63,15 @@ Set-EntraBetaUserExtension @params This example shows how to update the value of the extension attribute for a specified user. -- `-ObjectId` parameter specifies the user Id. -- `-ExtensionName` parameter specifies the name of an extension. -- `-ExtensionValue` parameter specifies the extension name values. +- `-UserId` parameter specifies the user Id. +- `-ExtensionId` parameter specifies the Id of an extension. +- `-ExtensionValue` parameter specifies the extension Id values. ## Parameters -### -ExtensionName +### -ExtensionId -Specifies the name of an extension. +Specifies the Id of an extension. ```yaml Type: System.String @@ -117,9 +117,9 @@ Accept pipeline input: True (ByPropertyName, ByValue) Accept wildcard characters: False ``` -### -ObjectId +### -UserId -Specifies the ID of an object. +Specifies the ID of a User. ```yaml Type: System.String diff --git a/module/docs/entra-powershell-beta/Microsoft.Graph.Entra.Beta/Set-EntraBetaUserLicense.md b/module/docs/entra-powershell-beta/Microsoft.Graph.Entra.Beta/Set-EntraBetaUserLicense.md index 3b653a5d4d..f9182b77a5 100644 --- a/module/docs/entra-powershell-beta/Microsoft.Graph.Entra.Beta/Set-EntraBetaUserLicense.md +++ b/module/docs/entra-powershell-beta/Microsoft.Graph.Entra.Beta/Set-EntraBetaUserLicense.md @@ -26,7 +26,7 @@ Adds or removes licenses for a Microsoft online service to the list of assigned ```powershell Set-EntraBetaUserLicense - -ObjectId + -UserId -AssignedLicenses [] ``` @@ -50,13 +50,13 @@ For delegated scenarios, the calling user needs at least one of the following Mi ```powershell Connect-Entra -Scopes 'User.ReadWrite.All' -$LicensedUser = Get-EntraBetaUser -ObjectId 'TemplateUser@contoso.com' +$LicensedUser = Get-EntraBetaUser -UserId 'TemplateUser@contoso.com' $License = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense $License.SkuId = $LicensedUser.AssignedLicenses.SkuId $Licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses $Licenses.AddLicenses = $License $Params = @{ - ObjectId = 'SawyerM@contoso.com' + UserId = 'SawyerM@contoso.com' AssignedLicenses = $Licenses } Set-EntraBetaUserLicense @Params @@ -82,15 +82,15 @@ isLicenseReconciliationNeeded False This example demonstrates how to assign a license to a user based on a template user. -- `-ObjectId` parameter specifies the object Id of a user(as a UserPrincipalName or ObjectId). +- `-UserId` parameter specifies the Id of a user(as a UserPrincipalName or UserId). - `-AssignedLicenses` parameter specifies a list of licenses to assign or remove. ### Example 2: Add a license to a user by copying license from another user ```powershell Connect-Entra -Scopes 'User.ReadWrite.All' -$LicensedUser = Get-EntraBetaUser -ObjectId 'AdeleV@contoso.com' -$User = Get-EntraBetaUser -ObjectId 'SawyerM@contoso.com' +$LicensedUser = Get-EntraBetaUser -UserId 'AdeleV@contoso.com' +$User = Get-EntraBetaUser -UserId 'SawyerM@contoso.com' $License1 = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense $License1.SkuId = $LicensedUser.AssignedLicenses.SkuId[0] $License2 = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense @@ -101,7 +101,7 @@ $addLicensesArray += $License2 $Licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses $Licenses.AddLicenses = $addLicensesArray $Params = @{ - ObjectId = $User.ObjectId + UserId = $User.UserId AssignedLicenses = $Licenses } Set-EntraBetaUserLicense @Params @@ -127,7 +127,7 @@ isLicenseReconciliationNeeded False This example demonstrates how to assign a license to a user by copying license from another user. -- `-ObjectId` parameter specifies the object Id of a user(as a UserPrincipalName or ObjectId). +- `-UserId` parameter specifies the Id of a user(as a UserPrincipalName or UserId). - `-AssignedLicenses` parameter specifies a list of licenses to assign or remove. ### Example 3: Remove an assigned User's License @@ -135,11 +135,11 @@ This example demonstrates how to assign a license to a user by copying license f ```powershell Connect-Entra -Scopes 'User.ReadWrite.All' $UserPrincipalName = 'SawyerM@contoso.com' -$User = Get-EntraBetaUser -ObjectId $UserPrincipalName -$SkuId = (Get-EntraBetaUserLicenseDetail -ObjectId $UserPrincipalName).SkuId +$User = Get-EntraBetaUser -UserId $UserPrincipalName +$SkuId = (Get-EntraBetaUserLicenseDetail -UserId $UserPrincipalName).SkuId $Licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses $Licenses.RemoveLicenses = $SkuId -Set-EntraBetaUserLicense -ObjectId $User.ObjectId -AssignedLicenses $Licenses +Set-EntraBetaUserLicense -UserId $User.UserId -AssignedLicenses $Licenses ``` ```Output @@ -161,7 +161,7 @@ givenName Sawyer This example demonstrates how to remove a user's license by retrieving the user details. -- `-ObjectId` parameter specifies the object Id of a user(as a UserPrincipalName or ObjectId). +- `-UserId` parameter specifies the Id of a user(as a UserPrincipalName or UserId). - `-AssignedLicenses` parameter specifies a list of licenses to assign or remove. ## Parameters @@ -182,9 +182,9 @@ Accept pipeline input: True (ByPropertyName, ByValue) Accept wildcard characters: False ``` -### -ObjectId +### -UserId -Specifies the ID of a user (as a UserPrincipalName or ObjectId) in Microsoft Entra ID. +Specifies the ID of a user (as a UserPrincipalName or UserId) in Microsoft Entra ID. ```yaml Type: System.String diff --git a/module/docs/entra-powershell-beta/Microsoft.Graph.Entra.Beta/Set-EntraBetaUserPassword.md b/module/docs/entra-powershell-beta/Microsoft.Graph.Entra.Beta/Set-EntraBetaUserPassword.md index 6f56024d9c..30f94ee0e9 100644 --- a/module/docs/entra-powershell-beta/Microsoft.Graph.Entra.Beta/Set-EntraBetaUserPassword.md +++ b/module/docs/entra-powershell-beta/Microsoft.Graph.Entra.Beta/Set-EntraBetaUserPassword.md @@ -26,7 +26,7 @@ Sets the password of a user. ```powershell Set-EntraBetaUserPassword - -ObjectId + -UserId -Password [-ForceChangePasswordNextLogin ] [-EnforceChangePasswordPolicy ] @@ -47,12 +47,12 @@ Any user can update their password without belonging to any administrator role. Connect-Entra -Scopes 'Directory.AccessAsUser.All' $newPassword = '' $securePassword = ConvertTo-SecureString $newPassword -AsPlainText -Force -Set-EntraBetaUserPassword -ObjectId 'SawyerM@contoso.com' -Password $securePassword +Set-EntraBetaUserPassword -UserId 'SawyerM@contoso.com' -Password $securePassword ``` This command sets the specified user's password. -- `-ObjectId` parameter specifies the ID of a user in Microsoft Entra ID. +- `-UserId` parameter specifies the ID of a user in Microsoft Entra ID. - `-Password` parameter specifies the password to set. ### Example 2: Set a user's password with EnforceChangePasswordPolicy parameter @@ -61,12 +61,12 @@ This command sets the specified user's password. Connect-Entra -Scopes 'Directory.AccessAsUser.All' $newPassword= '' $securePassword = ConvertTo-SecureString $newPassword -AsPlainText -Force -Set-EntraBetaUserPassword -ObjectId 'SawyerM@contoso.com' -Password $securePassword -EnforceChangePasswordPolicy $True +Set-EntraBetaUserPassword -UserId 'SawyerM@contoso.com' -Password $securePassword -EnforceChangePasswordPolicy $True ``` This command sets the specified user's password with EnforceChangePasswordPolicy parameter. -- `-ObjectId` parameter specifies the ID of a user in Microsoft Entra ID. +- `-UserId` parameter specifies the ID of a user in Microsoft Entra ID. - `-Password` parameter specifies the password to set. - `-EnforceChangePasswordPolicy` parameter force the user to change their password, if set to true. @@ -81,7 +81,7 @@ Set-EntraBetaUserPassword -ObjectId 'SawyerM@contoso.com' -Password $securePassw This command sets the specified user's password with ForceChangePasswordNextLogin parameter. -- `-ObjectId` parameter specifies the ID of a user in Microsoft Entra ID. +- `-UserId` parameter specifies the ID of a user in Microsoft Entra ID. - `-Password` parameter specifies the password to set. - `-ForceChangePasswordNextLogin` parameter forces a user to change their password during their next log in. @@ -119,9 +119,9 @@ Accept pipeline input: True (ByPropertyName, ByValue) Accept wildcard characters: False ``` -### -ObjectId +### -UserId -Specifies the ID of an object. +Specifies the UserId ```yaml Type: System.String From ec4b6faf789ff06ec984e480c43fdad87ae0b2af Mon Sep 17 00:00:00 2001 From: Emmanuel Ng'ang'a <60355631+emmanuel-karanja@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:30:29 +0300 Subject: [PATCH 02/10] Updated Usability for Beta --- src/CompatibilityAdapterBuilder.ps1 | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/CompatibilityAdapterBuilder.ps1 b/src/CompatibilityAdapterBuilder.ps1 index d47b584dc2..75fcc92be7 100644 --- a/src/CompatibilityAdapterBuilder.ps1 +++ b/src/CompatibilityAdapterBuilder.ps1 @@ -86,7 +86,6 @@ class CompatibilityAdapterBuilder { 'Get-EntraBetaApplicationTemplate', 'Select-EntraBetaGroupIdsContactIsMemberOf', 'Set-EntraBetaCustomSecurityAttributeDefinitionAllowedValue', - 'Set-EntraBetaUserLicense', 'Set-EntraBetaTrustFrameworkPolicy', 'Remove-EntraBetaUserAppRoleAssignment', 'Get-EntraBetaApplicationPolicy', @@ -128,7 +127,6 @@ class CompatibilityAdapterBuilder { 'Set-EntraBetaPolicy', 'Set-EntraBetaCustomSecurityAttributeDefinition', 'Get-EntraBetaPrivilegedResource', - 'Set-EntraBetaUserPassword', 'New-EntraBetaApplicationFromApplicationTemplate', 'Set-EntraBetaPrivilegedRoleSetting', 'Remove-EntraBetaApplicationKey', From b17fc257c439d8000d7a0cba18e7af9613ba18df Mon Sep 17 00:00:00 2001 From: Emmanuel Ng'ang'a <60355631+emmanuel-karanja@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:48:45 +0300 Subject: [PATCH 03/10] Added tests --- .../Set-EntraBetaUserLicense.Tests.ps1 | 124 ++++++++++++++++++ .../Set-EntraBetaUserPassword.Tests.ps1 | 122 +++++++++++++++++ 2 files changed, 246 insertions(+) create mode 100644 test/module/EntraBeta/Set-EntraBetaUserLicense.Tests.ps1 create mode 100644 test/module/EntraBeta/Set-EntraBetaUserPassword.Tests.ps1 diff --git a/test/module/EntraBeta/Set-EntraBetaUserLicense.Tests.ps1 b/test/module/EntraBeta/Set-EntraBetaUserLicense.Tests.ps1 new file mode 100644 index 0000000000..5e92f3636c --- /dev/null +++ b/test/module/EntraBeta/Set-EntraBetaUserLicense.Tests.ps1 @@ -0,0 +1,124 @@ +# ------------------------------------------------------------------------------ +# 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.Graph.Entra.Beta) -eq $null) { + Import-Module Microsoft.Graph.Entra.Beta + } + Import-Module (Join-Path $psscriptroot "..\Common-Functions.ps1") -Force + + $scriptblock = { + # Write-Host "Mocking Set-EntraUserLicense with parameters: $($args | ConvertTo-Json -Depth 3)" + return @( + [PSCustomObject]@{ + userPrincipalName = "test122@M365x99297270.OnMicrosoft.com" + preferredLanguage = "EN" + mobilePhone = "9984534564" + displayName = "SNEHALtest" + givenName = "test12" + mail = "test122@M365x99297270.OnMicrosoft.com" + '@odata.context' = "https://graph.microsoft.com/beta/$metadata#users/$entity" + id = "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" + jobTitle = "testqa" + officeLocation = "test" + businessPhones = @("8976546787") + surname = "KTETSs" + Parameters = $args + } + ) + + } + + Mock -CommandName Invoke-GraphRequest -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra.Beta +} + +Describe "Set-EntraBetaUserLicense" { + Context "Test for Set-EntraBetaUserLicense" { + It "Should return specific User" { + $addLicensesArray = [PSCustomObject]@{ + skuId = "66aa66aa-bb77-cc88-dd99-00ee00ee00ee" + } + $Licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses + $Licenses.AddLicenses =$addLicensesArray + $result = Set-EntraBetaUserLicense -UserId 1139c016-f606-45f0-83f7-40eb2a552a6f -AssignedLicenses $Licenses + + $result | Should -Not -BeNullOrEmpty + $result.userPrincipalName | Should -Be "test122@M365x99297270.OnMicrosoft.com" + $result.preferredLanguage | Should -Be "EN" + $result.mobilePhone | Should -Be "9984534564" + $result.displayName | Should -Be "SNEHALtest" + $result.givenName | Should -Be "test12" + $result.mail | Should -Be "test122@M365x99297270.OnMicrosoft.com" + $result.'@odata.context' | Should -Be "https://graph.microsoft.com/beta/$metadata#users/$entity" + $result.id | Should -Be "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" + $result.jobTitle | Should -Be "testqa" + $result.officeLocation | Should -Be "test" + $result.businessPhones | Should -Be @("8976546787") + $result.surname | Should -Be "KTETSs" + + Should -Invoke -CommandName Invoke-GraphRequest -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + + It "Should fail when UserId is empty string value" { + { Set-EntraBetaUserLicense -UserId "" } | Should -Throw "Cannot bind argument to parameter 'UserId' because it is an empty string." + } + + It "Should fail when UserId is empty" { + { Set-EntraBetaUserLicense -UserId } | Should -Throw "Missing an argument for parameter 'UserId'. Specify a parameter of type 'System.String' and try again." + } + + It "Should fail when AssignedLicenses is empty" { + { Set-EntraBetaUserLicense -UserId '00aa00aa-bb11-cc22-dd33-44ee44ee44ee' -AssignedLicenses } | Should -Throw "Missing an argument for parameter 'AssignedLicenses'. Specify a parameter of type 'Microsoft.Open.AzureAD.Model.AssignedLicenses' and try again." + } + + It "Should contain UserId in parameters when passed UserId to it" { + $addLicensesArray = [PSCustomObject]@{ + skuId = "66aa66aa-bb77-cc88-dd99-00ee00ee00ee" + } + $Licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses + $Licenses.AddLicenses =$addLicensesArray + $result = Set-EntraBetaUserLicense -UserId '00aa00aa-bb11-cc22-dd33-44ee44ee44ee' -AssignedLicenses $Licenses + + $params = Get-Parameters -data $result.Parameters + $params.Uri | Should -Match "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" + } + + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Set-EntraBetaUserLicense" + $addLicensesArray = [PSCustomObject]@{ + skuId = "66aa66aa-bb77-cc88-dd99-00ee00ee00ee" + } + $Licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses + $Licenses.AddLicenses =$addLicensesArray + + Set-EntraBetaUserLicense -UserId '00aa00aa-bb11-cc22-dd33-44ee44ee44ee' -AssignedLicenses $Licenses + + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Set-EntraBetaUserLicense" + + Should -Invoke -CommandName Invoke-GraphRequest -ModuleName Microsoft.Graph.Entra.Beta -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' + $addLicensesArray = [PSCustomObject]@{ + skuId = "66aa66aa-bb77-cc88-dd99-00ee00ee00ee" + } + $Licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses + $Licenses.AddLicenses =$addLicensesArray + + try { + # Act & Assert: Ensure the function doesn't throw an exception + { Set-EntraBetaUserLicense -UserId '00aa00aa-bb11-cc22-dd33-44ee44ee44ee' -AssignedLicenses $Licenses -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } + + } +} diff --git a/test/module/EntraBeta/Set-EntraBetaUserPassword.Tests.ps1 b/test/module/EntraBeta/Set-EntraBetaUserPassword.Tests.ps1 new file mode 100644 index 0000000000..4d59daef35 --- /dev/null +++ b/test/module/EntraBeta/Set-EntraBetaUserPassword.Tests.ps1 @@ -0,0 +1,122 @@ +# ------------------------------------------------------------------------------ +# 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.Graph.Entra.Beta) -eq $null){ + Import-Module Microsoft.Graph.Entra.Beta + } + Import-Module (Join-Path $psscriptroot "..\Common-Functions.ps1") -Force + + Mock -CommandName Update-MgUser -MockWith {} -ModuleName Microsoft.Graph.Entra.Beta +} + +Describe "Set-EntraBetaUserPassword" { + Context "Test for Set-EntraBetaUserPassword" { + It "Should return empty object" { + $userUPN="mock106@M365x99297270.OnMicrosoft.com" + $newPassword="New@12345" + $secPassword = ConvertTo-SecureString $newPassword -AsPlainText -Force + $result = Set-EntraBetaUserPassword -UserId $userUPN -Password $secPassword -ForceChangePasswordNextLogin $true -EnforceChangePasswordPolicy $true + $result | Should -BeNullOrEmpty + + Should -Invoke -CommandName Update-MgUser -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + It "Should fail when UserId is empty" { + $userUPN="mock106@M365x99297270.OnMicrosoft.com" + $newPassword="New@12345" + $secPassword = ConvertTo-SecureString $newPassword -AsPlainText -Force + { Set-EntraBetaUserPassword -UserId -Password $secPassword } | Should -Throw "Missing an argument for parameter 'UserId'*" + } + It "Should fail when UserId is invalid" { + $userUPN="mock106@M365x99297270.OnMicrosoft.com" + $newPassword="New@12345" + $secPassword = ConvertTo-SecureString $newPassword -AsPlainText -Force + { Set-EntraBetaUserPassword -UserId "" -Password $secPassword } | Should -Throw "Cannot bind argument to parameter 'UserId' because it is an empty string*" + } + It "Should fail when Password is empty" { + $userUPN="mock106@M365x99297270.OnMicrosoft.com" + $newPassword="New@12345" + $secPassword = ConvertTo-SecureString $newPassword -AsPlainText -Force + { Set-EntraBetaUserPassword -UserId $userUPN -Password } | Should -Throw "Missing an argument for parameter 'Password'*" + } + It "Should fail when Password is invalid" { + $userUPN="mock106@M365x99297270.OnMicrosoft.com" + $newPassword="New@12345" + $secPassword = ConvertTo-SecureString $newPassword -AsPlainText -Force + { Set-EntraUserBetaPassword -UserId $userUPN -Password "" } | Should -Throw "Cannot process argument transformation on parameter 'Password'*" + } + It "Should fail when ForceChangePasswordNextLogin is empty" { + $userUPN="mock106@M365x99297270.OnMicrosoft.com" + $newPassword="New@12345" + $secPassword = ConvertTo-SecureString $newPassword -AsPlainText -Force + { Set-EntraBetaUserPassword -UserId $userUPN -Password $secPassword -ForceChangePasswordNextLogin } | Should -Throw "Missing an argument for parameter 'ForceChangePasswordNextLogin'*" + } + It "Should fail when ForceChangePasswordNextLogin is invalid" { + $userUPN="mock106@M365x99297270.OnMicrosoft.com" + $newPassword="New@12345" + $secPassword = ConvertTo-SecureString $newPassword -AsPlainText -Force + { Set-EntraBetaUserPassword -UserId $userUPN -Password $secPassword -ForceChangePasswordNextLogin xyz } | Should -Throw "Cannot process argument transformation on parameter 'ForceChangePasswordNextLogin'*" + } + It "Should fail when EnforceChangePasswordPolicy is empty" { + $userUPN="mock106@M365x99297270.OnMicrosoft.com" + $newPassword="New@12345" + $secPassword = ConvertTo-SecureString $newPassword -AsPlainText -Force + { Set-EntraBetaUserPassword -UserId $userUPN -Password $secPassword -EnforceChangePasswordPolicy } | Should -Throw "Missing an argument for parameter 'EnforceChangePasswordPolicy'*" + } + It "Should fail when EnforceChangePasswordPolicy is invalid" { + $userUPN="mock106@M365x99297270.OnMicrosoft.com" + $newPassword="New@12345" + $secPassword = ConvertTo-SecureString $newPassword -AsPlainText -Force + { Set-EntraBetaUserPassword -UserId $userUPN -Password $secPassword -EnforceChangePasswordPolicy xyz } | Should -Throw "Cannot process argument transformation on parameter 'EnforceChangePasswordPolicy'*" + } + It "Should contain ForceChangePasswordNextSignIn in parameters when passed ForceChangePasswordNextLogin to it" { + Mock -CommandName Update-MgUser -MockWith {$args} -ModuleName Microsoft.Graph.Entra.Beta + + $userUPN="mock106@M365x99297270.OnMicrosoft.com" + $newPassword="New@12345" + $secPassword = ConvertTo-SecureString $newPassword -AsPlainText -Force + $result = Set-EntraBetaUserPassword -UserId $userUPN -Password $secPassword -ForceChangePasswordNextLogin $true -EnforceChangePasswordPolicy $true + $params = Get-Parameters -data $result + $params.PasswordProfile.ForceChangePasswordNextSignIn | Should -Be $true + } + It "Should contain ForceChangePasswordNextSignInWithMfa in parameters when passed EnforceChangePasswordPolicy to it" { + Mock -CommandName Update-MgUser -MockWith {$args} -ModuleName Microsoft.Graph.Entra.Beta + + $userUPN="mock106@M365x99297270.OnMicrosoft.com" + $newPassword="New@12345" + $secPassword = ConvertTo-SecureString $newPassword -AsPlainText -Force + $result = Set-EntraBetaUserPassword -UserId $userUPN -Password $secPassword -ForceChangePasswordNextLogin $true -EnforceChangePasswordPolicy $true + $params = Get-Parameters -data $result + $params.PasswordProfile.ForceChangePasswordNextSignInWithMfa | Should -Be $true + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Set-EntraBetaUserPassword" + $userUPN="mock106@M365x99297270.OnMicrosoft.com" + $newPassword="New@12345" + $secPassword = ConvertTo-SecureString $newPassword -AsPlainText -Force + $result = Set-EntraBetaUserPassword -UserId $userUPN -Password $secPassword -ForceChangePasswordNextLogin $true -EnforceChangePasswordPolicy $true + $result | Should -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Set-EntraUserPassword" + Should -Invoke -CommandName Update-MgUser -ModuleName Microsoft.Graph.Entra.Beta -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' + $userUPN="mock106@M365x99297270.OnMicrosoft.com" + $newPassword="New@12345" + $secPassword = ConvertTo-SecureString $newPassword -AsPlainText -Force + + try { + # Act & Assert: Ensure the function doesn't throw an exception + { Set-EntraBetaUserPassword -UserId $userUPN -Password $secPassword -ForceChangePasswordNextLogin $true -EnforceChangePasswordPolicy $true -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } + } +} \ No newline at end of file From 714e264b93c20a805cdb248107327aabb7fed21a Mon Sep 17 00:00:00 2001 From: Emmanuel Ng'ang'a <60355631+emmanuel-karanja@users.noreply.github.com> Date: Thu, 31 Oct 2024 14:52:58 +0300 Subject: [PATCH 04/10] release --- test/module/EntraBeta/Set-EntraBetaUserPassword.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/module/EntraBeta/Set-EntraBetaUserPassword.Tests.ps1 b/test/module/EntraBeta/Set-EntraBetaUserPassword.Tests.ps1 index 4d59daef35..d31049e443 100644 --- a/test/module/EntraBeta/Set-EntraBetaUserPassword.Tests.ps1 +++ b/test/module/EntraBeta/Set-EntraBetaUserPassword.Tests.ps1 @@ -43,7 +43,7 @@ Describe "Set-EntraBetaUserPassword" { $userUPN="mock106@M365x99297270.OnMicrosoft.com" $newPassword="New@12345" $secPassword = ConvertTo-SecureString $newPassword -AsPlainText -Force - { Set-EntraUserBetaPassword -UserId $userUPN -Password "" } | Should -Throw "Cannot process argument transformation on parameter 'Password'*" + { Set-EntraBetaUserPassword -UserId $userUPN -Password "" } | Should -Throw "Cannot process argument transformation on parameter 'Password'*" } It "Should fail when ForceChangePasswordNextLogin is empty" { $userUPN="mock106@M365x99297270.OnMicrosoft.com" From bfd91020621b9dcf3b636fb91f5221d213a045d3 Mon Sep 17 00:00:00 2001 From: Emmanuel Ng'ang'a <60355631+emmanuel-karanja@users.noreply.github.com> Date: Thu, 31 Oct 2024 14:56:44 +0300 Subject: [PATCH 05/10] update tests del UPN --- test/module/EntraBeta/Set-EntraBetaUserPassword.Tests.ps1 | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/module/EntraBeta/Set-EntraBetaUserPassword.Tests.ps1 b/test/module/EntraBeta/Set-EntraBetaUserPassword.Tests.ps1 index d31049e443..e67538465d 100644 --- a/test/module/EntraBeta/Set-EntraBetaUserPassword.Tests.ps1 +++ b/test/module/EntraBeta/Set-EntraBetaUserPassword.Tests.ps1 @@ -22,13 +22,11 @@ Describe "Set-EntraBetaUserPassword" { Should -Invoke -CommandName Update-MgUser -ModuleName Microsoft.Graph.Entra.Beta -Times 1 } It "Should fail when UserId is empty" { - $userUPN="mock106@M365x99297270.OnMicrosoft.com" $newPassword="New@12345" $secPassword = ConvertTo-SecureString $newPassword -AsPlainText -Force { Set-EntraBetaUserPassword -UserId -Password $secPassword } | Should -Throw "Missing an argument for parameter 'UserId'*" } It "Should fail when UserId is invalid" { - $userUPN="mock106@M365x99297270.OnMicrosoft.com" $newPassword="New@12345" $secPassword = ConvertTo-SecureString $newPassword -AsPlainText -Force { Set-EntraBetaUserPassword -UserId "" -Password $secPassword } | Should -Throw "Cannot bind argument to parameter 'UserId' because it is an empty string*" From 0d7f49e4afcd66b5bb7812ecf194ca70e1a58515 Mon Sep 17 00:00:00 2001 From: Emmanuel Ng'ang'a <60355631+emmanuel-karanja@users.noreply.github.com> Date: Thu, 31 Oct 2024 15:09:31 +0300 Subject: [PATCH 06/10] update tests del UPN --- module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 | 2 ++ test/module/EntraBeta/Set-EntraBetaUserPassword.Tests.ps1 | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 b/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 index 95db4b4381..74b4ca39e9 100644 --- a/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 +++ b/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 @@ -17,6 +17,8 @@ [System.Security.SecureString] $Password, [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [System.Boolean] $EnforceChangePasswordPolicy + [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] + [System.Boolean] $ForceChangePasswordNextSignInWithMfa ) PROCESS { $params = @{} diff --git a/test/module/EntraBeta/Set-EntraBetaUserPassword.Tests.ps1 b/test/module/EntraBeta/Set-EntraBetaUserPassword.Tests.ps1 index e67538465d..50a38da075 100644 --- a/test/module/EntraBeta/Set-EntraBetaUserPassword.Tests.ps1 +++ b/test/module/EntraBeta/Set-EntraBetaUserPassword.Tests.ps1 @@ -94,7 +94,7 @@ Describe "Set-EntraBetaUserPassword" { $secPassword = ConvertTo-SecureString $newPassword -AsPlainText -Force $result = Set-EntraBetaUserPassword -UserId $userUPN -Password $secPassword -ForceChangePasswordNextLogin $true -EnforceChangePasswordPolicy $true $result | Should -BeNullOrEmpty - $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Set-EntraUserPassword" + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Set-EntraBetaUserPassword" Should -Invoke -CommandName Update-MgUser -ModuleName Microsoft.Graph.Entra.Beta -Times 1 -ParameterFilter { $Headers.'User-Agent' | Should -Be $userAgentHeaderValue $true From 30fc1fe776030b51a20498c5d1b0900d9298db25 Mon Sep 17 00:00:00 2001 From: Emmanuel Ng'ang'a <60355631+emmanuel-karanja@users.noreply.github.com> Date: Thu, 31 Oct 2024 15:20:33 +0300 Subject: [PATCH 07/10] update tests del UPN --- module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 b/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 index 74b4ca39e9..0f34d8b5f7 100644 --- a/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 +++ b/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 @@ -17,8 +17,6 @@ [System.Security.SecureString] $Password, [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [System.Boolean] $EnforceChangePasswordPolicy - [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] - [System.Boolean] $ForceChangePasswordNextSignInWithMfa ) PROCESS { $params = @{} @@ -88,7 +86,7 @@ $PasswordProfile = @{} if($null -ne $PSBoundParameters["ForceChangePasswordNextLogin"]) { $PasswordProfile["ForceChangePasswordNextSignIn"] = $ForceChangePasswordNextSignIn } - if($null -ne $PSBoundParameters["EnforceChangePasswordPolicy"]) { $PasswordProfile["ForceChangePasswordNextSignInWithMfa"] = $ForceChangePasswordNextSignInWithMfa } + if($null -ne $PSBoundParameters["EnforceChangePasswordPolicy"]) { $PasswordProfile["ForceChangePasswordNextSignInWithMfa"] = $EnforceChangePasswordPolicy } if($null -ne $PSBoundParameters["Password"]) { $PasswordProfile["password"] = $PlainPassword } Write-Debug("============================ TRANSFORMATIONS ============================") From 95e334fb5ff57e041ddbe8cd2ef5995f61540133 Mon Sep 17 00:00:00 2001 From: Emmanuel Ng'ang'a <60355631+emmanuel-karanja@users.noreply.github.com> Date: Thu, 31 Oct 2024 15:25:01 +0300 Subject: [PATCH 08/10] update tests del UPN --- module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 b/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 index 0f34d8b5f7..67bbf53fbe 100644 --- a/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 +++ b/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 @@ -86,7 +86,7 @@ $PasswordProfile = @{} if($null -ne $PSBoundParameters["ForceChangePasswordNextLogin"]) { $PasswordProfile["ForceChangePasswordNextSignIn"] = $ForceChangePasswordNextSignIn } - if($null -ne $PSBoundParameters["EnforceChangePasswordPolicy"]) { $PasswordProfile["ForceChangePasswordNextSignInWithMfa"] = $EnforceChangePasswordPolicy } + if($null -ne $PSBoundParameters["EnforceChangePasswordPolicy"]) { $PasswordProfile["ForceChangePasswordNextSignInWithMfa"] = $true } if($null -ne $PSBoundParameters["Password"]) { $PasswordProfile["password"] = $PlainPassword } Write-Debug("============================ TRANSFORMATIONS ============================") From f6b268c76516982231561858a71ba064ec067765 Mon Sep 17 00:00:00 2001 From: Emmanuel Ng'ang'a <60355631+emmanuel-karanja@users.noreply.github.com> Date: Thu, 31 Oct 2024 15:29:30 +0300 Subject: [PATCH 09/10] changes to tests --- module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 b/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 index 67bbf53fbe..0acbf7b98f 100644 --- a/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 +++ b/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 @@ -86,7 +86,7 @@ $PasswordProfile = @{} if($null -ne $PSBoundParameters["ForceChangePasswordNextLogin"]) { $PasswordProfile["ForceChangePasswordNextSignIn"] = $ForceChangePasswordNextSignIn } - if($null -ne $PSBoundParameters["EnforceChangePasswordPolicy"]) { $PasswordProfile["ForceChangePasswordNextSignInWithMfa"] = $true } + if($null -ne $PSBoundParameters["EnforceChangePasswordPolicy"]) { $PasswordProfile["ForceChangePasswordNextSignInWithMfa"] = $EnforceChangePasswordPolicy" } if($null -ne $PSBoundParameters["Password"]) { $PasswordProfile["password"] = $PlainPassword } Write-Debug("============================ TRANSFORMATIONS ============================") From 08f067373cb64e38ad89ca7bf4098036472853a1 Mon Sep 17 00:00:00 2001 From: Emmanuel Ng'ang'a <60355631+emmanuel-karanja@users.noreply.github.com> Date: Thu, 31 Oct 2024 15:52:47 +0300 Subject: [PATCH 10/10] Update Set-EntraBetaUserPassword.ps1 --- module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 b/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 index 0acbf7b98f..55f0b93a7b 100644 --- a/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 +++ b/module/EntraBeta/customizations/Set-EntraBetaUserPassword.ps1 @@ -86,7 +86,7 @@ $PasswordProfile = @{} if($null -ne $PSBoundParameters["ForceChangePasswordNextLogin"]) { $PasswordProfile["ForceChangePasswordNextSignIn"] = $ForceChangePasswordNextSignIn } - if($null -ne $PSBoundParameters["EnforceChangePasswordPolicy"]) { $PasswordProfile["ForceChangePasswordNextSignInWithMfa"] = $EnforceChangePasswordPolicy" } + if($null -ne $PSBoundParameters["EnforceChangePasswordPolicy"]) { $PasswordProfile["ForceChangePasswordNextSignInWithMfa"] = $EnforceChangePasswordPolicy } if($null -ne $PSBoundParameters["Password"]) { $PasswordProfile["password"] = $PlainPassword } Write-Debug("============================ TRANSFORMATIONS ============================") @@ -97,4 +97,4 @@ $response } '@ -} \ No newline at end of file +}