[英]How do i nest another cmdlet in foreach loop in powershell
For each userID I want to get the date created for that user.对于每个用户 ID,我想获取为该用户创建的日期。
This is what I use for single user and this works.这是我用于单用户的,并且有效。
(Get-AzureADUserExtension -ObjectId "00444a60-3468-48e7-97d7-69f08aac346d").Get_Item("createdDateTime")
But I want a loop.但我想要一个循环。 I tried below我在下面试过
Get-AzureADUser -All:$true | ForEach-Object{
$_.ObjectId
$_.UserPrincipalName
$_ | Add-Member -MemberType NoteProperty -Name "CreatedDateTime" -Value (Get-AzureADUserExtension -ObjectId $_.ObjectId).Get_Item("createdDateTime")
}
But then I'm asked to put a value (which I don't want) and when I do so I get an error -Value: The term '-Value' is not recognized as the name of a cmdlet[...]但是后来我被要求输入一个值(我不想要),当我这样做时,我得到一个错误-Value: The term '-Value' is not Recognized as the name of a cmdlet[...]
I also tried this我也试过这个
Get-AzureADUser -All:$true |
ForEach-Object {
$UserObjectID = $_
ForEach-Object{
[PSCustomObject]@{
UserObjectID = $_.ObjectId
UserUserPrincipalName = $_.UserPrincipalName
UserDisplayName = $_.DisplayName
createdDateTime = $_.createdDateTime
}
}
}
But here in result last column is empty which is understandable, because this command is not part of Get-AzureADUser cmdlet.但是这里结果最后一列是空的,这是可以理解的,因为此命令不是Get-AzureADUser cmdlet 的一部分。
I tried some more but also without a success.我尝试了更多但也没有成功。 After I get this I also want to add +60 days to the createdDateTime but here I have no clue even how to start.在我得到这个之后,我还想在createdDateTime中添加 +60 天,但在这里我什至不知道如何开始。
Please advise how to correct the script and add 60 days to the result.请告知如何更正脚本并将结果添加 60 天。
EDIT:编辑:
I tried now this我现在试过这个
Get-AzureADUser -All:$true |
ForEach-Object {
$UserObjectID = $_
ForEach-Object{
[PSCustomObject]@{
UserObjectID = $_.ObjectId
UserUserPrincipalName = $_.UserPrincipalName
UserDisplayName = $_.DisplayName
DateCreated = $_ | Add-Member -MemberType NoteProperty -Name "CreatedDateTime" -Value (Get-AzureADUserExtension -ObjectId $_.ObjectId).Get_Item("createdDateTime")
}
}
}
But the DateCreated column is still empty但是 DateCreated 列仍然是空的
You don't really need a loop construct here, you can get the desired output with Select-Object
:您在这里并不真正需要循环构造,您可以使用Select-Object
获得所需的 output :
Get-AzureADUser -All |Select ObjectId,UserPrincipalName,@{Name='CreationDate'; Expression={(Get-AzureADUserExtension -ObjectId $_.ObjectId).Get_Item("createdDateTime")}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.