PowerShell/Active Directory
Aller à la navigation
Aller à la recherche
Script de mise à jour d'Active Directory
Lire les lignes d'un fichier
Avec le fichier suivant C:\tmp\source.csv :
compte;civilité;nom;prénom;société;service dupondp;M.;Dupond;Pierre;MaSociété;SI martinj;M.;Martin;Jean;MaSociété;Commercial defrattec;Mme;De Fratte;Catherine;MaSociété;Export
Premier script :
$fichier = Import-Csv "C:\tmp\source.csv" -Delimiter ";"
Foreach ($ligne in $fichier) {
Write-Host $ligne
}
Le résultat du premier script est :
@{compte=dupondp; nom=Dupond; prénom=Pierre; société=MaSociété; service=SI}
@{compte=martinj; nom=Martin; prénom=Jean; société=MaSociété; service=Commercial}
@{compte=defrattec; nom=De Fratte; prénom=Catherine; société=MaSociété; service=Export}
Deuxième script :
Import-Csv "C:\tmp\source.csv" -Delimiter ";" | Foreach { Write-Host ("{0} {1} appartient au service {2}" -f $_.civilité, $_.nom, $_.service) }
Le résultat du deuxième script est :
M. Dupond appartient au service SI M. Martin appartient au service Commercial Mme De Fratte appartient au service Export
Mise à jour d'utilisateur AD
site : https://www.thelazyadministrator.com
Import-CSV -Path C:\Test\TestFile.CSV | Foreach-Object {
$ldaphash = @{
"employeeType" = $_.employeeType;
"departmentnumber" = $_.departmentNumber;
"msDS-cloudExtensionAttribute1" = $_."msDS-cloudExtensionAttribute1";
"msDS-cloudExtensionAttribute2" = $_."msDS-cloudExtensionAttribute2";
"msDS-cloudExtensionAttribute3" = $_."msDS-cloudExtensionAttribute3";
"msDS-cloudExtensionAttribute4" = $_."msDS-cloudExtensionAttribute4"
}
#Get all keys that have null values and store in variable keystoremove
$keysToRemoveldap = $ldaphash.keys | Where-Object {
!$ldaphash[$_]
}
#Foreach key, remove them from the hashtable
$keysToRemoveldap | Foreach-Object {
$ldaphash.remove($_)
}
$hashtable = @{
Identity = Get-ADuser -Filter "name -eq '$($_.name)'" -Properties *
SAMAccountName = $_.SAMAccountName;
EmailAddress = $_.EmailAddress;
DisplayName = $_.DisplayName;
GivenName = $_.GivenName;
Surname = $_.Surname;
Office = $_.physicalDeliveryOfficeName;
EmployeeID = $_.EmployeeID;
Title = $_.title;
Company = $_.Company;
Division = $_.Division;
Department = $_.Department;
MobilePhone = $_.mobile;
StreetAddress = $_.streetAddress;
City = $_.City;
State = $_.State;
PostalCode = $_.postalCode;
Country = $_.Country;
OfficePhone = $_.telephoneNumber;
ErrorAction = "Stop";
Replace = $ldaphash
}
$keysToRemove = $hashtable.keys | Where-Object {
!$hashtable[$_]
}
$keysToRemove | Foreach-Object {
$hashtable.remove($_)
}
#If there are no LDAP properties to change, remove it from the main hash table
if ($ldaphash.count -eq 0)
{
$hashtable.remove("Replace")
}
Set-ADUser @hashtable
}
PowerShell and Active Directory Essentials
Vidéo Youtube, Varonis, PowerShell and Active Directory Essentials
- https://www.youtube.com/watch?v=-zDXTLiX_wk
- durée : 5 h 02
Compte local
Contrôle
# contrôle de l'existance d'un compte local
$compte="recupe";if($compte -in ((Get-LocalUser).name)){Write-Host "Le compte $compte existe" -ForegroundColor Green} else {Write-Host "Le compte $compte n'existe pas" -ForegroundColor Yellow}