azure - PowerShell - 将导入的 csv 列添加到新导出的 csv 中

标签 azure powershell office365

我希望有人能帮助我。我正在尝试获取将许可证分配给用户的日期并将其导出到新的 csv。导入 csv 包含 UserPrincipalName。我能够缩小范围,仅显示我想要的许可证,但在许可证/日期旁边显示 UPN 将完成此脚本。提前致谢

$getusers = Import-csv -Path 'C:\test\userlist.csv'

foreach ($user in $getusers) {

    (Get-AzureADUser -searchstring $User.UserPrincipalName).assignedplans | where {$_.Service -eq 'MicrosoftOffice'} | Select-Object Service,AssignedTimeStamp |
    

    Export-CSV -Path "C:\test\userlist-export.csv" -notypeinformation



}

最佳答案

我会这样做,首先查询用户并将其存储在变量中,然后过滤 AssignedPlans 其中 Service = MicrosoftOffice。要构造对象,您可以使用 [pscustomobject] 。值得注意的是,对 Export-Csv 的调用应该是管道中的最后一个语句(它不应该在循环内),否则您将在每次循环迭代时用新值替换 Csv而不是附加数据。

Import-Csv -Path 'C:\test\userlist.csv' | ForEach-Object {
    $azUser = Get-AzureADUser -ObjectId $_.UserPrincipalName
    foreach($plan in $azUser.AssignedPlans) {
        if($plan.Service -eq 'MicrosoftOffice') {
            [pscustomobject]@{
                UserPrincipalName = $azUser.UserPrincipalName
                Service           = $plan.Service
                AssignedTimeStamp = $plan.AssignedTimeStamp
            }
        }
    }
} | Export-Csv "C:\test\userlist-export.csv" -NoTypeInformation

关于azure - PowerShell - 将导入的 csv 列添加到新导出的 csv 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74778562/

相关文章:

powershell - 在Powershell中复制和重命名具有特定文件名的文件

json - Office 365 API 为全天事件返回不正确的开始和结束日期时间

c# - Microsoft Graph API 更新其他用户的照片?

c# - Microsoft.NET.Sdk.Functions引起的版本冲突

windows - 将现有服务器安装 Chocolatey

visual-studio - 如何在 Visual Studio 2012 中调试我的 Azure Web 角色?

windows - 显示 GetInvalidFileNameChars() 的 Powershell 输出的十六进制值

office365 - 使用 Microsoft OneDrive API/SDK 的客户端分页

azure - 用 KQL 投影一周中的每一天

azure - 错误索引方法 FunctionName 未解析为值