Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/functions/public/Device/Get-IntuneDeviceLogin.ps1
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -59,6 +61,7 @@
PSCustomObject with the following properties
- DeviceId (string)
- DeviceName (string)
- OperatingSystem (string)
- UserId (string)
- UserPrincipalName (string)
- LastLogonDateTime (datetime)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = @()
Expand Down Expand Up @@ -356,6 +361,7 @@
$user = Resolve-EntraUserById -UserId $targetUserId
[PSCustomObject]@{
DeviceName = $device.deviceName
OperatingSystem = $device.operatingSystem
UserPrincipalName = $user.userPrincipalName
DeviceId = $device.id
UserId = $targetUserId
Expand Down
1 change: 0 additions & 1 deletion src/init/initializer.ps1

This file was deleted.

53 changes: 35 additions & 18 deletions tests/public/Device/Get-IntuneDeviceLogin.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -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'
Expand All @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
}
Expand All @@ -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'
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand All @@ -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' {
Expand Down
Loading