Windows

Export_History_Powershell.ps1
# Récupère le chemin du répertoire du script
$scriptDirectory = $PSScriptRoot
 
# Nom de la machine
$machineName = $env:COMPUTERNAME
$Directory = "History-Logs"
 
# Crée le répertoire 'History-Logs' s'il n'existe pas
$fullDirectoryPath = Join-Path $scriptDirectory $Directory
if (-not (Test-Path $fullDirectoryPath)) {
    New-Item -ItemType Directory -Path $fullDirectoryPath
}
 
# Nom de fichier avec le nom de la machine, la date et l'heure
$dateTime = (Get-Date).ToString('yyyyMMdd-HHmm')
$exportFileName = "${machineName}_Full_History_PowerShell_export_$dateTime.txt"
$exportPath = Join-Path $fullDirectoryPath $exportFileName
 
# Chemin vers le fichier d'historique pour l'utilisateur actuel
$historyPath = "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history.txt"
 
# Vérifie si le fichier d'historique existe
if (Test-Path $historyPath) {
    # Copie le contenu du fichier d'historique vers le fichier d'exportation
    Get-Content $historyPath | Out-File $exportPath
 
    Write-Host "L'historique des commandes a été exporté avec succès à '$exportPath'"
} else {
    Write-Host "Le fichier d'historique n'a pas été trouvé à '$historyPath'"
}

Linux

Export_History_Shell.ps1
## A faire en premier :
#root@SRV-DEB11:~# cp .bash_history /home/administrateur/root_bash_history
#root@SRV-DEB11:~# chown administrateur:administrateur /home/administrateur/root_bash_history
 
$IP = "172.22.15.11"
$User = "administrateur"
$HistoryFileToExport = "/home/administrateur/root_bash_history"
$ExportHistoryFile = "C:\Users\vincent1890\OneDrive - ENI Ecole Informatique\Bureau\bash_history.txt"
 
ssh $User@$IP "cat " + "$HistoryFileToExport" | Out-File -FilePath $ExportHistoryFile
 
 
# Configuration initiale
$IP = "172.22.15.11"
$User = "administrateur"
$HistoryFileToExport = "/home/administrateur/root_bash_history"
 
# Récupère le chemin du répertoire du script
$scriptDirectory = $PSScriptRoot
 
# Emplacement local pour les exports
$Directory = "History-Logs"
 
# Crée le répertoire 'History-Logs' s'il n'existe pas
$fullDirectoryPath = Join-Path $scriptDirectory $Directory
if (-not (Test-Path $fullDirectoryPath)) {
    New-Item -ItemType Directory -Path $fullDirectoryPath
}
 
# Formatage du nom du fichier d'exportation
$MachineName = $IP.Replace(".", "_")
 
# Nom de fichier avec le nom de la machine, la date et l'heure
$dateTime = (Get-Date).ToString('yyyyMMdd-HHmm')
$exportFileName = "${MachineName}_Full_History_bash_export_$dateTime.txt"
$exportPath = Join-Path $fullDirectoryPath $exportFileName
 
 
# Construction de la commande SSH
$SshCommand = "cat `"$HistoryFileToExport`""
 
# Exécution de la commande SSH et exportation de l'historique
ssh $User@$IP "cat " + "$HistoryFileToExport" | Out-File -FilePath $exportPath
 
# Confirmation
Write-Host "L'historique des commandes Bash a été exporté avec succès à '$ExportHistoryFileFullPath'"