Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
| backuppersonnalfolder [2019/11/28 14:43] – [Script principal] nekan | backuppersonnalfolder [2021/03/05 16:12] (Version actuelle) – nekan | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| - | ~~CLOSETOC~~ | ||
| ====== Backup-PersonnalFolder - Sauvegarde de dossiers utilisateurs ====== | ====== Backup-PersonnalFolder - Sauvegarde de dossiers utilisateurs ====== | ||
| + | <label type=" | ||
| Il y a des utilisateurs qui prennent l' | Il y a des utilisateurs qui prennent l' | ||
| Ligne 16: | Ligne 16: | ||
| Les informations sont affichés en temps réel dans la console avec une coloration syntaxique, information en <color green> | Les informations sont affichés en temps réel dans la console avec une coloration syntaxique, information en <color green> | ||
| Il inclut aussi la possibilité de création d'un rapport succin qui pourra être traité par un autre script renvoyant les informations dans Nagios. | Il inclut aussi la possibilité de création d'un rapport succin qui pourra être traité par un autre script renvoyant les informations dans Nagios. | ||
| + | |||
| + | ===== Prérequis ===== | ||
| + | |||
| + | Pour fonctionner, | ||
| + | |||
| + | Voir : [[powershell_password|Sécurisation d'un mot de passe]] | ||
| ===== Script principal ===== | ===== Script principal ===== | ||
| - | <code powershell> | + | <sxh powershell> |
| <# | <# | ||
| .SYNOPSIS | .SYNOPSIS | ||
| Ligne 124: | Ligne 130: | ||
| Param ( | Param ( | ||
| - | [String[]]$ComputerName = 'All', | + | [String[]]$ComputerName = "All", |
| [Switch]$Verbose, | [Switch]$Verbose, | ||
| [Switch]$Nagios | [Switch]$Nagios | ||
| Ligne 145: | Ligne 151: | ||
| $BackupLog = " | $BackupLog = " | ||
| # Initialisation du chemin de profil utilisateur | # Initialisation du chemin de profil utilisateur | ||
| - | $UserProfilePath = "" | + | $UserProfilePath = '' |
| # Chemin pour la racine des destinations de sauvegardes | # Chemin pour la racine des destinations de sauvegardes | ||
| $Partage = " | $Partage = " | ||
| Ligne 204: | Ligne 210: | ||
| #region Langue | #region Langue | ||
| - | ##### En cas de traductions dans une autres langues, d' | + | ##### En cas de traductions dans une autre langue, d' |
| $BackupSuccessful = " | $BackupSuccessful = " | ||
| Ligne 224: | Ligne 230: | ||
| $WarningMessage = "Des avertissements sont signalés. Merci de vérifier le fichier Backup.log dans le dossier de sauvegarde utilisateur." | $WarningMessage = "Des avertissements sont signalés. Merci de vérifier le fichier Backup.log dans le dossier de sauvegarde utilisateur." | ||
| - | ##### En cas de traductions dans une autres langues, d' | + | ##### En cas de traductions dans une autre langue, d' |
| #endregion Langue | #endregion Langue | ||
| Ligne 257: | Ligne 263: | ||
| { | { | ||
| # Sinon, on traite le message | # Sinon, on traite le message | ||
| - | $CheckTime = Get-Date -Format | + | $CheckTime = Get-Date -Format G |
| If ($Verbose) | If ($Verbose) | ||
| { | { | ||
| Ligne 307: | Ligne 313: | ||
| # Sinon, on récupère le login de l' | # Sinon, on récupère le login de l' | ||
| $Pattern = $Domaine + ' | $Pattern = $Domaine + ' | ||
| - | $Name = $Name.Replace(" | + | $Name = $Name.Replace(" |
| Add-Log -Type " | Add-Log -Type " | ||
| } | } | ||
| Ligne 369: | Ligne 375: | ||
| # Récupération du chemin du profil utilisateur et formatage du chemin pour accès réseau | # Récupération du chemin du profil utilisateur et formatage du chemin pour accès réseau | ||
| $UserProfilePath = Get-UserLocalPath -PC $PC -User $User | $UserProfilePath = Get-UserLocalPath -PC $PC -User $User | ||
| - | $UserProfilePath = $UserProfilePath.Replace(':', ': | + | $UserProfilePath = $UserProfilePath.Replace(":", ": |
| # Définitions des dossiers pour la copie | # Définitions des dossiers pour la copie | ||
| Ligne 422: | Ligne 428: | ||
| $ID = Get-WmiObject Win32_ShadowCopy | Where-Object { $_.ID -eq $ShadowCopy.ShadowID } | $ID = Get-WmiObject Win32_ShadowCopy | Where-Object { $_.ID -eq $ShadowCopy.ShadowID } | ||
| # Formatage du chemin du ShadowCopy | # Formatage du chemin du ShadowCopy | ||
| - | $CorrectPath = $ID.DeviceObject + '\' | + | $CorrectPath = $ID.DeviceObject + "\" |
| + | # Création du type d'objet [Custom.SymLink] pour le lien symbolique | ||
| + | Try | ||
| + | { | ||
| + | $Null = [Win32.SymLink] | ||
| + | } | ||
| + | Catch | ||
| + | { | ||
| + | Add-Type @" | ||
| + | using System; | ||
| + | using System.Runtime.InteropServices; | ||
| + | |||
| + | namespace Win32 | ||
| + | { | ||
| + | public class SymLink | ||
| + | { | ||
| + | [DllImport(" | ||
| + | public static extern bool CreateSymbolicLink(string lpSymlinkFileName, | ||
| + | } | ||
| + | } | ||
| + | "@ | ||
| + | } | ||
| + | | ||
| # Création du lien symbolique | # Création du lien symbolique | ||
| - | cmd /c mklink /d C: | + | [Win32.SymLink]:: |
| - | + | ||
| # Identifiants pour l' | # Identifiants pour l' | ||
| $Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $Pass | $Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $Pass | ||
| Ligne 449: | Ligne 477: | ||
| $FileToBackup | ForEach-Object { | $FileToBackup | ForEach-Object { | ||
| $FileFullName = $_.FullName | $FileFullName = $_.FullName | ||
| - | $PrintName = $FileFullName.Replace('ShadowCopy\', "") | + | $PrintName = $FileFullName.Replace("ShadowCopy\" |
| $TestFile = ($_.FullName).Replace($ScriptSource, | $TestFile = ($_.FullName).Replace($ScriptSource, | ||
| $TestFileType = (Get-Item -Path $TestFile).GetType().Name | $TestFileType = (Get-Item -Path $TestFile).GetType().Name | ||
| - | If ((Test-Path $TestFile) -and ($TestFileType -ne 'DirectoryInfo')) | + | If ((Test-Path $TestFile) -and ($TestFileType -ne "DirectoryInfo")) |
| { | { | ||
| $SourceFileDate = Get-Date -Date (Get-ItemProperty -Path $FileFullName).LastWriteTime | $SourceFileDate = Get-Date -Date (Get-ItemProperty -Path $FileFullName).LastWriteTime | ||
| Ligne 462: | Ligne 490: | ||
| { | { | ||
| Copy-Item -Path $FileFullName -Destination $TestFile | Copy-Item -Path $FileFullName -Destination $TestFile | ||
| - | $CheckDate = Get-Date -Format | + | $CheckDate = Get-Date -Format G |
| Add-Content -Path " | Add-Content -Path " | ||
| } | } | ||
| Catch | Catch | ||
| { | { | ||
| - | $CheckDate = Get-Date -Format | + | $CheckDate = Get-Date -Format G |
| Add-Content -Path " | Add-Content -Path " | ||
| } | } | ||
| Ligne 475: | Ligne 503: | ||
| { | { | ||
| $RecentFile = $TestFile.Replace(" | $RecentFile = $TestFile.Replace(" | ||
| - | $CheckDate = Get-Date -Format | + | $CheckDate = Get-Date -Format G |
| Add-Content -Path " | Add-Content -Path " | ||
| } | } | ||
| Ligne 481: | Ligne 509: | ||
| If ($SourceFileDate -eq $DestinationFileDate) | If ($SourceFileDate -eq $DestinationFileDate) | ||
| { | { | ||
| - | $CheckDate = Get-Date -Format | + | $CheckDate = Get-Date -Format G |
| Add-Content -Path " | Add-Content -Path " | ||
| } | } | ||
| Ligne 496: | Ligne 524: | ||
| { | { | ||
| Remove-Item -Path $FileFullName -Force -Recurse 2> $Null | Remove-Item -Path $FileFullName -Force -Recurse 2> $Null | ||
| - | $CheckDate = Get-Date -Format | + | $CheckDate = Get-Date -Format G |
| Add-Content -Path " | Add-Content -Path " | ||
| } | } | ||
| Catch | Catch | ||
| { | { | ||
| - | $CheckDate = Get-Date -Format | + | $CheckDate = Get-Date -Format G |
| Add-Content -Path " | Add-Content -Path " | ||
| } | } | ||
| Ligne 511: | Ligne 539: | ||
| $FileFullName = $_.FullName | $FileFullName = $_.FullName | ||
| $TestFile = ($_.FullName).Replace($ScriptSource, | $TestFile = ($_.FullName).Replace($ScriptSource, | ||
| - | $PrintName = $FileFullName.Replace('ShadowCopy\', "") | + | $PrintName = $FileFullName.Replace("ShadowCopy\" |
| If (-not (Test-Path $TestFile)) | If (-not (Test-Path $TestFile)) | ||
| { | { | ||
| Ligne 517: | Ligne 545: | ||
| { | { | ||
| Copy-Item -Path $FileFullName -Destination $TestFile | Copy-Item -Path $FileFullName -Destination $TestFile | ||
| - | $CheckDate = Get-Date -Format | + | $CheckDate = Get-Date -Format G |
| Add-Content -Path " | Add-Content -Path " | ||
| } | } | ||
| Catch | Catch | ||
| { | { | ||
| - | $CheckDate = Get-Date -Format | + | $CheckDate = Get-Date -Format G |
| Add-Content -Path " | Add-Content -Path " | ||
| } | } | ||
| Ligne 532: | Ligne 560: | ||
| # Suppression du lien de shadowcopy | # Suppression du lien de shadowcopy | ||
| - | cmd /c rmdir C: | + | (Get-Item -Path C: |
| } | } | ||
| Ligne 699: | Ligne 727: | ||
| Add-Log -Type " | Add-Log -Type " | ||
| } | } | ||
| - | </code> | + | </sxh> |
| ===== Script d' | ===== Script d' | ||
| - | <code:ps1> | + | <sxh powershell> |
| - | ############################################################################## | + | ####################################################################################### |
| - | # # | + | # |
| - | # Vérification du fichier de réponse (SaveReport.dat) de DailySave.ps1 | + | # Vérification du fichier de réponse (SaveReport.dat) de Backup-PersonnalFolder |
| - | # # | + | # |
| - | # Par Nicolas THOREZ | + | # Par Nicolas THOREZ |
| - | # # | + | # |
| - | ############################################################################## | + | ####################################################################################### |
| - | $Report = Import-Csv -Path ' | + | $Report = Import-Csv -Path ' |
| [int]$Total = $Report.Total | [int]$Total = $Report.Total | ||
| Ligne 720: | Ligne 748: | ||
| [int]$Critical = $Report.Critical | [int]$Critical = $Report.Critical | ||
| - | $Output = "" | + | $Output = '' |
| $ExitCode = 3 | $ExitCode = 3 | ||
| Ligne 740: | Ligne 768: | ||
| Else | Else | ||
| { | { | ||
| - | | + | |
| exit $ExitCode | exit $ExitCode | ||
| } | } | ||
| - | echo " | + | Write-Host |
| exit $ExitCode | exit $ExitCode | ||
| - | </code> | + | </sxh> |
| - | + | ||
| - | --- // | + | |
| ~~DISCUSSION~~ | ~~DISCUSSION~~ | ||