Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Définition des en-têtes
  2. $Header = 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'
  3.  
  4. #Choix du dernier Fichier File_F1 dans le dossier d'archives
  5. $SourcePath = ".\archives"
  6. $Filter = '*.txt'
  7.  
  8. $Dernierearchive = Get-ChildItem -Path $SourcePath -File -Filter $Filter |
  9. Sort-Object LastWriteTime |
  10. Select-Object -Last 1
  11.  
  12. $Dernierearchive
  13.  
  14. # Import des fichiers
  15. $F1 = Import-Csv -Path ".\archives\$Dernierearchive" -Encoding Default -Header $Header -Delimiter ";"
  16. $F2 = Import-Csv -Path "GRH_TIL_F1.txt" -Encoding Default -Header $Header -Delimiter ";"
  17.  
  18. $ListeAlias = Get-Content -Path ".\liste-fonction_comparaison\listealias.txt" -Encoding Default
  19.  
  20. # Variable qui servira à stocker le résultat
  21. $result = @()
  22.  
  23. # Récupère les différences d'ID (ajout, suppression)
  24. # Et stock dans $F1 et $F2 le statut SideIndicator pour les objects découverts, via le paramètre PassThru
  25. $result = (Compare-Object -ReferenceObject $F1 -DifferenceObject $F2 -Property A -PassThru) | Select-Object A, B, C, D, E, F, G, H, I, J, @{
  26.     Label = "Code d'action"; Expression = {
  27.         if ($_.SideIndicator -eq "=>") { 0 }
  28.         else { 2 }
  29.     }
  30. }
  31.  
  32. # Récupère les modifications pour les doublons d'ID en comparant toutes les propriétés (A, B, C, D, E)
  33. $result += (Compare-Object -ReferenceObject @($F1 | Where-Object { $_.SideIndicator -eq $null }) -DifferenceObject @($F2 | Where-Object { $_.SideIndicator -eq $null }) -Property A, B, C, D, E, F, G, H, I, J -ErrorAction SilentlyContinue) | Where-Object { $_.SideIndicator -eq "=>" } | Select-Object A, B, C, D, E, F, G, H, I, J, @{ Label = "Code d'action"; Expression = { 1 } }
  34.  
  35. # Identifie le travail métier et trie les résultats
  36. $result = $result | Select-Object *, @{
  37.     Label = "IsAlias"; Expression = {
  38.         if ($ListeAlias -contains $_.I) { $true }
  39.         else { $false }
  40.     }
  41. } | Sort-Object -Property "Code d'action" -Descending
  42.  
  43. # Export
  44. $result | Where-Object { $_.IsAlias } | Select-Object A, B, C, D, E, F, G, H, I, J, "Code d'action" | Export-Csv -Path ".\import\File_F1_Import.csv" -NoTypeInformation -Encoding Default
  45. $result | Where-Object { !$_.IsAlias } | Select-Object A, B, C, D, E, F, G, H, I, J, "Code d'action" | Export-Csv -Path ".\erreur_import\File_F1_Import_Defaut.csv" -NoTypeInformation -Encoding Default
  46.  
  47. #Archive F2
  48. Move-Item -Path "GRH_TIL_F1.txt" -Destination ".\archives\GRH_TIL_F1.txt"
  49. $SourceFilePath = ".\archives\GRH_TIL_F1.txt" #Chemin complet du fichier
  50. $DateNow = Get-Date -Format "ddMMyyyyhhmmss" #Date 20140924
  51. $FileName = [io.path]::GetFileNameWithoutExtension($SourceFilePath) #Récupère le nom du fichier uniquement
  52. $FileExtension = [io.path]::GetExtension($SourceFilePath) #Récupère l'extension du fichier uniquement
  53. Rename-Item -Path $SourceFilePath -newname "$FileName _ $DateNow$FileExtension" #Renommage du fichier
  54.  
  55.  
  56. # Create some var
  57. $Date = Get-Date -format "yyyy-MM-dd"
  58. $ArchiveName = "Archive-$date.zip"
  59. $ArchivePath = ".\archives\"
  60. $Source1Path = ".\archives\"
  61.  
  62. # Identify the file in the SourcePath
  63.  
  64. $Files = Get-ChildItem -Path $Source1Path -File -Filter ("*.txt") | Sort-Object LastWriteTime | Select-Object -First 1 # I'm use filter just in case of you have some other file in the folder
  65.  
  66. # Now treatment
  67. foreach ($file in $Files)
  68. {
  69.    
  70.     Compress-Archive -Path $($file.fullName) -DestinationPath $ArchivePath\$ArchiveName -CompressionLevel Optimal -Update
  71.    
  72.     # this only copy the file in the archive and create the archive file if necessary
  73.    
  74.     # the -update parameter is necessary. At the first loop this create the archive file and add file in it, and at the others, this add file and not overwrite the archive file
  75.    
  76.     # Now it's time to delete the source file
  77.    
  78.     Remove-Item -Path $($file.fullName)
  79.    
  80. }
  81.  
  82. # #Création Sémaphore de fin de traitement
  83. $semaphore = new-item "E:\Interfaces_prototype\SEMAPHORES\fin_traitement_F1.ok"type file -force
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement