diff --git a/module/Entra/AdditionalFunctions/Get-EntraUserInactiveSignIn.ps1 b/module/Entra/AdditionalFunctions/Get-EntraUserInactiveSignIn.ps1 new file mode 100644 index 000000000..96664d6f8 --- /dev/null +++ b/module/Entra/AdditionalFunctions/Get-EntraUserInactiveSignIn.ps1 @@ -0,0 +1,78 @@ +# ------------------------------------------------------------------------------ +# Copyright (c) Microsoft Corporation. All Rights Reserved. +# Licensed under the MIT License. See License in the project root for license information. +# ------------------------------------------------------------------------------ + +function Get-EntraUserInactiveSignIn { + [CmdletBinding(DefaultParameterSetName = 'GetQuery')] + [OutputType([string])] + param ( + # User Last Sign In Activity is before Days ago + [Parameter( + Mandatory = $true, + ValueFromPipeline = $true, + HelpMessage = "Number of days to check for Last Sign In Activity" + )] + [Alias("LastSignInBeforeDaysAgo")] + [int] + $Ago, + + # Return results for All, Member, or Guest userTypes + [Parameter( + HelpMessage = "Specifies the type of user to filter. Choose 'All' for all users, 'Member' for internal users, or 'Guest' for external users." + )] + [ValidateSet("All", "Member", "Guest")] + [System.String] + $UserType = "All" + ) + + process { + $params = @{} + $customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand + + foreach ($param in @("Debug", "WarningVariable", "InformationVariable", "InformationAction", "OutVariable", "OutBuffer", "ErrorVariable", "PipelineVariable", "ErrorAction", "WarningAction")) { + if ($PSBoundParameters.ContainsKey($param)) { + $params[$param] = $PSBoundParameters[$param] + } + } + + $queryDate = Get-Date (Get-Date).AddDays($(0 - $Ago)) -UFormat %Y-%m-%dT00:00:00Z + $queryFilter = ("(signInActivity/lastSignInDateTime le {0})" -f $queryDate) + + Write-Debug ("Retrieving Users with Filter {0}" -f $queryFilter) + + Write-Debug("============================ TRANSFORMATIONS ============================") + $params.Keys | ForEach-Object { "$_ : $($params[$_])" } | Write-Debug + Write-Debug("=========================================================================`n") + + $queryUsers = Get-MgUser -Filter $queryFilter -PageSize 999 -All:$true -Property signInActivity, UserPrincipalName, Id, DisplayName, mail, userType, createdDateTime, accountEnabled -Headers $customHeaders + + $users = if ($UserType -eq "All") { + $queryUsers + } + else { + $queryUsers | Where-Object { $_.userType -eq $UserType } + } + + foreach ($userObject in $users) { + $checkedUser = [ordered] @{ + UserID = $userObject.Id + DisplayName = $userObject.DisplayName + UserPrincipalName = $userObject.UserPrincipalName + Mail = $userObject.Mail + UserType = $userObject.UserType + AccountEnabled = $userObject.AccountEnabled + LastSignInDateTime = $userObject.signInActivity.LastSignInDateTime + LastSigninDaysAgo = if ($null -eq $userObject.signInActivity.LastSignInDateTime) { "Unknown" } else { (New-TimeSpan -Start $userObject.signInActivity.LastSignInDateTime -End (Get-Date)).Days } + lastSignInRequestId = $userObject.signInActivity.lastSignInRequestId + lastNonInteractiveSignInDateTime = if ($null -eq $userObject.signInActivity.lastNonInteractiveSignInDateTime) { "Unknown" } else { $userObject.signInActivity.lastNonInteractiveSignInDateTime } + LastNonInteractiveSigninDaysAgo = if ($null -eq $userObject.signInActivity.lastNonInteractiveSignInDateTime) { "Unknown" } else { (New-TimeSpan -Start $userObject.signInActivity.lastNonInteractiveSignInDateTime -End (Get-Date)).Days } + lastNonInteractiveSignInRequestId = $userObject.signInActivity.lastNonInteractiveSignInRequestId + CreatedDateTime = if ($null -eq $userObject.CreatedDateTime) { "Unknown" } else { $userObject.CreatedDateTime } + CreatedDaysAgo = if ($null -eq $userObject.CreatedDateTime) { "Unknown" } else { (New-TimeSpan -Start $userObject.CreatedDateTime -End (Get-Date)).Days } + } + + Write-Output ([pscustomobject]$checkedUser) + } + } +} diff --git a/module/EntraBeta/AdditionalFunctions/Get-EntraBetaUserInactiveSignIn.ps1 b/module/EntraBeta/AdditionalFunctions/Get-EntraBetaUserInactiveSignIn.ps1 new file mode 100644 index 000000000..feceed699 --- /dev/null +++ b/module/EntraBeta/AdditionalFunctions/Get-EntraBetaUserInactiveSignIn.ps1 @@ -0,0 +1,78 @@ +# ------------------------------------------------------------------------------ +# Copyright (c) Microsoft Corporation. All Rights Reserved. +# Licensed under the MIT License. See License in the project root for license information. +# ------------------------------------------------------------------------------ + +function Get-EntraBetaUserInactiveSignIn { + [CmdletBinding(DefaultParameterSetName = 'GetQuery')] + [OutputType([string])] + param ( + # User Last Sign In Activity is before Days ago + [Parameter( + Mandatory = $true, + ValueFromPipeline = $true, + HelpMessage = "Number of days to check for Last Sign In Activity" + )] + [Alias("LastSignInBeforeDaysAgo")] + [int] + $Ago, + + # Return results for All, Member, or Guest userTypes + [Parameter( + HelpMessage = "Specifies the type of user to filter. Choose 'All' for all users, 'Member' for internal users, or 'Guest' for external users." + )] + [ValidateSet("All", "Member", "Guest")] + [System.String] + $UserType = "All" + ) + + process { + $params = @{} + $customHeaders = New-EntraBetaCustomHeaders -Command $MyInvocation.MyCommand + + foreach ($param in @("Debug", "WarningVariable", "InformationVariable", "InformationAction", "OutVariable", "OutBuffer", "ErrorVariable", "PipelineVariable", "ErrorAction", "WarningAction")) { + if ($PSBoundParameters.ContainsKey($param)) { + $params[$param] = $PSBoundParameters[$param] + } + } + + $queryDate = Get-Date (Get-Date).AddDays($(0 - $Ago)) -UFormat %Y-%m-%dT00:00:00Z + $queryFilter = ("(signInActivity/lastSignInDateTime le {0})" -f $queryDate) + + Write-Debug ("Retrieving Users with Filter {0}" -f $queryFilter) + + Write-Debug("============================ TRANSFORMATIONS ============================") + $params.Keys | ForEach-Object { "$_ : $($params[$_])" } | Write-Debug + Write-Debug("=========================================================================`n") + + $queryUsers = Get-MgBetaUser -Filter $queryFilter -PageSize 999 -All:$true -Property signInActivity, UserPrincipalName, Id, DisplayName, mail, userType, createdDateTime, accountEnabled -Headers $customHeaders + + $users = if ($UserType -eq "All") { + $queryUsers + } + else { + $queryUsers | Where-Object { $_.userType -eq $UserType } + } + + foreach ($userObject in $users) { + $checkedUser = [ordered] @{ + UserID = $userObject.Id + DisplayName = $userObject.DisplayName + UserPrincipalName = $userObject.UserPrincipalName + Mail = $userObject.Mail + UserType = $userObject.UserType + AccountEnabled = $userObject.AccountEnabled + LastSignInDateTime = $userObject.signInActivity.LastSignInDateTime + LastSigninDaysAgo = if ($null -eq $userObject.signInActivity.LastSignInDateTime) { "Unknown" } else { (New-TimeSpan -Start $userObject.signInActivity.LastSignInDateTime -End (Get-Date)).Days } + lastSignInRequestId = $userObject.signInActivity.lastSignInRequestId + lastNonInteractiveSignInDateTime = if ($null -eq $userObject.signInActivity.lastNonInteractiveSignInDateTime) { "Unknown" } else { $userObject.signInActivity.lastNonInteractiveSignInDateTime } + LastNonInteractiveSigninDaysAgo = if ($null -eq $userObject.signInActivity.lastNonInteractiveSignInDateTime) { "Unknown" } else { (New-TimeSpan -Start $userObject.signInActivity.lastNonInteractiveSignInDateTime -End (Get-Date)).Days } + lastNonInteractiveSignInRequestId = $userObject.signInActivity.lastNonInteractiveSignInRequestId + CreatedDateTime = if ($null -eq $userObject.CreatedDateTime) { "Unknown" } else { $userObject.CreatedDateTime } + CreatedDaysAgo = if ($null -eq $userObject.CreatedDateTime) { "Unknown" } else { (New-TimeSpan -Start $userObject.CreatedDateTime -End (Get-Date)).Days } + } + + Write-Output ([pscustomobject]$checkedUser) + } + } +} diff --git a/module/docs/entra-powershell-beta/Microsoft.Graph.Entra.Beta/Get-EntraBetaUserInactiveSignIn.md b/module/docs/entra-powershell-beta/Microsoft.Graph.Entra.Beta/Get-EntraBetaUserInactiveSignIn.md new file mode 100644 index 000000000..225604055 --- /dev/null +++ b/module/docs/entra-powershell-beta/Microsoft.Graph.Entra.Beta/Get-EntraBetaUserInactiveSignIn.md @@ -0,0 +1,144 @@ +--- +title: Get-EntraBetaUserInactiveSignIn +description: This article provides details on the Get-EntraBetaUserInactiveSignIn command. + + +ms.topic: reference +ms.date: 11/08/2024 +ms.author: eunicewaweru +ms.reviewer: stevemutungi +manager: CelesteDG +author: msewaweru +external help file: Microsoft.Graph.Entra.Beta-Help.xml +Module Name: Microsoft.Graph.Entra.Beta +online version: https://learn.microsoft.com/powershell/module/Microsoft.Graph.Entra.Beta/Get-EntraBetaUserInactiveSignIn + +schema: 2.0.0 +--- + +# Get-EntraBetaUserInactiveSignIn + +## Synopsis + +Retrieve users without interactive sign-ins in the last N days. + +## Syntax + +```powershell +Get-EntraBetaUserInactiveSignIn + -Ago + [-UserType ] + [] +``` + +## Description + +This cmdlet retrieves users without interactive sign-ins in the last N days. + +## Examples + +### Example 1: Retrieve users without interactive sign-ins in the last 10 days + +```powershell +Connect-Entra -Scopes 'User.Read.All','AuditLog.Read.All' +Get-EntraBetaUserInactiveSignIn -Ago 10 +``` + +```Output +UserID : cccccccc-2222-3333-4444-dddddddddddd +DisplayName : Allan Deyoung +UserPrincipalName : AllanD@Contoso.com +Mail : AllanD@Contoso.com +UserType : Member +AccountEnabled : True +LastSignInDateTime : 10/7/2024 12:15:17 PM +LastSigninDaysAgo : 30 +lastSignInRequestId : eeeeeeee-4444-5555-6666-ffffffffffff +lastNonInteractiveSignInDateTime : 10/7/2024 12:13:13 PM +LastNonInteractiveSigninDaysAgo : 30 +lastNonInteractiveSignInRequestId : dddddddd-3333-4444-5555-eeeeeeeeeeee +CreatedDateTime : 10/7/2024 12:32:30 AM +CreatedDaysAgo : 31 +``` + +This example shows how to find users who haven’t signed in within the past 30 days. + +### Example 2: Retrieve guest users without interactive sign-ins in the last 10 days + +```powershell +Connect-Entra -Scopes 'User.Read.All','AuditLog.Read.All' +Get-EntraBetaUserInactiveSignIn -Ago 10 -UserType 'Guest' +``` + +```Output +UserID : cccccccc-2222-3333-4444-dddddddddddd +DisplayName : Allan Deyoung +UserPrincipalName : AllanD@Contoso.com +Mail : AllanD@Contoso.com +UserType : Guest +AccountEnabled : True +LastSignInDateTime : 10/7/2024 12:15:17 PM +LastSigninDaysAgo : 30 +lastSignInRequestId : eeeeeeee-4444-5555-6666-ffffffffffff +lastNonInteractiveSignInDateTime : 10/7/2024 12:13:13 PM +LastNonInteractiveSigninDaysAgo : 30 +lastNonInteractiveSignInRequestId : dddddddd-3333-4444-5555-eeeeeeeeeeee +CreatedDateTime : 10/7/2024 12:32:30 AM +CreatedDaysAgo : 31 +``` + +This example shows how to find guest users who haven’t signed in within the past 30 days. Choose `All` for all users, `Member` for internal users, or `Guest` for external users. + +## Parameters + +### -Ago + +Number of days to check for Last Sign In Activity. + +```yaml +Type: System.Int32 +Parameter Sets: (All) +Aliases: LastSignInBeforeDaysAgo + +Required: True +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -UserType + +Specifies the type of user to filter. Choose `All` for all users, `Member` for internal users, or `Guest` for external users. + +```yaml +Type: System.String +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: All +Accept pipeline input: False +Accept wildcard characters: False +``` + +### CommonParameters + +This cmdlet supports the common parameters: `-Debug`, `-ErrorAction`, `-ErrorVariable`, `-InformationAction`, `-InformationVariable`, `-OutVariable`, `-OutBuffer`, `-PipelineVariable`, `-Verbose`, `-WarningAction`, and `-WarningVariable`. For more information, see [about_CommonParameters](https://go.microsoft.com/fwlink/?LinkID=113216). + +## Inputs + +### System.String + +## Outputs + +### System.Object + +## Notes + +Updating Role Assignable Groups or Privileged Access Groups require `PrivilegedAccess.ReadWrite.AzureADGroup` permission scope. + +## Related Links + +[Get-EntraBetaUser](Get-EntraBetaUser.md) diff --git a/module/docs/entra-powershell-v1.0/Microsoft.Graph.Entra/Get-EntraUserInactiveSignIn.md b/module/docs/entra-powershell-v1.0/Microsoft.Graph.Entra/Get-EntraUserInactiveSignIn.md new file mode 100644 index 000000000..656dadabc --- /dev/null +++ b/module/docs/entra-powershell-v1.0/Microsoft.Graph.Entra/Get-EntraUserInactiveSignIn.md @@ -0,0 +1,144 @@ +--- +title: Get-EntraUserInactiveSignIn +description: This article provides details on the Get-EntraUserInactiveSignIn command. + + +ms.topic: reference +ms.date: 11/08/2024 +ms.author: eunicewaweru +ms.reviewer: stevemutungi +manager: CelesteDG +author: msewaweru +external help file: Microsoft.Graph.Entra-Help.xml +Module Name: Microsoft.Graph.Entra +online version: https://learn.microsoft.com/powershell/module/Microsoft.Graph.Entra/Get-EntraUserInactiveSignIn + +schema: 2.0.0 +--- + +# Get-EntraUserInactiveSignIn + +## Synopsis + +Retrieve users without interactive sign-ins in the last N days. + +## Syntax + +```powershell +Get-EntraUserInactiveSignIn + -Ago + [-UserType ] + [] +``` + +## Description + +This cmdlet retrieves users without interactive sign-ins in the last N days. + +## Examples + +### Example 1: Retrieve users without interactive sign-ins in the last 10 days + +```powershell +Connect-Entra -Scopes 'User.Read.All','AuditLog.Read.All' +Get-EntraUserInactiveSignIn -Ago 10 +``` + +```Output +UserID : cccccccc-2222-3333-4444-dddddddddddd +DisplayName : Allan Deyoung +UserPrincipalName : AllanD@Contoso.com +Mail : AllanD@Contoso.com +UserType : Member +AccountEnabled : True +LastSignInDateTime : 10/7/2024 12:15:17 PM +LastSigninDaysAgo : 30 +lastSignInRequestId : eeeeeeee-4444-5555-6666-ffffffffffff +lastNonInteractiveSignInDateTime : 10/7/2024 12:13:13 PM +LastNonInteractiveSigninDaysAgo : 30 +lastNonInteractiveSignInRequestId : dddddddd-3333-4444-5555-eeeeeeeeeeee +CreatedDateTime : 10/7/2024 12:32:30 AM +CreatedDaysAgo : 31 +``` + +This example shows how to find users who haven’t signed in within the past 30 days. + +### Example 2: Retrieve guest users without interactive sign-ins in the last 10 days + +```powershell +Connect-Entra -Scopes 'User.Read.All','AuditLog.Read.All' +Get-EntraUserInactiveSignIn -Ago 10 -UserType 'Guest' +``` + +```Output +UserID : cccccccc-2222-3333-4444-dddddddddddd +DisplayName : Allan Deyoung +UserPrincipalName : AllanD@Contoso.com +Mail : AllanD@Contoso.com +UserType : Guest +AccountEnabled : True +LastSignInDateTime : 10/7/2024 12:15:17 PM +LastSigninDaysAgo : 30 +lastSignInRequestId : eeeeeeee-4444-5555-6666-ffffffffffff +lastNonInteractiveSignInDateTime : 10/7/2024 12:13:13 PM +LastNonInteractiveSigninDaysAgo : 30 +lastNonInteractiveSignInRequestId : dddddddd-3333-4444-5555-eeeeeeeeeeee +CreatedDateTime : 10/7/2024 12:32:30 AM +CreatedDaysAgo : 31 +``` + +This example shows how to find guest users who haven’t signed in within the past 30 days. Choose `All` for all users, `Member` for internal users, or `Guest` for external users. + +## Parameters + +### -Ago + +Number of days to check for Last Sign In Activity. + +```yaml +Type: System.Int32 +Parameter Sets: (All) +Aliases: LastSignInBeforeDaysAgo + +Required: True +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -UserType + +Specifies the type of user to filter. Choose `All` for all users, `Member` for internal users, or `Guest` for external users. + +```yaml +Type: System.String +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: All +Accept pipeline input: False +Accept wildcard characters: False +``` + +### CommonParameters + +This cmdlet supports the common parameters: `-Debug`, `-ErrorAction`, `-ErrorVariable`, `-InformationAction`, `-InformationVariable`, `-OutVariable`, `-OutBuffer`, `-PipelineVariable`, `-Verbose`, `-WarningAction`, and `-WarningVariable`. For more information, see [about_CommonParameters](https://go.microsoft.com/fwlink/?LinkID=113216). + +## Inputs + +### System.String + +## Outputs + +### System.Object + +## Notes + +Updating Role Assignable Groups or Privileged Access Groups require `PrivilegedAccess.ReadWrite.AzureADGroup` permission scope. + +## Related Links + +[Get-EntraUser](Get-EntraUser.md) diff --git a/test/module/Entra/Get-EntraUserInactiveSignIn.Tests.ps1 b/test/module/Entra/Get-EntraUserInactiveSignIn.Tests.ps1 new file mode 100644 index 000000000..19648a71d --- /dev/null +++ b/test/module/Entra/Get-EntraUserInactiveSignIn.Tests.ps1 @@ -0,0 +1,89 @@ +# ------------------------------------------------------------------------------ +# 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) -eq $null) { + Import-Module Microsoft.Graph.Entra + } + Import-Module (Join-Path $psscriptroot "..\Common-Functions.ps1") -Force + + Mock -CommandName Get-MgUser -MockWith { + param ($Property) + if ($Property -contains 'signInActivity') { + # Return a mock object including signInActivity property + [pscustomobject]@{ + Id = '00001111-aaaa-2222-bbbb-3333cccc4444' + DisplayName = 'Allan Deyoung' + UserPrincipalName = 'AllanD@Contoso.com' + Mail = 'AllanD@Contoso.com' + UserType = 'Member' + AccountEnabled = 'True' + createdDateTime = '2024-10-07T12:15:17Z' + signInActivity = @{ + LastSignInDateTime = '10/7/2024 12:15:17 PM' + LastNonInteractiveSignInDateTime = '10/7/2024 12:13:13 PM' + LastSignInRequestId = 'aaaabbbb-0000-cccc-1111-dddd2222eeee' + LastNonInteractiveSignInRequestId = '' + } + } + } + else { + # Return a generic mock object + [pscustomobject]@{ + Id = '00001111-aaaa-2222-bbbb-3333cccc4444' + DisplayName = 'Allan Deyoung' + UserPrincipalName = 'AllanD@Contoso.com' + Mail = 'AllanD@Contoso.com' + UserType = 'Member' + AccountEnabled = 'True' + createdDateTime = '2024-10-07T12:15:17Z' + } + } + } -ModuleName Microsoft.Graph.Entra +} + +Describe "Get-EntraUserInactiveSignIn" { + Context "Test for Get-EntraUserInactiveSignIn" { + It "Returns all the Inactive users in a tenant in the last N days." { + $result = Get-EntraUserInactiveSignIn -Ago 30 -UserType "All" + $result | Should -Not -BeNullOrEmpty + + Should -Invoke -CommandName Get-MgUser -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when Ago is empty" { + { Get-EntraUserInactiveSignIn -Ago } | Should -Throw "Missing an argument for parameter 'Ago'*" + } + + It "Should fail when Ago is invalid" { + { Get-EntraUserInactiveSignIn -Ago HH } | Should -Throw "Cannot process argument transformation on parameter 'Ago'*" + } + + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraUserInactiveSignIn" + + $result = Get-EntraUserInactiveSignIn -Ago 30 -UserType "All" + $result | Should -Not -BeNullOrEmpty + + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraUserInactiveSignIn" + + Should -Invoke -CommandName Get-MgUser -ModuleName Microsoft.Graph.Entra -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 + { Get-EntraUserInactiveSignIn -Ago 30 -Debug } | Should -Not -Throw + } + finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } + } +} \ No newline at end of file diff --git a/test/module/EntraBeta/Get-EntraBetaUserInactiveSignIn.Tests.ps1 b/test/module/EntraBeta/Get-EntraBetaUserInactiveSignIn.Tests.ps1 new file mode 100644 index 000000000..b773c819c --- /dev/null +++ b/test/module/EntraBeta/Get-EntraBetaUserInactiveSignIn.Tests.ps1 @@ -0,0 +1,90 @@ +# ------------------------------------------------------------------------------ +# 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 Get-MgBetaUser -MockWith { + param ($Property) + if ($Property -contains 'signInActivity') { + # Return a mock object including signInActivity property + [pscustomobject]@{ + Id = '00001111-aaaa-2222-bbbb-3333cccc4444' + DisplayName = 'Allan Deyoung' + UserPrincipalName = 'AllanD@Contoso.com' + Mail = 'AllanD@Contoso.com' + UserType = 'Member' + AccountEnabled = 'True' + createdDateTime = '2024-10-07T12:15:17Z' + signInActivity = @{ + LastSignInDateTime = '10/7/2024 12:15:17 PM' + LastNonInteractiveSignInDateTime = '10/7/2024 12:13:13 PM' + LastSignInRequestId = 'aaaabbbb-0000-cccc-1111-dddd2222eeee' + LastNonInteractiveSignInRequestId = '' + } + } + } + else { + # Return a generic mock object + [pscustomobject]@{ + Id = '00001111-aaaa-2222-bbbb-3333cccc4444' + DisplayName = 'Allan Deyoung' + UserPrincipalName = 'AllanD@Contoso.com' + Mail = 'AllanD@Contoso.com' + UserType = 'Member' + AccountEnabled = 'True' + createdDateTime = '2024-10-07T12:15:17Z' + } + } + } -ModuleName Microsoft.Graph.Entra.Beta + +} + +Describe "Get-EntraBetaUserInactiveSignIn" { + Context "Test for Get-EntraBetaUserInactiveSignIn" { + It "Returns all the Inactive users in a tenant in the last N days." { + $result = Get-EntraBetaUserInactiveSignIn -Ago 30 -UserType "All" + $result | Should -Not -BeNullOrEmpty + + Should -Invoke -CommandName Get-MgBetaUser -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + It "Should fail when Ago is empty" { + { Get-EntraBetaUserInactiveSignIn -Ago } | Should -Throw "Missing an argument for parameter 'Ago'*" + } + + It "Should fail when Ago is invalid" { + { Get-EntraBetaUserInactiveSignIn -Ago HH } | Should -Throw "Cannot process argument transformation on parameter 'Ago'*" + } + + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraBetaUserInactiveSignIn" + + $result = Get-EntraBetaUserInactiveSignIn -Ago 30 -UserType "All" + $result | Should -Not -BeNullOrEmpty + + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraBetaUserInactiveSignIn" + + Should -Invoke -CommandName Get-MgBetaUser -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' + + try { + # Act & Assert: Ensure the function doesn't throw an exception + { Get-EntraBetaUserInactiveSignIn -Ago 30 -Debug } | Should -Not -Throw + } + finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } + } +} \ No newline at end of file