From 7d31ddd11a39c1e8cde3a1b6cd1ed48f2d785f03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charl=C3=A8ne=20Auger?= Date: Wed, 15 Sep 2021 17:41:14 +0200 Subject: [PATCH 1/3] Add try catch --- agent/winusers.ps1 | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/agent/winusers.ps1 b/agent/winusers.ps1 index fe0a44c..14150cb 100644 --- a/agent/winusers.ps1 +++ b/agent/winusers.ps1 @@ -16,10 +16,13 @@ function Get-AdminUser { function Get-Size { param([string]$pth) - "{0:n2}" -f ((gci -path $pth -recurse | measure-object -property length -sum).sum /1mb) + try { + "{0:n2}" -f ((gci -path $pth -recurse -ErrorAction Ignore | measure-object -ErrorAction Stop -property length -sum).sum /1mb) + } catch { + "{0:n2}" -f 0 + } } - $users = Get-LocalUser | Select * $pathUsers = "C:\Users" $allUsers = @() @@ -32,17 +35,17 @@ foreach ($user in $users) { $folderSize = Get-Size $path if($user.Enabled -ne "False") { $userStatus = "Disabled" } else { $userStatus = "Enabled" } - $xml += "" - $xml += ""+ $user.Name +"" - $xml += ""+ $userType +"" - $xml += ""+ $folderSize +"" - $xml += ""+ $user.LastLogon +"" - $xml += ""+ $user.Description +"" - $xml += ""+ $userStatus +"" - $xml += ""+ $user.UserMayChangePassword +"" - $xml += ""+ $user.PasswordExpires +"" - $xml += ""+ $user.SID +"" - $xml += "" + $xml += "`n" + $xml += ""+ $user.Name +"`n" + $xml += ""+ $userType +"`n" + $xml += ""+ $folderSize +"`n" + $xml += ""+ $user.LastLogon +"`n" + $xml += ""+ $user.Description +"`n" + $xml += ""+ $userStatus +"`n" + $xml += ""+ $user.UserMayChangePassword +"`n" + $xml += ""+ $user.PasswordExpires +"`n" + $xml += ""+ $user.SID +"`n" + $xml += "`n" $allUsers += $user.Name } @@ -61,11 +64,11 @@ foreach ($userAd in $usersAd) { $path = "C:\Users\"+ $userAd $folderSize = Get-Size $path - $xml += "" - $xml += ""+ $userAd +"" - $xml += "Domain user" - $xml += ""+ $folderSize +"" - $xml += "" + $xml += "`n" + $xml += ""+ $userAd +"`n" + $xml += "Domain user`n" + $xml += ""+ $folderSize +"`n" + $xml += "`n" } [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 From 032c79246569f2b5466fc7e46682f589100a9172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charl=C3=A8ne=20Auger?= Date: Wed, 15 Sep 2021 17:41:26 +0200 Subject: [PATCH 2/3] UPdate version --- infos.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/infos.json b/infos.json index 8b9dc87..0f9e828 100644 --- a/infos.json +++ b/infos.json @@ -1,9 +1,9 @@ { "displayName" : "Winusers", "author" : ["J.C.BELLAMY"], - "contributor" : ["Guillaume PRIOU", "Gilles DUBOIS", "Rudy LAURENT"], + "contributor" : ["Guillaume PRIOU", "Gilles DUBOIS", "Rudy LAURENT", "Charlene AUGER"], "supportedAgent" : ["Windows"], - "version" : "3.1", + "version" : "3.2", "licence" : "GPLv2", "description" : { "fr" : "Remonte les utilisateurs Windows", From f9f0d802c51cdeb3cacb38060432e83a511b822d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charl=C3=A8ne=20Auger?= Date: Wed, 15 Sep 2021 17:48:25 +0200 Subject: [PATCH 3/3] Use principal source for user type if not admin --- agent/winusers.ps1 | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/agent/winusers.ps1 b/agent/winusers.ps1 index 14150cb..16bea04 100644 --- a/agent/winusers.ps1 +++ b/agent/winusers.ps1 @@ -1,12 +1,12 @@ function Get-AdminUser { param([string] $username) $admingroup = Get-LocalGroupMember -SID "S-1-5-32-544" - $userType = "Local user" + $userType = "Local" foreach ($admin in $admingroup) { $name = $admin.name -split "\\" if($name[1] -eq $username){ - $userType = "Admin user" + $userType = "Admin" } } @@ -34,7 +34,8 @@ foreach ($user in $users) { $path = "C:\Users\"+ $user.Name $folderSize = Get-Size $path if($user.Enabled -ne "False") { $userStatus = "Disabled" } else { $userStatus = "Enabled" } - + if($userType -eq "Local") { $userType = $user.PrincipalSource } + $xml += "`n" $xml += ""+ $user.Name +"`n" $xml += ""+ $userType +"`n" @@ -66,7 +67,7 @@ foreach ($userAd in $usersAd) { $xml += "`n" $xml += ""+ $userAd +"`n" - $xml += "Domain user`n" + $xml += "Domain`n" $xml += ""+ $folderSize +"`n" $xml += "`n" }