From 175248ec4d8149c78c5ed6e50807f91f7996f311 Mon Sep 17 00:00:00 2001 From: David Yoder Date: Mon, 26 Feb 2024 16:10:34 -0500 Subject: [PATCH] better profile path selection --- Get-BackupCapacityPlanningData.ps1 | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/Get-BackupCapacityPlanningData.ps1 b/Get-BackupCapacityPlanningData.ps1 index cf86ea1..2b4d63c 100644 --- a/Get-BackupCapacityPlanningData.ps1 +++ b/Get-BackupCapacityPlanningData.ps1 @@ -8,21 +8,23 @@ Function Update-ListOfPaths { Function Add-UserProfilePaths { param([System.Collections.ArrayList]$List,[string]$Path) - # Get a list of all user profile directories - $ProfilePath = (Get-ItemPropertyValue -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList' -Name ProfilesDirectory) - $UserProfilePaths = @() - ForEach ( $userProfile in (Get-ChildItem -Path $ProfilePath | Where-Object { $_.PSIsContainer }) ) { - $UserProfilePaths += ,@($userProfile.FullName) - } + $ChildPath = ($Path -split '%')[2] - # Iterate over each $profilePath and join it with the supplied $Path - ForEach ( $profilePath in $UserProfilePaths ) { + # Get a list of all user profile directories + ForEach ( $profile in (Get-ChildItem -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList') ) { + $profileImagePath = Get-ItemPropertyValue -Path $([System.Environment]::ExpandEnvironmentVariables($profile.PSPath)) -Name ProfileImagePath + + # Skip profiles located in C:\Windows as MSP360 doesn't currently include these when using the %UserProfile% variable + If ( $profileImagePath -ilike "*${Env:WinDir}*" ) { Continue } # Build the path we're trying to add - $path = $(Join-Path -Path $profilePath -ChildPath $($Path -split '%')[2]) + $path = Join-Path -Path $profileImagePath -ChildPath $ChildPath + + # Skip adding the path if it doesn't exist + If ( !(Test-Path $path) ) { Continue } - # Only add the path if it exists (domain computers may have many invalid/old user accounts with nonexistant paths) - If ( Test-Path $path ) { Update-ListOfPaths -List $List -Path $path } + # Add the path + Update-ListOfPaths -List $List -Path $path } }