« PowerShell/Active Directory » : différence entre les versions

Aller à la navigation Aller à la recherche
mAucun résumé des modifications
 
Ligne 36 : Ligne 36 :
Mme De Fratte appartient au service Export
Mme De Fratte appartient au service Export
</pre>
</pre>
== Mise à jour d'utilisateur AD ==
site : https://www.thelazyadministrator.com
* source : https://www.thelazyadministrator.com/2023/03/15/set-aduser-dealing-with-null-values-when-importing-a-csv-working-with-parameters-and-properties-that-dont-accept-empty-strings/#Adding_Codeblock_to_a_Hashtable
<source>
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
}
</source>


== PowerShell and Active Directory Essentials ==
== PowerShell and Active Directory Essentials ==
Vidéo Youtube, Varonis, PowerShell and Active Directory Essentials
Vidéo Youtube, Varonis, PowerShell and Active Directory Essentials
* https://www.youtube.com/watch?v=-zDXTLiX_wk
* https://www.youtube.com/watch?v=-zDXTLiX_wk
* durée : 5 h 02  
* durée : 5 h 02


== Voir aussi ==
== Voir aussi ==
* &nbsp;
* &nbsp;

Dernière version du 4 novembre 2023 à 16:24

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

Voir aussi

  •