Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- Inventaire logiciel de votre station Windows - 2017/09/25 - Version 1.0
- Copyright © 2017 - Denis Szalkowski Formateur Consultant - https://www.dsfc.net - Licence GPL 2
- #>
- <#
- -- Création de la table dans MySQL
- CREATE TABLE `logiciel` (
- `logiciel_md5` CHAR(32) NOT NULL,
- `logiciel_chemin` VARCHAR(256) NOT NULL,
- `logiciel_creation` DATETIME NOT NULL,
- `logiciel_modification` DATETIME NOT NULL,
- `logiciel_taille` INT(11) NOT NULL,
- `logiciel_editeur` VARCHAR(255) NULL DEFAULT NULL,
- `logiciel_version` VARCHAR(255) NULL DEFAULT NULL,
- `logiciel_description` VARCHAR(255) NULL DEFAULT NULL,
- `logiciel_date` DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
- UNIQUE INDEX `logiciel_uk` (`logiciel_md5`, `logiciel_chemin`)
- );
- #>
- Clear-Host
- # Chargement de l'assembly MySQL
- [void][system.reflection.Assembly]::LoadFrom("C:\Program Files (x86)\MySQL\MySQL Connector Net 6.9.9\Assemblies\v4.5\MySql.Data.dll")
- Try
- {
- # Connexion à la base MySQL
- $strConn="DataSource='127.0.0.1';Database='inventaire';User ID='root';Password='root'"
- $oConn = New-Object MySql.Data.MySqlClient.MySqlConnection
- $oConn.ConnectionString = $strConn
- $oConn.Open()
- # Liste des fichiers exécutables
- $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)$'}
- $sql = New-Object MySql.Data.MySqlClient.MySqlCommand
- $sql.Connection = $oConn
- ForEach($file in $files)
- {
- # Récupération des informations sur les fichiers
- $chemin=$file.FullName.Replace('\','/')
- $creation=Get-Date $file.CreationTime -format 'yyyy-MM-dd HH:mm:ss'
- $modification=Get-Date $file.LastWriteTime -format 'yyyy-MM-dd HH:mm:ss'
- $taille=$file.Length
- $editeur=$file.VersionInfo.CompanyName
- $version=$file.VersionInfo.FileVersion
- $description=$file.VersionInfo.FileDescription
- $md5=Get-FileHash -Algorithm MD5 -path $chemin | select Hash
- # Insertion des données dans la table
- $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')"
- Try
- {
- $sql.ExecuteNonQuery()
- }
- Catch
- {
- }
- }
- $sql.Dispose()
- $sql=$null
- $file=$null
- $files=$null
- }
- Catch [System.Exception]
- {
- $e = $_.Exception
- Write-Host $e.Message
- $e=$null
- }
- Finally
- {
- $oConn.Close()
- $oConn.Dispose()
- $oConn=$null
- }
Add Comment
Please, Sign In to add comment