簡體   English   中英

Powershell - json 文件 - Active Directory - 添加屬性

[英]Powershell - json File - Active Directory - add Attribut

這是我的 json 文件的提取:

[
        {
        "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"
                                   ]
        }
]

如果 json 文件中只有“Production”條目而沒有“ProductionFolders”或“ProductionSubfolders”條目:將“Test=UID”添加到 Active Directory OU 的 AD 屬性“描述”(如果它尚不存在) )

如果 json 文件中也有“ProductionFolders”條目,但沒有“ProductionSubfolders”條目:僅將“Test= name of the Production Folder”添加到每個 Production Folder OU 的 AD 屬性“description”(如果沒有已經存在)

如果 json 文件中有“ProductionSubfolders”條目:僅將“Test=name of the Production Subfolder”添加到每個 Production Subfolder OU 的 AD 屬性“description”中(如果它尚不存在)

我的方法:

$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"}}}
    }

}

你能幫我么?

謝謝!!!

如果你想測試鍵“Production”是否存在,你可以這樣做:

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

您可以將此語法用於您正在搜索的所有鍵...

如果你想遍歷列表:

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];
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM