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”条目:
仅将“测试=生产文件夹的名称”添加到每个生产文件夹 OU 的 AD 属性“描述”(如果它尚不存在)
如果 json 文件中有“ProductionSubfolders”条目:
仅将“测试=生产子文件夹的名称”添加到每个生产子文件夹 OU 的 AD 属性“描述”(如果它尚不存在)
我的做法:
$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];
}
}
这是我的 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”条目: 仅将“测试=生产文件夹的名称”添加到每个生产文件夹 OU 的 AD 属性“描述”(如果它尚不存在)
如果 json 文件中有“ProductionSubfolders”条目: 仅将“测试=生产子文件夹的名称”添加到每个生产子文件夹 OU 的 AD 属性“描述”(如果它尚不存在)
我的做法:
$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];
}
}