英文:
PsCustomObject - controlling null or not null value
问题
以下是您提供的代码的中文翻译:
我的工作流程如下:
-
如果经理字段不为空,我将把这个值添加到 pscustomobject 中。
-
如果经理字段为空,我将添加 NULL 关键字。
这一行代码没有正确运行。
manager = if ((Get-ADuser -identity $obj.Manager)) {'(Get-ADuser -identity $obj.Manager -properties displayname).DisplayName'} Else {'NULL'}
脚本:
$Objects = Get-ADUser -identity user01 -properties *
foreach ($obj in $Objects) {
$LogonDate = (Get-LastLogon -Identity $obj.SamAccountName).DateTime
$logging += [PSCustomObject]@{
DisplayName = $obj.Name
samaccountname = $obj.samaccountname
mail = $obj.mail
title = $obj.title
department = $obj.department
manager = if ((Get-ADuser -identity $obj.Manager)) {(Get-ADuser -identity $obj.Manager -properties displayname).DisplayName} Else {$null}
physicalDeliveryOfficeName = $obj.physicalDeliveryOfficeName
postalCode = $obj.postalCode
State = $obj.St
description = $obj.description
Country = $obj.c
company =$obj.company
homephone = $obj.homephone
telephoneNumber = $obj.telephoneNumber
mobile = $obj.mobile
Location = $obj.l
LastLogon = $LogonDate
}
}
$logging
错误消息:
Get-ADUser: 无法验证参数 'Identity'。参数为 null。请为参数提供有效值,然后尝试重新运行命令。
在行:18 字符:45
+ manager = if ((Get-ADuser -identity $obj.Manager)) {(Get-ADus ...
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser
如果您有任何其他需要翻译的内容,请告诉我。
英文:
My workflow is :
-
if manager field is not null , I will add this value inside pscustomobject.
-
if manager field is NULL , I will add NULL keyword.
This line is not working correctly.
manager = if ((Get-ADuser -identity $obj.Manager)) {'(Get-ADuser -identity $obj.Manager -properties displayname).DisplayName'} Else {'NULL'}
script :
$Objects = Get-ADUser -identity user01 -properties *
foreach ($obj in $Objects) {
$LogonDate = (Get-LastLogon -Identity $obj.SamAccountName).DateTime
$logging += [PSCustomObject]@{
DisplayName = $obj.Name
samaccountname = $obj.samaccountname
mail = $obj.mail
title = $obj.title
department = $obj.department
manager = if ((Get-ADuser -identity $obj.Manager)) {(Get-ADuser -identity $obj.Manager -properties displayname).DisplayName} Else {$null}
physicalDeliveryOfficeName = $obj.physicalDeliveryOfficeName
postalCode = $obj.postalCode
State = $obj.St
description = $obj.description
Country = $obj.c
company =$obj.company
homephone = $obj.homephone
telephoneNumber = $obj.telephoneNumber
mobile = $obj.mobile
Location = $obj.l
LastLogon = $LogonDate
}
}
$logging
Error Message:
Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again.
At line:18 char:45
+ manager = if ((Get-ADuser -identity $obj.Manager)) {(Get-ADus ...
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser
答案1
得分: 0
在foreach
循环的顶部行中执行以下操作:
$manager = if (![string]::IsNullOrWhiteSpace($obj.Manager)) {
(Get-ADUser -Identity $obj.Manager -Properties DisplayName).DisplayName
} else {'NULL'}
然后在你的 [PsCustomObject]
中设置 manager = $manager
。
或者可以像这样做:
$manager = Get-ADUser -Identity $obj.Manager -Properties DisplayName -ErrorAction SilentlyContinue
然后在你的 [PsCustomObject]
中设置 manager = if ($manager) { $manager.DisplayName } else { 'NULL' }
另外,不要使用 +=
将对象添加到一个数组中(你没有在脚本的顶部声明它)。
而应该使用:
$logging = foreach ($obj in $Objects) { ... }
此外,使用 -Properties *
非常浪费资源,因为这会获取所有用户属性,而你只想获取一个子集。
在这种情况下,我建议在顶部创建一个属性数组:
$props = 'DisplayName', 'EmailAddress', 'Title', 'Department', 'Manager', 'Office', 'PostalCode', 'State', 'Description', 'Country', 'Company', 'HomePhone', 'OfficePhone', 'MobilePhone', 'City'
$Objects = Get-ADUser -Filter * -Properties $props
另外,我在这里使用了PowerShell的别名,但如果你更喜欢LDAP属性名称,也可以使用它们:
PowerShell | LDAP |
---|---|
EmailAddress | |
OfficePhone | telephoneNumber |
Office | physicalDeliveryOfficeName |
MobilePhone | mobile |
City | l |
Country | c |
英文:
As top line inside the foreach loop do
$manager = if (![string]::IsNullOrWhiteSpace($obj.Manager)) {
(Get-ADUser -Identity $obj.Manager -Properties DisplayName).DisplayName
} else {'NULL'}
then in your [PsCustomObject] you set manager = $manager
or do something like
$manager = Get-ADUser -Identity $obj.Manager -Properties DisplayName -ErrorAction SilentlyContinue
then in your [PsCustomObject] you set manager = if ($manager) { $manager.DisplayName } else { 'NULL'}
BTW, you should not use +=
to add objects to an array (which you didn't declare on top of the script..)
Instead use
$logging = foreach ($obj in $Objects) { ... }
Also, using -Properties *
is very wasteful because you then ask for ALL user properties where you only want a subset.
In this case I would create an array of properties to fetch on top
$props = 'DisplayName', 'EmailAddress', 'Title', 'Department', 'Manager', 'Office', 'PostalCode',
'State', 'Description', 'Country', 'Company', 'HomePhone', 'OfficePhone', 'MobilePhone', 'City'
$Objects = Get-ADUser -Filter * -Properties $props
P.S. I'm using PowerShell aliases here but you can use the LDAP attribute names if you like that better:
PowerShell | LDAP |
---|---|
EmailAddress | |
OfficePhone | telephoneNumber |
Office | physicalDeliveryOfficeName |
MobilePhone | mobile |
City | l |
Country | c |
答案2
得分: 0
你为什么要这样做 - 你正在无故重建一个对象?
Get-ADUser -identity gordom04 -Properties * | Select-Object Name,
samaccountname,
mail,
title,
department,
@{Name='Manager';Expression={(Get-AdUser $.Manager -Properties DisplayName).DisplayName}},
physicalDeliveryOfficeName,
postalCode,
State,
description,
Country,
company,
homephone,
telephoneNumber,
mobile,
Location,
@{Name='LastLogon';Expression={[DateTime]::FromFileTime($.LastLogon)}}
或者,如果你有一个需要的用户列表并希望将其保存到一个对象中:
$Objects = 'user01','user02'
$ResultObj = $Objects | ForEach-Object {
Get-ADUser -Identity $_ -Properties * | Select-Object Name,
samaccountname,
mail,
title,
department,
@{Name='Manager';Expression={(Get-AdUser $.Manager -Properties DisplayName).DisplayName}},
physicalDeliveryOfficeName,
postalCode,
State,
description,
Country,
company,
homephone,
telephoneNumber,
mobile,
Location,
@{Name='LastLogon';Expression={[DateTime]::FromFileTime($.LastLogon)}}
}
输出结果
$ResultObj
英文:
Why do it that way at all - you're reconstructing an object for no reason?
Get-ADUser -identity gordom04 -Properties * | Select-Object Name,
samaccountname,
mail,
title,
department,
@{Name='Manager';Expression={(Get-AdUser $_.Manager -Properties DisplayName).DisplayName}},
physicalDeliveryOfficeName,
postalCode,
State,
description,
Country,
company,
homephone,
telephoneNumber,
mobile,
Location,
@{Name='LastLogon';Expression={[DateTime]::FromFileTime($_.LastLogon)}}
Or, if you have a list of users you need and need it saved to an object:
$Objects = 'user01','user02'
$ResultObj = $Objects | ForEach-Object {
Get-ADUser -Identity $_ -Properties * | Select-Object Name,
samaccountname,
mail,
title,
department,
@{Name='Manager';Expression={(Get-AdUser $_.Manager -Properties DisplayName).DisplayName}},
physicalDeliveryOfficeName,
postalCode,
State,
description,
Country,
company,
homephone,
telephoneNumber,
mobile,
Location,
@{Name='LastLogon';Expression={[DateTime]::FromFileTime($_.LastLogon)}}
}
# Output results
$ResultObj
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论