'Powershell - update attributes in Active Directory based on JSON file
This is an extraction of my json file:
[
{
"ProductionSubfolders": "XYZ",
"ProductionSubfoldersDN": "OU=XYZ,DC=bla,DC=root,DC=local"
}
]
ProductionSubfolders = the values are the name of an Organizational Unit in Active Directory
ProductionSubfoldersDN = the values are the assocciated DistinguishedName of the Organizational Unit
I want to add an entry for the Active Directory attribut called "description" of the Organizational Units that are listed in the json file - in this case "ABC" and "XYZ".
The new value of the attribut "description" in Active Directory should be "Test + the name of the OU", so in this case : "Test-ABC" and "Test-XYZ".
This is what I have:
$json = Get-Content "C:\prod.json" |ConvertFrom-JSON
foreach ($item in $json) {
foreach {$item1 in $item.ProductionSubfoldersDN} {
$name = $item.ProductionSubfolder
$p= Get-ADOrganizationalUnit -Identity $item1 -Properties description| Select description
if ($p.description -notlike "*Test*") {Set-ADObject -Identity $item1 -Add
@{description="Test-$name}
}
}
It works. For the OU "ABC" there is a new description attribut value in AD and for the OU "XYZ" is also a new description attribut value in the AD. But the name of the attribut is wrong. It is "Test-ABCXYZ" for both OUs, instead of "Test-ABC" for the OU "ABC" and "Test-XYZ" for the OU "XYZ".
I know that this is because $item.ProductionSubfolder contains both names: "ABC" and "XYZ". But I do not know how to change it, how to separate the names and match them correctly to the associated ProductionSubfolderDN.
THANKS IN ADVANCE!!
Solution 1:[1]
a solution to link the value of subfolder to subfolderDN:
foreach ($item in $json)
{
foreach ($name in $item.ProductionSubfolders)
{
foreach($ou in $item.ProductionSubfoldersDN)
{
if ($ou -like 'OU=' + $name + '*')
{
$n = 'Test-{0}' -f $name
$name + ' is inside ' + $ou;
$ou;
$n;
#Set-ADOrganizationalUnit $ou -Description $n
}
}
}
}
ABC is inside OU=ABC,DC=bla,DC=root,DC=local
OU=ABC,DC=bla,DC=root,DC=local
Test-ABC
XYZ is inside OU=XYZ,DC=bla,DC=root,DC=local
OU=XYZ,DC=bla,DC=root,DC=local
Test-XYZ
i test if the OU contains the name of OU from subfolders using like which is not case sensitive (use clikefor case sensitive)
this solution is functional whatever the order of OU..
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
