简体   繁体   English

我如何在 powershell 的 foreach 循环中嵌套另一个 cmdlet

[英]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.

相关问题 如何摆脱 Azure Powershell 警告“警告:AzureVM 此 cmdlet 输出的属性将在即将到来的...中更改” - How do I get rid of Azure Powershell warning "WARNING: AzureVM A property of the output of this cmdlet will change in an upcoming..." 如何通过具有凭据的代理服务器运行Azure PowerShell cmdlet? - How can I run an Azure powershell cmdlet through a proxy server with credentials? Azure 数据工厂 - 如何使用 ForEach 循环遍历 CSV 文件中的记录 - Azure Data Factory - How do I iterate through records in a CSV file using a ForEach loop 如何在 Azure Pipeline ForEach 循环中迭代 json 对象列表 - How do I iterate a list of json objects in an Azure Pipeline ForEach loop PowerShell Azure Cmdlet无法识别 - PowerShell Azure Cmdlet not Recognized Azure PowerShell cmdlet转换为字符串 - Azure PowerShell cmdlet into Strings 如何使用 AzureRm Powershell cmdlet 添加 azure 订阅 sqlfilter? - how to add azure subscription sqlfilter using AzureRm Powershell cmdlet? 如何检查 Powershell cmdlet Get-AzKeyVaultSecret 是否支持 -AsPlainText 参数? - How to check if the Powershell cmdlet Get-AzKeyVaultSecret supports -AsPlainText parameter? 如何打包 PowerShell Binary Cmdlet 从 Azure DevOps Gallery 安装 - How to pack PowerShell Binary Cmdlet to install from Azure DevOps Gallery 在foreach循环Powershell中将NSG关联到子网 - Associate NSG to Subnets in foreach loop Powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM