This commit is contained in:
2023-07-04 12:59:44 +02:00
parent 2cef42a718
commit 09c2faad93
231 changed files with 261001 additions and 4 deletions

View 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"
#>

View 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

View 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
}

View 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!"
}
}

View 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>

View File

@ -0,0 +1,5 @@
Prénom;Nom
David;Dupont
Bertrand;Torte
Ugo;Lotti
Sébastien;Concas
1 Prénom Nom
2 David Dupont
3 Bertrand Torte
4 Ugo Lotti
5 Sébastien Concas

View File

@ -0,0 +1,6 @@
Prénom;Nom
Cécile;Duprat
Bertrand;Tamac
Mickael;Mickael
Sébastien;Concas
Anaîs;Dupont
1 Prénom Nom
2 Cécile Duprat
3 Bertrand Tamac
4 Mickael Mickael
5 Sébastien Concas
6 Anaîs Dupont

View File

@ -0,0 +1,3 @@
Source;Destination
\\SERVER\Source1;C:\Partages\Dest1
\\SERVER\Source2;C:\Partages\Dest2
1 Source Destination
2 \\SERVER\Source1 C:\Partages\Dest1
3 \\SERVER\Source2 C:\Partages\Dest2

View 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
1 Name GroupType IsShared AccessType GroupLocation
2 Commun Global true Read OU=Groupes,OU=Agen
3 Commerce Global true Read OU=Groupes,OU=Agen
4 Achats Global true Read OU=Groupes,OU=Agen
5 Direction Global true Read OU=Groupes,OU=Agen
6 Marketing Global true Read OU=Groupes,OU=Agen
7 Technique Global true Read OU=Groupes,OU=Agen
8 Commun Global true Write OU=Groupes,OU=Agen
9 Commerce Global true Write OU=Groupes,OU=Agen
10 Achats Global true Write OU=Groupes,OU=Agen
11 Direction Global true Write OU=Groupes,OU=Agen
12 Marketing Global true Write OU=Groupes,OU=Agen
13 Technique Global true Write OU=Groupes,OU=Agen