如何在 Powershell 中添加计算 属性 的自定义数据
How to add custom data as calculated property in Powershell
我正在尝试创建一个包含 4 个自定义命名列的 csv 文件,其中 3 个填充了来自 Active Directory 的属性,第 4 个填充了自定义数据,因为它在 Active Directory 中不可用。到目前为止,我已经设法从 AD 中获取 3 个属性并将它们存储为计算属性,但我不知道如何使用静态数据添加第 4 行。
我想导入到 cvs 文件的静态数据在另一个文件中,但我不介意在脚本中动态创建它,例如作为数组。
到目前为止,这是我的代码。如您所见,我只搜索职位 属性 的帐户,并提取有关其部门、描述、samaccountname 的信息,并将它们显示为计算属性 GroupID、GroupName 和 ManagerID
Get-AdUser -Filter {(Enabled -eq "True" ) -and (title -like '*')} -Properties title,Description,department |select -Property @{Name='GroupID'; Expression={$_.department}}, @{Name='GroupName'; Expression={$_.Description}},@{Name='ManagerID'; Expression= {$_.samaccountname}}
输出如下所示。
GroupID GroupName ManagerID
------- --------- ---------
Z0 Division blabla (Z0) fakeID1
ZR0 Department blabla (SR) fakeID2
ZR5 Unit bla bla (ZR5) fakeID3
ZK2 Unit bla bla (ZK2) fakeID4
OB2 Unit bla bla (OB2) fakeID5
我不能做的是获取自定义数据“N,NR0,NR0,N,NV0,NV0,NV0,Z0,ZK0,OB0,O”并将其放入名称为“的新列中父组”。 “parentGroup”是关于哪个部门是另一个部门的父级的信息,它不存储在 AD 中。
我希望看到像输出之外的结果。
GroupID GroupName ParentID ManagerID
------- --------- -------- ---------
Z0 Division blabla (Z0) Z fakeID1
ZR0 Department blabla (ZR0) Z fakeID2
ZR5 Unit bla bla (ZR5) ZR0 fakeID3
ZK2 Unit bla bla (ZK2) ZK0 fakeID4
OB2 Unit bla bla (OB2) OB0 fakeID5
如果有人能帮助我,我将不胜感激。
此致!
假设您有一个父子关系列表,您可以将数据加载到哈希表中并使用它从 属性 表达式中查找父 ID:
# load data, this could as well be done from an external csv file
$relationships = @'
parent,child
Z,Z0
Z,ZR0
ZR0,ZR5
ZK0,ZK2
OB2,OB0
'@ |ConvertFrom-Csv
# create and populate hashtable
$parentIDs = @{}
$relationships |ForEach-Object {
$parentIDs[$_.child] = $_.parent
}
然后在你的计算中 属性:
... |Select -Property @{Name='ParentID';Expression={$parentIDs[$_.department]}},...
我正在尝试创建一个包含 4 个自定义命名列的 csv 文件,其中 3 个填充了来自 Active Directory 的属性,第 4 个填充了自定义数据,因为它在 Active Directory 中不可用。到目前为止,我已经设法从 AD 中获取 3 个属性并将它们存储为计算属性,但我不知道如何使用静态数据添加第 4 行。 我想导入到 cvs 文件的静态数据在另一个文件中,但我不介意在脚本中动态创建它,例如作为数组。
到目前为止,这是我的代码。如您所见,我只搜索职位 属性 的帐户,并提取有关其部门、描述、samaccountname 的信息,并将它们显示为计算属性 GroupID、GroupName 和 ManagerID
Get-AdUser -Filter {(Enabled -eq "True" ) -and (title -like '*')} -Properties title,Description,department |select -Property @{Name='GroupID'; Expression={$_.department}}, @{Name='GroupName'; Expression={$_.Description}},@{Name='ManagerID'; Expression= {$_.samaccountname}}
输出如下所示。
GroupID GroupName ManagerID
------- --------- ---------
Z0 Division blabla (Z0) fakeID1
ZR0 Department blabla (SR) fakeID2
ZR5 Unit bla bla (ZR5) fakeID3
ZK2 Unit bla bla (ZK2) fakeID4
OB2 Unit bla bla (OB2) fakeID5
我不能做的是获取自定义数据“N,NR0,NR0,N,NV0,NV0,NV0,Z0,ZK0,OB0,O”并将其放入名称为“的新列中父组”。 “parentGroup”是关于哪个部门是另一个部门的父级的信息,它不存储在 AD 中。 我希望看到像输出之外的结果。
GroupID GroupName ParentID ManagerID
------- --------- -------- ---------
Z0 Division blabla (Z0) Z fakeID1
ZR0 Department blabla (ZR0) Z fakeID2
ZR5 Unit bla bla (ZR5) ZR0 fakeID3
ZK2 Unit bla bla (ZK2) ZK0 fakeID4
OB2 Unit bla bla (OB2) OB0 fakeID5
如果有人能帮助我,我将不胜感激。 此致!
假设您有一个父子关系列表,您可以将数据加载到哈希表中并使用它从 属性 表达式中查找父 ID:
# load data, this could as well be done from an external csv file
$relationships = @'
parent,child
Z,Z0
Z,ZR0
ZR0,ZR5
ZK0,ZK2
OB2,OB0
'@ |ConvertFrom-Csv
# create and populate hashtable
$parentIDs = @{}
$relationships |ForEach-Object {
$parentIDs[$_.child] = $_.parent
}
然后在你的计算中 属性:
... |Select -Property @{Name='ParentID';Expression={$parentIDs[$_.department]}},...