diff --git a/src/functions/public/Device/Get-IntuneDeviceLogin.ps1 b/src/functions/public/Device/Get-IntuneDeviceLogin.ps1 index 605c1a5..453cede 100644 --- a/src/functions/public/Device/Get-IntuneDeviceLogin.ps1 +++ b/src/functions/public/Device/Get-IntuneDeviceLogin.ps1 @@ -1,4 +1,6 @@ -function Get-IntuneDeviceLogin { +#Requires -Modules @{ ModuleName = 'Microsoft.Graph.Authentication'; ModuleVersion = '2.28.0' } + +function Get-IntuneDeviceLogin { <# .SYNOPSIS Retrieves logged-on user info for an Intune-managed device by DeviceId, DeviceName, UserPrincipalName, or UserId. @@ -59,6 +61,7 @@ PSCustomObject with the following properties - DeviceId (string) - DeviceName (string) + - OperatingSystem (string) - UserId (string) - UserPrincipalName (string) - LastLogonDateTime (datetime) @@ -168,6 +171,7 @@ $user = Resolve-EntraUserById -UserId $entry.userId [PSCustomObject]@{ DeviceName = $device.deviceName + OperatingSystem = $device.operatingSystem UserPrincipalName = $user.userPrincipalName DeviceId = $device.id UserId = $entry.userId @@ -269,6 +273,7 @@ $user = Resolve-EntraUserById -UserId $entry.userId [PSCustomObject]@{ DeviceName = $device.deviceName + OperatingSystem = $device.operatingSystem UserPrincipalName = $user.userPrincipalName DeviceId = $device.id UserId = $entry.userId @@ -318,7 +323,7 @@ # Get all managed devices with usersLoggedOn property. # Invoke-GraphGet already handles pagination, so we query once and filter client-side. - $uri = "$baseUri`?`$select=id,deviceName,usersLoggedOn" + $uri = "$baseUri`?`$select=id,deviceName,operatingSystem,usersLoggedOn" $resp = Invoke-GraphGet -Uri $uri $allDevices = @() @@ -356,6 +361,7 @@ $user = Resolve-EntraUserById -UserId $targetUserId [PSCustomObject]@{ DeviceName = $device.deviceName + OperatingSystem = $device.operatingSystem UserPrincipalName = $user.userPrincipalName DeviceId = $device.id UserId = $targetUserId diff --git a/src/init/initializer.ps1 b/src/init/initializer.ps1 deleted file mode 100644 index 506377a..0000000 --- a/src/init/initializer.ps1 +++ /dev/null @@ -1 +0,0 @@ -#Requires -Modules @{ ModuleName = 'Microsoft.Graph.Authentication'; ModuleVersion = '2.28.0' } diff --git a/tests/public/Device/Get-IntuneDeviceLogin.Tests.ps1 b/tests/public/Device/Get-IntuneDeviceLogin.Tests.ps1 index 18736dd..0acec9b 100644 --- a/tests/public/Device/Get-IntuneDeviceLogin.Tests.ps1 +++ b/tests/public/Device/Get-IntuneDeviceLogin.Tests.ps1 @@ -21,6 +21,8 @@ Describe 'Get-IntuneDeviceLogin' { [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] $testDeviceName = 'DEVICE-001' [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] + $testDeviceType = 'Windows' + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] $testUserId = 'u1e1a1d7-2d2b-4d8c-9f0a-0d2a3d1e2f3a' [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] $testUserPrincipalName = 'user@contoso.com' @@ -31,9 +33,10 @@ Describe 'Get-IntuneDeviceLogin' { It 'Should return a PSCustomObject with logged-on user information' { # Arrange $mockDevice = [PSCustomObject]@{ - id = $testDeviceId - deviceName = $testDeviceName - usersLoggedOn = @( + id = $testDeviceId + deviceName = $testDeviceName + operatingSystem = $testDeviceType + usersLoggedOn = @( [PSCustomObject]@{ userId = $testUserId lastLogOnDateTime = $testLastLogonDateTime @@ -56,6 +59,7 @@ Describe 'Get-IntuneDeviceLogin' { $result | Should -Not -BeNullOrEmpty $result.DeviceId | Should -Be $testDeviceId $result.DeviceName | Should -Be $testDeviceName + $result.OperatingSystem | Should -Be $testDeviceType $result.UserId | Should -Be $testUserId $result.UserPrincipalName | Should -Be $testUserPrincipalName $result.LastLogonDateTime | Should -BeOfType [datetime] @@ -210,6 +214,8 @@ Describe 'Get-IntuneDeviceLogin' { [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] $testDeviceName = 'DEVICE-001' [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] + $testDeviceType = 'Windows' + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] $testUserId = 'u1e1a1d7-2d2b-4d8c-9f0a-0d2a3d1e2f3a' [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] $testUserPrincipalName = 'user@contoso.com' @@ -223,9 +229,10 @@ Describe 'Get-IntuneDeviceLogin' { } $mockDevice = [PSCustomObject]@{ - id = $testDeviceId - deviceName = $testDeviceName - usersLoggedOn = @( + id = $testDeviceId + deviceName = $testDeviceName + operatingSystem = $testDeviceType + usersLoggedOn = @( [PSCustomObject]@{ userId = $testUserId lastLogOnDateTime = '2024-03-05T10:30:00Z' @@ -248,6 +255,7 @@ Describe 'Get-IntuneDeviceLogin' { # Assert $result | Should -Not -BeNullOrEmpty $result.DeviceName | Should -Be $testDeviceName + $result.OperatingSystem | Should -Be $testDeviceType $result.UserPrincipalName | Should -Be $testUserPrincipalName Assert-MockCalled -CommandName 'Resolve-IntuneDeviceByName' -Times 1 -Exactly Assert-MockCalled -CommandName 'Invoke-GraphGet' -Times 1 -Exactly @@ -585,9 +593,10 @@ Describe 'Get-IntuneDeviceLogin' { $mockDevicesResponse = [PSCustomObject]@{ value = @( [PSCustomObject]@{ - id = $testDeviceId1 - deviceName = 'DEVICE-001' - usersLoggedOn = @( + id = $testDeviceId1 + deviceName = 'DEVICE-001' + operatingSystem = 'Windows' + usersLoggedOn = @( [PSCustomObject]@{ userId = $testUserId lastLogOnDateTime = '2024-03-05T10:30:00Z' @@ -624,6 +633,7 @@ Describe 'Get-IntuneDeviceLogin' { $results.Count | Should -Be 1 $results[0].DeviceId | Should -Be $testDeviceId1 $results[0].DeviceName | Should -Be 'DEVICE-001' + $results[0].OperatingSystem | Should -Be 'Windows' $results[0].UserId | Should -Be $testUserId $results[0].UserPrincipalName | Should -Be $testUserPrincipalName } @@ -638,9 +648,10 @@ Describe 'Get-IntuneDeviceLogin' { $mockDevicesResponse = [PSCustomObject]@{ value = @( [PSCustomObject]@{ - id = $testDeviceId1 - deviceName = 'DEVICE-001' - usersLoggedOn = @( + id = $testDeviceId1 + deviceName = 'DEVICE-001' + operatingSystem = 'Windows' + usersLoggedOn = @( [PSCustomObject]@{ userId = $testUserId lastLogOnDateTime = '2024-03-05T10:30:00Z' @@ -737,9 +748,10 @@ Describe 'Get-IntuneDeviceLogin' { $mockDevicesResponse = [PSCustomObject]@{ value = @( [PSCustomObject]@{ - id = $testDeviceId1 - deviceName = 'DEVICE-001' - usersLoggedOn = @( + id = $testDeviceId1 + deviceName = 'DEVICE-001' + operatingSystem = 'Windows' + usersLoggedOn = @( [PSCustomObject]@{ userId = $testUserId lastLogOnDateTime = '2024-03-05T10:30:00Z' @@ -1064,9 +1076,10 @@ Describe 'Get-IntuneDeviceLogin' { $mockDevicesResponse = [PSCustomObject]@{ value = @( [PSCustomObject]@{ - id = $testDeviceId1 - deviceName = 'DEVICE-001' - usersLoggedOn = @( + id = $testDeviceId1 + deviceName = 'DEVICE-001' + operatingSystem = 'Windows' + usersLoggedOn = @( [PSCustomObject]@{ userId = $testUserId lastLogOnDateTime = '2024-03-05T10:30:00Z' @@ -1085,8 +1098,12 @@ Describe 'Get-IntuneDeviceLogin' { # Assert $results.Count | Should -Be 1 $results[0].DeviceId | Should -Be $testDeviceId1 + $results[0].OperatingSystem | Should -Be 'Windows' $results[0].UserId | Should -Be $testUserId $results[0].UserPrincipalName | Should -Be $testUserPrincipalName + Assert-MockCalled -CommandName 'Invoke-GraphGet' -Times 1 -Exactly -ParameterFilter { + $Uri -match 'managedDevices\?\$select=id,deviceName,operatingSystem,usersLoggedOn' + } } It 'Should reject invalid GUID format for UserId' {