Plugins:WinUsers

From OCS Inventory NG
Jump to: navigation, search

Retrieve Windows Users

Windows Agent configuration

Create the script

This script is a VB script. You have to place it in C:\Program Files\OCS Inventory Agent\Plugins . The name is not important. Nothing more to do.

Code Select the script that you want to use.

Script 1

'---------------------------------------------------------- 
' Liste des utilisateurs 
' Auteur : ' J.C.BELLAMY © 2000 
' Adaptation pour OCS : Guillaume PRIOU
'----------------------------------------------------------
Dim network, computer, SAM, Item 
Set network = Wscript.CreateObject("WScript.Network") 
computer=network.ComputerName 
 
set SAM=GetObject("WinNT://" & computer & ",computer") 
for each Item in SAM 
   Classe=Item.Class 
   If Classe = "User" then 
      wscript.echo "<WINUSERS>"
      wscript.echo "<NAME>" & Item.name & "</NAME>"
      wscript.echo "</WINUSERS>"
   End if 
next

Script 2

'---------------------------------------------------------- 
' Liste des utilisateurs 
' Source : Net
' Adaptation pour OCS : Guillaume PRIOU
'----------------------------------------------------------
Dim stRep 'Nom du répertoire à parcourir
Dim oFSO,oFld
Set oFSO = CreateObject("Scripting.FileSystemObject")
stRep = "C:\Documents and Settings"
If oFSO.FolderExists(stRep) Then
 For each oFld in  oFSO.GetFolder(stRep).SubFolders
   wscript.echo "<WINUSERS>"
   Wscript.Echo "<NAME>" & oFld.Name & "</NAME>"
   wscript.echo "</WINUSERS>"
 Next
End If

Check the result

Result is inserted in XML file generated by agent. If you want to store this file in local, launch your agent with option /XML="C:\Documents and Settings\All Users\Application Data\OCS Inventory NG\Agent".

<WINUSERS>
            <NAME>Administrateur</NAME>
        </WINUSERS>
        <WINUSERS>
            <NAME>All Users</NAME>
        </WINUSERS>
        <WINUSERS>
            <NAME>Default User</NAME>
        </WINUSERS>
        <WINUSERS>
            <NAME>Guillaume PRIOU</NAME>
        </WINUSERS>
        <WINUSERS>
            <NAME>LocalService</NAME>
        </WINUSERS>
        <WINUSERS>
            <NAME>NetworkService</NAME>
        </WINUSERS>

Database server

Create table in database to store informations

You have to create a new table which will receive new data

In this exemple, create table winusers with ID and NAME.

CREATE TABLE `winusers` (`ID` INT(11) NOT NULL AUTO_INCREMENT,`HARDWARE_ID` INT(11) NOT NULL,
 `NAME` VARCHAR(255) DEFAULT NULL, PRIMARY KEY  (`ID`,`HARDWARE_ID`)) ENGINE=InnoDB;

Management server

Modifiy the engine

You have to modify Map.pm ( in /usr/local/share/perl/5.10.0/Apache/Ocsinventory/ )

#Plugin WINDOWS USERS
winusers => {
mask => 0,
multi => 1,
auto => 1,
delOnReplace => 1,
sortBy => 'NAME',
writeDiff => 0,
cache => 0,
fields => {
     NAME => {},
}
 
},
Warning: When you will upgrade your OCS Inventory Server, Map.pm will be overwrite. Don't forget to save this file before upgrading.


Communication server

Create the workspace

First, it's important to respect files architecture. It's necessary to create a new branch in the

/plugins/computer_detail directory

and named your new directory

cd_<your_module_name> 

In our case, cd_winusers. This directory will contain cd_winusers.php.

Plugin winusers 1.jpg

Then, import your 3 icons in

/plugins/computer_detail/img

The name of the new icon will be in the form cd_<your_module_name>.png for inactive icon and cd_<your_module_name>_a.png for active icon. Add an icon is not an obligation because you can add your module to a menu already in place or even create a menu on a icon.


Here, icons you can use : I retrieve ms_users.png and ms_users_a.png from /plugins/main_sections/img to rename in cd_winusers.png and cd_winusers_a.png

Cd users.pngCd users a.png

Create the PHP file

In our case, we have just to display a table with one colomn. So I will use an existant page : /plugins/cd_registry/cd_registry.php. I copy it in

/plugins/cd_winusers/

and rename it in

cd_winusers.php

I modify the code simply to display what I want

<?php
//====================================================================================
// OCS INVENTORY REPORTS
// Model page : Erwan GOALOU
// Copyleft Guillaume RPIOU 2011 (guillaume.priou(at)ocsinventory-ng(pt)org)
// Web: http://www.ocsinventory-ng.org
//
// This code is open source and may be copied and modified as long as the source
// code is always made freely available.
// Please refer to the General Public Licence http://www.gnu.org/ or Licence.txt
//====================================================================================
 
	print_item_header('WINDOWS USERS');
	$form_name="affich_users";
	$table_name=$form_name;
	echo "<form name='".$form_name."' id='".$form_name."' method='POST' action=''>";
	$list_fields=array('NAME' => 'NAME');
	$list_col_cant_del=$list_fields;
	$default_fields= $list_fields;
	$tab_options['FILTRE']=array('NAME'=>'NAME');
	$queryDetails  = "SELECT * FROM winusers WHERE (hardware_id=$systemid)";
	tab_req($table_name,$list_fields,$default_fields,$list_col_cant_del,$queryDetails,$form_name,80,$tab_options);
	echo "</form>";
?>


Warning: You need to fix good permissions on cd_winusers.php.


chmod 0755 ./cd_winusers.php
chown root:www-data ./cd_winusers.php

Activate the plugin

You have to activate your plugin in

/plugins/computer_detail/cd_config.txt

Here, just the diff of this file.

<ORDER>
.......
.......
25:cd_winusers
</ORDER>

<LBL>
.......
.......
cd_winusers:cd_winusers
</LBL>

<ISAVAIL>
.......
.......
cd_winusers:winusers
</ISAVAIL>

<URL>
.......
cd_winusers:25
.......
</URL>

Display the result in administration console

Connect to the GUI, select a machine, and click on new icon. You can see the users list.

Plugin winusers 3.jpg