From deca4c8ddb0db3f8eed9a7a66e8d8f7ea6b17ca5 Mon Sep 17 00:00:00 2001 From: charleneauger Date: Tue, 14 Apr 2020 17:06:01 +0200 Subject: [PATCH 1/2] Rework agent plugin in powershell --- agent/winusers.ps1 | 48 ++++++++++++++++ agent/winusers.vbs | 112 ------------------------------------ cd_winusers/cd_winusers.php | 2 + install.php | 2 + 4 files changed, 52 insertions(+), 112 deletions(-) create mode 100644 agent/winusers.ps1 delete mode 100644 agent/winusers.vbs diff --git a/agent/winusers.ps1 b/agent/winusers.ps1 new file mode 100644 index 0000000..760e228 --- /dev/null +++ b/agent/winusers.ps1 @@ -0,0 +1,48 @@ +function Get-AdminUser { + param([string] $username) + $admingroup = Get-LocalGroupMember -Group "Administrateurs" + $userType = "Local user" + + foreach ($admin in $admingroup) { + $name = $admin.name -split "\\" + if($name[1] -eq $username){ + $userType = "Admin user" + } + } + + return $userType +} + +function Get-Size +{ + param([string]$pth) + write-host $pth + "{0:n2}" -f ((gci -path $pth -recurse | measure-object -property length -sum).sum /1mb) +} + + +$users = Get-LocalUser | Select * + +foreach ($user in $users) { + if($user.Name -ne $null){ + + $userType = Get-AdminUser $user.Name + $path = "C:\Users\"+ $user.Name + $folderSize = Get-Size $path + if($user.Enabled -ne "False") { $userStatus = "Disabled" } else { $userStatus = "Enabled" } + + $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" + } +} + +Write-Host $xml diff --git a/agent/winusers.vbs b/agent/winusers.vbs deleted file mode 100644 index 55d971b..0000000 --- a/agent/winusers.vbs +++ /dev/null @@ -1,112 +0,0 @@ -'---------------------------------------------------------- -' Plugin for OCS Inventory NG 2.x -' Script : Users list -' Version : 2.20 -' Date : 01/02/2018 -' Authors : J.C. BELLAMY © 2000 and Stéphane PAUTREL (acb78.com) -' OCS adaptation : Guillaume PRIOU -'---------------------------------------------------------- -' OS checked [X] on 32b 64b (Professionnal edition) -' Windows XP [X] -' Windows Vista [X] [X] -' Windows 7 [X] [X] -' Windows 8.1 [X] [X] -' Windows 10 [X] [X] -' Windows 2k8R2 [X] -' Windows 2k12R2 [X] -' Windows 2k16 [X] -' --------------------------------------------------------- -' NOTE : No checked on Windows 8 -' --------------------------------------------------------- -On Error Resume Next - -Dim Network, objFSO, Computer, objWMIService -Dim colItems, objItem, colAdmGroup, UserType, UserStatus, objAdm -Dim accent, noaccent, currentChar, result, k, o - -Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject") -Set Network = Wscript.CreateObject("WScript.Network") -Computer=Network.ComputerName - -Function StripAccents(str) - accent = "ÈÉÊËÛÙÏÎÀÂÔÖÇèéêëûùïîàâôöç" - noaccent = "EEEEUUIIAAOOCeeeeuuiiaaooc" - currentChar = "" - result = "" - k = 0 - o = 0 - For k = 1 To len(str) - currentChar = mid(str,k, 1) - o = InStr(accent, currentChar) - If o > 0 Then - result = result & mid(noaccent,o,1) - Else - result = result & currentChar - End If - Next - StripAccents = result -End Function - -Function IfAdmin(str) - Set colAdmGroup = GetObject("WinNT://./Administrateurs") ' get members of the local admin group - UserType = "Local user" - For Each objAdm In colAdmGroup.Members - If objAdm.Name = objItem.Name Then - UserType = "Local admin" - End If - Next -End Function - -Function getFolderSize(folderName) - On Error Resume Next - size = 0 - hasSubfolders = False - Set folder = objFSO.GetFolder(folderName) - Err.Clear - size = folder.Size - - If Err.Number <> 0 then - For Each subfolder in folder.SubFolders - size = size + getFolderSize(subfolder.Path) - hasSubfolders = True - Next - - If not hasSubfolders then - size = folder.Size - End If - End If - - getFolderSize = size - - Set folder = Nothing -End Function - -Set objWMIService = GetObject("winmgmts:root\cimv2") - -Set colItems = objWMIService.ExecQuery _ - ("Select * from Win32_UserAccount Where LocalAccount = True") - -For Each objItem in colItems - IfAdmin(objItem.Name) - UserStatus = objItem.Disabled - If objItem.Disabled = "False" Or objItem.Disabled = "Faux" Then UserStatus = "Actif" ' or Enabled in your native language - If objItem.Disabled = "True" Or objItem.Disabled = "Vrai" Then UserStatus = "Inactif" ' or Disabled in your native language - Set objFolder = objFSO.GetFolder("C:\Users\" & objItem.Name & "") - - dtmLastLogin = "NC" ' Default text (for ex. Never connected) - On Error Resume Next - Set objUser = GetObject("WinNT://./" & objItem.Name & ",user") - dtmLastLogin = objUser.LastLogin - - Wscript.echo _ - "" & VbCrLf &_ - "" & StripAccents(objItem.Name) & "" & VbCrLf &_ - "" & UserType & "" & VbCrLf &_ - "" & round(getFolderSize(objFolder)/(1024*1024),0) & "" & VbCrLf &_ - "" & dtmLastLogin & "" & VbCrLf &_ - "" & StripAccents(objItem.Description) & "" & VbCrLf &_ - "" & UserStatus & "" & VbCrLf &_ - "" & objItem.SID & "" & VbCrLf &_ - "" - Set objFolder = Nothing -Next \ No newline at end of file diff --git a/cd_winusers/cd_winusers.php b/cd_winusers/cd_winusers.php index cc44ed5..1af35aa 100644 --- a/cd_winusers/cd_winusers.php +++ b/cd_winusers/cd_winusers.php @@ -33,6 +33,8 @@ 'Last logon' => 'lastlogon', 'Description' => 'description', 'Status' => 'status', + 'Change Password' => 'usermaychangepwd' + 'Password expires' => 'passwordexpires' 'Sid' => 'sid' ); $list_col_cant_del=$list_fields; diff --git a/install.php b/install.php index ddd1830..4b0d8c0 100644 --- a/install.php +++ b/install.php @@ -16,6 +16,8 @@ function extension_install_winusers() `LASTLOGON` VARCHAR(255) DEFAULT NULL, `DESCRIPTION` VARCHAR(255) DEFAULT NULL, `STATUS` VARCHAR(255) DEFAULT NULL, + `USERMAYCHANGEPWD` VARCHAR(255) DEFAULT NULL, + `PASSWORDEXPIRES` VARCHAR(255) DEFAULT NULL, `SID` VARCHAR(255) DEFAULT NULL, PRIMARY KEY (`ID`,`HARDWARE_ID`) ) ENGINE=InnoDB ;"); From b97a13d45a3bafbe67f459d26d8003f4b6156521 Mon Sep 17 00:00:00 2001 From: charleneauger Date: Wed, 15 Apr 2020 09:13:15 +0200 Subject: [PATCH 2/2] Update Map.pm and drop table before create --- APACHE/Map.pm | 2 ++ install.php | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/APACHE/Map.pm b/APACHE/Map.pm index 10200f2..2a2b189 100644 --- a/APACHE/Map.pm +++ b/APACHE/Map.pm @@ -28,6 +28,8 @@ fields => { SIZE => {}, LASTLOGON => {}, DESCRIPTION => {}, + USERMAYCHANGEPWD => {}, + PASSWORDEXPIRES => {}, STATUS => {}, SID => {} } diff --git a/install.php b/install.php index 4b0d8c0..2dcd45e 100644 --- a/install.php +++ b/install.php @@ -7,7 +7,10 @@ function extension_install_winusers() { $commonObject = new ExtensionCommon; - $commonObject -> sqlQuery("CREATE TABLE IF NOT EXISTS `winusers` ( + // Drop table first + $commonObject -> sqlQuery("DROP TABLE `winusers`;"); + + $commonObject -> sqlQuery("CREATE TABLE `winusers` ( `ID` INT(11) NOT NULL AUTO_INCREMENT, `HARDWARE_ID` INT(11) NOT NULL, `NAME` VARCHAR(255) DEFAULT NULL,