bloginfo

Inventaire Logiciel Windows

Sep 25th, 2017 (edited)
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.     Inventaire logiciel de votre station Windows - 2017/09/25 - Version 1.0
  3.     Copyright © 2017 - Denis Szalkowski Formateur Consultant - https://www.dsfc.net - Licence GPL 2
  4. #>
  5. <#
  6. -- Création de la table dans MySQL
  7. CREATE TABLE `logiciel` (
  8.     `logiciel_md5` CHAR(32) NOT NULL,
  9.     `logiciel_chemin` VARCHAR(256) NOT NULL,
  10.     `logiciel_creation` DATETIME NOT NULL,
  11.     `logiciel_modification` DATETIME NOT NULL,
  12.     `logiciel_taille` INT(11) NOT NULL,
  13.     `logiciel_editeur` VARCHAR(255) NULL DEFAULT NULL,
  14.     `logiciel_version` VARCHAR(255) NULL DEFAULT NULL,
  15.     `logiciel_description` VARCHAR(255) NULL DEFAULT NULL,
  16.     `logiciel_date` DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
  17.     UNIQUE INDEX `logiciel_uk` (`logiciel_md5`, `logiciel_chemin`)
  18. );
  19. #>
  20. Clear-Host
  21. # Chargement de l'assembly MySQL
  22. [void][system.reflection.Assembly]::LoadFrom("C:\Program Files (x86)\MySQL\MySQL Connector Net 6.9.9\Assemblies\v4.5\MySql.Data.dll")
  23. Try
  24. {
  25.     # Connexion à la base MySQL
  26.     $strConn="DataSource='127.0.0.1';Database='inventaire';User ID='root';Password='root'"
  27.     $oConn = New-Object MySql.Data.MySqlClient.MySqlConnection
  28.     $oConn.ConnectionString = $strConn
  29.     $oConn.Open()
  30.     # Liste des fichiers exécutables
  31.     $files=Get-ChildItem -Path 'C:\*.*' -ErrorAction SilentlyContinue -Recurse -File|Select FullName,LastWriteTime,Length,VersionInfo,CreationTime |Where {$_.FullName -notmatch '\\(DriverStore|Lastgood|Microsoft\.NET\\Framework(64)?|WinSxS|wsusoffline)\\' -AND $_.FullName -match '\.(bin|com|dll|drv|exe|ocx|sys)$'}
  32.     $sql = New-Object MySql.Data.MySqlClient.MySqlCommand
  33.     $sql.Connection = $oConn
  34.     ForEach($file in $files)
  35.     {
  36.         # Récupération des informations sur les fichiers
  37.         $chemin=$file.FullName.Replace('\','/')
  38.         $creation=Get-Date $file.CreationTime -format 'yyyy-MM-dd HH:mm:ss'
  39.         $modification=Get-Date $file.LastWriteTime -format 'yyyy-MM-dd HH:mm:ss'
  40.         $taille=$file.Length
  41.         $editeur=$file.VersionInfo.CompanyName
  42.         $version=$file.VersionInfo.FileVersion
  43.         $description=$file.VersionInfo.FileDescription
  44.         $md5=Get-FileHash -Algorithm MD5 -path  $chemin | select Hash
  45.         # Insertion des données dans la table
  46.         $sql.CommandText = "INSERT INTO logiciel(logiciel_md5,logiciel_chemin,logiciel_creation,logiciel_modification,logiciel_taille,logiciel_editeur,logiciel_version,logiciel_description) VALUES ('$($md5.Hash)','$chemin','$creation','$modification',$taille,'$editeur','$version','$description')"
  47.         Try
  48.         {
  49.             $sql.ExecuteNonQuery()
  50.         }
  51.         Catch
  52.         {
  53.         }
  54.     }
  55.     $sql.Dispose()
  56.     $sql=$null
  57.     $file=$null
  58.     $files=$null
  59. }
  60. Catch [System.Exception]
  61. {
  62.     $e = $_.Exception
  63.     Write-Host $e.Message
  64.     $e=$null
  65. }
  66. Finally
  67. {
  68.     $oConn.Close()
  69.     $oConn.Dispose()
  70.     $oConn=$null
  71. }
Add Comment
Please, Sign In to add comment