this is an extraction of my json File:
[
{
"UID": "Name of UID OU",
"Production": "DistinguishedName of Production OU",
"ProductionFolders": [
"Name of Production Folder 1",
"Name of Production Folder 2"
],
"ProductionFoldersDN": [
"DistinguishedName of Production Folder 1 ",
"DistinguishedName of Production Folder 2"
],
"ProductionSubfolders": [
"Name of ProductionSubfolder 1 ",
"Name of ProductionSubfolder 2"
],
"ProductionSubfoldersDN": [
"Distinguished Name of ProductionSubfolder 1",
"DistinguishedName of Prodcution Subfolder 2"
]
}
]
If there is only the "Production" entry in the json file and no "ProductionFolders" or "ProductionSubfolders" entry: add "Test=UID" to the AD-Attribute "description" of the Active Directory OU (if it does not already exists)
If there are also the "ProductionFolders" entries in the json file, but no "ProductionSubfolders" entries: only add "Test= name of the Production Folder" to the AD-Attribute "description" of every Production Folder OU (if it does not already exist)
If there are "ProductionSubfolders" entries in the json file: only add "Test=name of the Production Subfolder" to the AD-Attribute "description" of every Production Subfolder OU (if it does not already exist)
My approach:
$json = Get-Content "C:\prod.json" |ConvertFrom-JSON
$onlyProd = $item.UID
$Folders = $item.ProdcutionFolders
$Subfolders = $item.ProductionSubfolders
foreach($item in $json) {
if ($item.ProductionFolders -eq $null -and $item.ProductionSubfolders -eq $null) { ###only Production entry exists
$Prod = Get-ADOrganizationalUnit $item.Production -Properties description|Select-Object description
if ($Prod.description -notlike "*Test*") {Set-ADObject -Identity $item.Production -Add @{description="Test=$onlyProd"}}
}
elseif ($item.ProductionSubfolders -ne $null) { ###Production Subfolders entries exist
foreach (Entry in $item.ProductionSubfolders ???){
$ProdSubFolders = Get-ADOrganizationalUnit $item.ProductionSubfoldersDN -Properties description|Select-Object description
if ($ProdSubFolders.description -notlike "*Test*") {Set-ADObject -Identity $item.ProductionSubfoldersDN -Add @{description="Test=$Subfolders"}}}
}
elseif ($item.ProductionFolders -ne $null -and $item.ProductionSubfolders -eq $null) { ###only ProductionFolders entries exists
foreach (Entry in $item.ProductionFolders ????) {
$ProdFolders = Get-ADOrganizationalUnit $item.ProductionFoldersDN -Properties description|Select-Object description
if ($ProdFolders.description -notlike "*Test*") {Set-ADObject -Identity $item.ProductionFoldersDN -Add @{description="Test=$Folders"}}}
}
}
Can you please help me?
THANKS!!!
CodePudding user response:
if you want to test if the key "Production" exists, you could do that:
foreach ($item in $json)
{
if (Get-Member -inputobject $item -name "Production" -Membertype Properties)
{
"key production exists";
$item.Production;
}
else
{
"key production doesnt exist";
}
if (Get-Member -inputobject $item -name 'ProductionSubfolders' -Membertype Properties)
{
foreach ($item1 in $item.ProductionSubfolders)
{
$item1;
}
}
}
you could use this syntax for all keys you are searching...
if you want to iterate over list:
if (Get-Member -inputobject $item -name 'ProductionSubfolders' -Membertype Properties)
{
foreach ($item1 in $item.ProductionSubfolders)
{
$item1;
}
# if you want to use for loop with index for example
For ($i = 0; $i -lt $item.ProductionSubfolders.Count; $i )
{
$item.ProductionSubfolders[$i];
$item.ProductionFoldersDN[$i];
}
}