From bda8f6b8acb66b2aa1818cf275cdb138208e4572 Mon Sep 17 00:00:00 2001 From: Frederik Hjorslev Nylander Date: Thu, 12 Mar 2026 10:25:40 +0100 Subject: [PATCH 1/2] Add OperatingSystem property --- .../public/Device/Get-IntuneDeviceLogin.ps1 | 6 +++++- .../Device/Get-IntuneDeviceLogin.Tests.ps1 | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/functions/public/Device/Get-IntuneDeviceLogin.ps1 b/src/functions/public/Device/Get-IntuneDeviceLogin.ps1 index 605c1a5..0a1f475 100644 --- a/src/functions/public/Device/Get-IntuneDeviceLogin.ps1 +++ b/src/functions/public/Device/Get-IntuneDeviceLogin.ps1 @@ -59,6 +59,7 @@ PSCustomObject with the following properties - DeviceId (string) - DeviceName (string) + - OperatingSystem (string) - UserId (string) - UserPrincipalName (string) - LastLogonDateTime (datetime) @@ -168,6 +169,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 +271,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 +321,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 +359,7 @@ $user = Resolve-EntraUserById -UserId $targetUserId [PSCustomObject]@{ DeviceName = $device.deviceName + OperatingSystem = $device.operatingSystem UserPrincipalName = $user.userPrincipalName DeviceId = $device.id UserId = $targetUserId diff --git a/tests/public/Device/Get-IntuneDeviceLogin.Tests.ps1 b/tests/public/Device/Get-IntuneDeviceLogin.Tests.ps1 index 18736dd..2307241 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' @@ -33,6 +35,7 @@ Describe 'Get-IntuneDeviceLogin' { $mockDevice = [PSCustomObject]@{ id = $testDeviceId deviceName = $testDeviceName + operatingSystem = $testDeviceType usersLoggedOn = @( [PSCustomObject]@{ userId = $testUserId @@ -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' @@ -225,6 +231,7 @@ Describe 'Get-IntuneDeviceLogin' { $mockDevice = [PSCustomObject]@{ id = $testDeviceId deviceName = $testDeviceName + operatingSystem = $testDeviceType usersLoggedOn = @( [PSCustomObject]@{ userId = $testUserId @@ -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 @@ -587,6 +595,7 @@ Describe 'Get-IntuneDeviceLogin' { [PSCustomObject]@{ id = $testDeviceId1 deviceName = 'DEVICE-001' + operatingSystem = 'Windows' usersLoggedOn = @( [PSCustomObject]@{ userId = $testUserId @@ -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 } @@ -640,6 +650,7 @@ Describe 'Get-IntuneDeviceLogin' { [PSCustomObject]@{ id = $testDeviceId1 deviceName = 'DEVICE-001' + operatingSystem = 'Windows' usersLoggedOn = @( [PSCustomObject]@{ userId = $testUserId @@ -739,6 +750,7 @@ Describe 'Get-IntuneDeviceLogin' { [PSCustomObject]@{ id = $testDeviceId1 deviceName = 'DEVICE-001' + operatingSystem = 'Windows' usersLoggedOn = @( [PSCustomObject]@{ userId = $testUserId @@ -1066,6 +1078,7 @@ Describe 'Get-IntuneDeviceLogin' { [PSCustomObject]@{ id = $testDeviceId1 deviceName = 'DEVICE-001' + operatingSystem = 'Windows' usersLoggedOn = @( [PSCustomObject]@{ userId = $testUserId @@ -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' { From 0676290c28d18a7ae6ca1c1ac8335d7bb0befc02 Mon Sep 17 00:00:00 2001 From: Frederik Hjorslev Nylander Date: Thu, 12 Mar 2026 10:39:54 +0100 Subject: [PATCH 2/2] fix linter and change RequireModules --- .../public/Device/Get-IntuneDeviceLogin.ps1 | 4 ++- src/init/initializer.ps1 | 1 - .../Device/Get-IntuneDeviceLogin.Tests.ps1 | 36 +++++++++---------- 3 files changed, 21 insertions(+), 20 deletions(-) delete mode 100644 src/init/initializer.ps1 diff --git a/src/functions/public/Device/Get-IntuneDeviceLogin.ps1 b/src/functions/public/Device/Get-IntuneDeviceLogin.ps1 index 0a1f475..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. 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 2307241..0acec9b 100644 --- a/tests/public/Device/Get-IntuneDeviceLogin.Tests.ps1 +++ b/tests/public/Device/Get-IntuneDeviceLogin.Tests.ps1 @@ -33,10 +33,10 @@ Describe 'Get-IntuneDeviceLogin' { It 'Should return a PSCustomObject with logged-on user information' { # Arrange $mockDevice = [PSCustomObject]@{ - id = $testDeviceId - deviceName = $testDeviceName + id = $testDeviceId + deviceName = $testDeviceName operatingSystem = $testDeviceType - usersLoggedOn = @( + usersLoggedOn = @( [PSCustomObject]@{ userId = $testUserId lastLogOnDateTime = $testLastLogonDateTime @@ -229,10 +229,10 @@ Describe 'Get-IntuneDeviceLogin' { } $mockDevice = [PSCustomObject]@{ - id = $testDeviceId - deviceName = $testDeviceName + id = $testDeviceId + deviceName = $testDeviceName operatingSystem = $testDeviceType - usersLoggedOn = @( + usersLoggedOn = @( [PSCustomObject]@{ userId = $testUserId lastLogOnDateTime = '2024-03-05T10:30:00Z' @@ -593,10 +593,10 @@ Describe 'Get-IntuneDeviceLogin' { $mockDevicesResponse = [PSCustomObject]@{ value = @( [PSCustomObject]@{ - id = $testDeviceId1 - deviceName = 'DEVICE-001' + id = $testDeviceId1 + deviceName = 'DEVICE-001' operatingSystem = 'Windows' - usersLoggedOn = @( + usersLoggedOn = @( [PSCustomObject]@{ userId = $testUserId lastLogOnDateTime = '2024-03-05T10:30:00Z' @@ -648,10 +648,10 @@ Describe 'Get-IntuneDeviceLogin' { $mockDevicesResponse = [PSCustomObject]@{ value = @( [PSCustomObject]@{ - id = $testDeviceId1 - deviceName = 'DEVICE-001' + id = $testDeviceId1 + deviceName = 'DEVICE-001' operatingSystem = 'Windows' - usersLoggedOn = @( + usersLoggedOn = @( [PSCustomObject]@{ userId = $testUserId lastLogOnDateTime = '2024-03-05T10:30:00Z' @@ -748,10 +748,10 @@ Describe 'Get-IntuneDeviceLogin' { $mockDevicesResponse = [PSCustomObject]@{ value = @( [PSCustomObject]@{ - id = $testDeviceId1 - deviceName = 'DEVICE-001' + id = $testDeviceId1 + deviceName = 'DEVICE-001' operatingSystem = 'Windows' - usersLoggedOn = @( + usersLoggedOn = @( [PSCustomObject]@{ userId = $testUserId lastLogOnDateTime = '2024-03-05T10:30:00Z' @@ -1076,10 +1076,10 @@ Describe 'Get-IntuneDeviceLogin' { $mockDevicesResponse = [PSCustomObject]@{ value = @( [PSCustomObject]@{ - id = $testDeviceId1 - deviceName = 'DEVICE-001' + id = $testDeviceId1 + deviceName = 'DEVICE-001' operatingSystem = 'Windows' - usersLoggedOn = @( + usersLoggedOn = @( [PSCustomObject]@{ userId = $testUserId lastLogOnDateTime = '2024-03-05T10:30:00Z'