update
This commit is contained in:
38
Système de fichiers/Compare-Csv.ps1
Normal file
38
Système de fichiers/Compare-Csv.ps1
Normal file
@ -0,0 +1,38 @@
|
||||
param (
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
$Csv1,
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
$Csv2,
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
$Property
|
||||
)
|
||||
|
||||
#Importer les 2 fichiers CSV
|
||||
$File1 = Import-Csv -Path $Csv1 -Delimiter ";" -Encoding utf8
|
||||
$File2 = Import-Csv -Path $Csv2 -Delimiter ";" -Encoding utf8
|
||||
|
||||
#Comparer les 2 objets importés
|
||||
$Results = Compare-Object $File1 $File2 -Property $Property -IncludeEqual
|
||||
|
||||
#Parcourir le tableau pour trouver les valeurs identiques
|
||||
$Array = @()
|
||||
Foreach ($R in $Results) {
|
||||
If ( $R.sideindicator -eq "==" ) {
|
||||
$Object = [pscustomobject][ordered] @{
|
||||
|
||||
"Valeurs identiques" = $R.$($Property)
|
||||
|
||||
}
|
||||
$Array += $Object
|
||||
}
|
||||
}
|
||||
|
||||
#Retourner le résultat
|
||||
$Array
|
||||
|
||||
<#
|
||||
.\Compare-Csv.ps1 -csv1 ".\templates\Compare-Csv-1.csv" -csv2 ".\Templates\Compare-Csv-2.csv" -property "Nom"
|
||||
#>
|
13
Système de fichiers/Connect-PsDrive.ps1
Normal file
13
Système de fichiers/Connect-PsDrive.ps1
Normal file
@ -0,0 +1,13 @@
|
||||
#Commande basique
|
||||
net use L: UNC_DU_PARTAGE /PERSISTENT:NO /user:USER PASSWORD
|
||||
|
||||
##Créer un lecteur avec PowerShell
|
||||
|
||||
##Créer un objet Credential avec nom d'utilisateur et mot de passe
|
||||
$Credential = New-Object System.Management.Automation.PsCredential("USER", (ConvertTo-SecureString -String "PASSWORD" -AsPlainText -Force))
|
||||
|
||||
##Créer le lecteur PowerShell non persistent
|
||||
New-PSDrive -Name "NOM_DU_LECTEUR" -Root "UNC_DU_PARTAGE" -PSProvider "FileSystem" -Credential $Credential
|
||||
|
||||
##Créer le lecteur PowerShell persistent
|
||||
New-PSDrive -Name "NOM_DU_LECTEUR" -Root "UNC_DU_PARTAGE" -PSProvider "FileSystem" -Credential $Credential -Persist
|
27
Système de fichiers/Copy-Files.ps1
Normal file
27
Système de fichiers/Copy-Files.ps1
Normal file
@ -0,0 +1,27 @@
|
||||
#Copier un fichier
|
||||
Copy-Item "SOURCE" -Destination "DESTINATION"
|
||||
|
||||
#Télécharger un fichier depuis le Web
|
||||
Invoke-WebRequest 'https://aka.ms/WACDownload' -OutFile "DESTINATION.msi"
|
||||
|
||||
#Copier des fichiers et dossiers avec BITS
|
||||
Start-BitsTransfer -Source "SOURCE\*" -Destination "DESTINATION"
|
||||
|
||||
#Copier des fichiers et dossiers avec Robocopy et PowerShell en mode miroir
|
||||
|
||||
##La commande de base
|
||||
robocopy "SOURCE" "DEST" /MIR /NDL /NP /FFT /Z /R:3 /W:10 /LOG+:C:\Lab\Log.txt
|
||||
|
||||
##Importer le CSV qui contient les sources et destinations
|
||||
$Files = Import-Csv "Copy-Files.csv" -Encoding "UTF8" -Delimiter ";"
|
||||
|
||||
##Définir le dossier de logs
|
||||
$RootLogs = "C:\Lab"
|
||||
|
||||
##Parcourir les lignes du CSV et créer des tâches Robocopy
|
||||
foreach ($Item in $Files) {
|
||||
$Logs = $RootLogs + "\Logs_" + (Get-Date -UFormat "%d-%m-%Y") + ".log"
|
||||
$RobocopyParams = "/MIR /NDL /NP /FFT /Z /R:1 /W:5 /LOG+:$Logs"
|
||||
New-Item -Path $Item.Destination -ItemType Directory -Force
|
||||
Start-Process "robocopy.exe" -Argumentlist `"$($Item.Source)`", `"$($Item.Destination)`", $RobocopyParams -Wait
|
||||
}
|
112
Système de fichiers/New-SharedFolders.ps1
Normal file
112
Système de fichiers/New-SharedFolders.ps1
Normal file
@ -0,0 +1,112 @@
|
||||
Import-Module activedirectory
|
||||
|
||||
$Partages = Import-csv "Templates\New-SharedFolders.csv" -Delimiter ";" -Encoding UTF8
|
||||
$BaseDir = "D:\Partages\"
|
||||
|
||||
$searchbase = Get-ADDomain | ForEach-Object { $_.DistinguishedName }
|
||||
$netbios = Get-ADDomain | ForEach-Object { $_.NetBIOSName }
|
||||
|
||||
ForEach ($item In $Partages) {
|
||||
|
||||
if ($item.AccessType -eq "Write") {
|
||||
$Rights = "Modify, Synchronize"
|
||||
$Inheritance = "ContainerInherit, ObjectInherit"
|
||||
$Propagation = "None"
|
||||
$AccessControlType = "Allow"
|
||||
}
|
||||
elseif ($item.AccessType -eq "Read") {
|
||||
$Rights = "ReadAndExecute"
|
||||
$Inheritance = "ContainerInherit, ObjectInherit"
|
||||
$Propagation = "None"
|
||||
$AccessControlType = "Allow"
|
||||
}
|
||||
elseif ($item.AccessType -eq "Access") {
|
||||
$Rights = "ReadAndExecute"
|
||||
$Inheritance = "None"
|
||||
$Propagation = "None"
|
||||
$AccessControlType = "Allow"
|
||||
}
|
||||
else {
|
||||
Write-Host "AccessType is empty"
|
||||
Return
|
||||
}
|
||||
|
||||
$Shared_Folder = Join-Path $BaseDir $item.Name
|
||||
|
||||
try {
|
||||
if (Test-Path $Shared_Folder) {
|
||||
Write-Host "Folder $($Shared_Folder) alread exists! Folder creation skipped!"
|
||||
}
|
||||
else {
|
||||
New-Item -ItemType Directory -Path $Shared_Folder
|
||||
Write-Host "Folder $($Shared_Folder) created!"
|
||||
}
|
||||
}
|
||||
catch {
|
||||
Write-Host "Error, Folder $($Shared_Folder) not created!"
|
||||
}
|
||||
|
||||
if (($item.IsShared -eq $true) -and (!(Get-SmbShare -Name $item.Name -ErrorAction SilentlyContinue))) {
|
||||
try {
|
||||
New-SmbShare -Name $item.Name -Path $Shared_Folder -FullAccess "Tout le monde"
|
||||
Set-SmbShare -Name $item.Name -FolderEnumerationMode AccessBased -Force
|
||||
Write-Host "$($Shared_Folder) is shared now!"
|
||||
}
|
||||
catch {
|
||||
Write-Host "Error, $($Shared_Folder) not shared!"
|
||||
}
|
||||
}
|
||||
else {
|
||||
Write-Host "Folder $($Shared_Folder) is already shared or IsShared is not set to true!"
|
||||
}
|
||||
|
||||
$check = [ADSI]::Exists("LDAP://$($item.GroupLocation),$($searchbase)")
|
||||
|
||||
$Group = (($item.name -replace " ", "-" -replace "\\", "_" -replace ",", "-") + "_" + $item.AccessType)
|
||||
|
||||
If ($check -eq $True) {
|
||||
Try {
|
||||
$TheGroup = Get-ADGroup $Group
|
||||
Write-Host "Group $($Group) alread exists! Group creation skipped! SID: $($TheGroup.SID)"
|
||||
}
|
||||
Catch {
|
||||
|
||||
$TheGroup = New-ADGroup -Name $Group -Path ($($item.GroupLocation) + "," + $($searchbase)) -GroupCategory Security -GroupScope $item.GroupType -PassThru -Verbose
|
||||
Write-Host "Group $($Group) created! SID: $($TheGroup.SID)"
|
||||
}
|
||||
|
||||
try {
|
||||
$acl = Get-Acl $Shared_Folder
|
||||
|
||||
if ($acl.Access.IdentityReference -notcontains ($($netbios) + "\" + $Group)) {
|
||||
|
||||
$acl.SetAccessRuleProtection($true, $true)
|
||||
|
||||
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule ($TheGroup.SID, $Rights, $Inheritance, $Propagation, $AccessControlType)
|
||||
$acl.AddAccessRule($AccessRule)
|
||||
|
||||
Set-Acl -Path $Shared_Folder -AclObject $acl -ea Stop
|
||||
|
||||
Write-Host "ACL for $($Shared_Folder) created!"
|
||||
}
|
||||
else {
|
||||
Write-Host "ACL for $($Shared_Folder) alread exists! Folder ACL skipped!"
|
||||
}
|
||||
$acl = Get-Acl $Shared_Folder
|
||||
$objUser = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-32-545")
|
||||
$objACE = New-Object System.Security.AccessControl.FileSystemAccessRule($objUser, "FullControl", "None", "None", "Allow")
|
||||
$acl.RemoveAccessRuleAll($objACE)
|
||||
|
||||
Set-Acl -Path $Shared_Folder -AclObject $acl -ea Stop
|
||||
|
||||
}
|
||||
catch {
|
||||
Write-Host "Error, ACL for folder $($Shared_Folder) not modified!"
|
||||
}
|
||||
|
||||
}
|
||||
Else {
|
||||
Write-Host "Target OU can't be found! Group creation skipped!"
|
||||
}
|
||||
|
||||
}
|
11
Système de fichiers/README.md
Normal file
11
Système de fichiers/README.md
Normal file
@ -0,0 +1,11 @@
|
||||
# Système de fichiers
|
||||
|
||||
- Mapper un lecteur réseau en Batch vs PowerShell.
|
||||
|
||||
<a href="https://www.youtube.com/watch?v=jWGikiYg97s" target="_blank"><img src="http://img.youtube.com/vi/jWGikiYg97s/0.jpg"
|
||||
alt="Comment mapper un lecteur réseau en PowerShell" width="240" height="180" border="10" /></a>
|
||||
|
||||
- Copier des fichiers et dossiers avec Robocopy et PowerShell
|
||||
|
||||
<a href="https://www.youtube.com/watch?v=YTwPF8F7NFY" target="_blank"><img src="http://img.youtube.com/vi/YTwPF8F7NFY/0.jpg"
|
||||
alt="Copier des fichiers avec Robocopy et PowerShell" width="240" height="180" border="10" /></a>
|
5
Système de fichiers/Templates/Compare-Csv-1.csv
Normal file
5
Système de fichiers/Templates/Compare-Csv-1.csv
Normal file
@ -0,0 +1,5 @@
|
||||
Prénom;Nom
|
||||
David;Dupont
|
||||
Bertrand;Torte
|
||||
Ugo;Lotti
|
||||
Sébastien;Concas
|
|
6
Système de fichiers/Templates/Compare-Csv-2.csv
Normal file
6
Système de fichiers/Templates/Compare-Csv-2.csv
Normal file
@ -0,0 +1,6 @@
|
||||
Prénom;Nom
|
||||
Cécile;Duprat
|
||||
Bertrand;Tamac
|
||||
Mickael;Mickael
|
||||
Sébastien;Concas
|
||||
Anaîs;Dupont
|
|
3
Système de fichiers/Templates/Copy-Files.csv
Normal file
3
Système de fichiers/Templates/Copy-Files.csv
Normal file
@ -0,0 +1,3 @@
|
||||
Source;Destination
|
||||
\\SERVER\Source1;C:\Partages\Dest1
|
||||
\\SERVER\Source2;C:\Partages\Dest2
|
|
13
Système de fichiers/Templates/New-SharedFolders.csv
Normal file
13
Système de fichiers/Templates/New-SharedFolders.csv
Normal file
@ -0,0 +1,13 @@
|
||||
Name;GroupType;IsShared;AccessType;GroupLocation
|
||||
Commun;Global;true;Read;OU=Groupes,OU=Agen
|
||||
Commerce;Global;true;Read;OU=Groupes,OU=Agen
|
||||
Achats;Global;true;Read;OU=Groupes,OU=Agen
|
||||
Direction;Global;true;Read;OU=Groupes,OU=Agen
|
||||
Marketing;Global;true;Read;OU=Groupes,OU=Agen
|
||||
Technique;Global;true;Read;OU=Groupes,OU=Agen
|
||||
Commun;Global;true;Write;OU=Groupes,OU=Agen
|
||||
Commerce;Global;true;Write;OU=Groupes,OU=Agen
|
||||
Achats;Global;true;Write;OU=Groupes,OU=Agen
|
||||
Direction;Global;true;Write;OU=Groupes,OU=Agen
|
||||
Marketing;Global;true;Write;OU=Groupes,OU=Agen
|
||||
Technique;Global;true;Write;OU=Groupes,OU=Agen
|
|
Reference in New Issue
Block a user