简体   繁体   English

如何使用 Powershell 将扩展属性从 Azure AD 导出到 csv

[英]How to export Extension Attributes from Azure AD to csv using Powershell

My goal is to export a user list from Azure AD to a csv file I can read from Python. That's easy enough using:我的目标是将用户列表从 Azure AD 导出到我可以从 Python 读取的 csv 文件。这很容易使用:

Get-MsolUser -All | Select-Object UserPrincipalName, WhenCreated | export-csv c:\try2.csv

But how do I include extension attributes in the output?但是如何在 output 中包含扩展属性? I tried:我试过:

Get-MsolUser -All | Select-Object UserPrincipalName, WhenCreated, 
    extension_60a1274a0a9d4344bd172d81b06d0f50_MemberNumber | export-csv c:\try2.csv

But this exports only a blank third column但这只导出空白的第三列

The extension attribute I want is there (extension_60a1274a0a9d4344bd172d81b06d0f50_MemberNumber), and I can see it using:我想要的扩展属性就在那里 (extension_60a1274a0a9d4344bd172d81b06d0f50_MemberNumber),我可以使用以下命令查看它:

$user = Get-AzureADUser -ObjectID first.last@domain.com

Then然后

Get-AzureADUserExtension -ObjectId $User.ObjectId

Which outputs:哪些输出:

Key                                                     Value
---                                                     -----
odata.metadata                                          https://graph.windows.net/3523a793-0e50-4646...
odata.type                                              Microsoft.DirectoryServices.User
createdDateTime                                         4/23/2020 10:22:17 PM
employeeId                                              12345
onPremisesDistinguishedName
thumbnailPhoto@odata.mediaEditLink                      directoryObjects/7fed7e4a-78be-4e87-9d88...
thumbnailPhoto@odata.mediaContentType                   image/Jpeg
userIdentities                                          []
extension_60a1274a0a9d4344bd172d81b06d0f50_MemberNumber 19999

But how do I export these extension attributes in a CSV file for all users (along with the regular attributes)?但是,如何将这些扩展属性导出到所有用户的 CSV 文件中(连同常规属性)? I don't care if it exports just the one extension attribute I need, or all of them--I can just use what I need from the Python side.我不关心它是只导出我需要的一个扩展属性,还是导出所有扩展属性——我可以只使用我需要的 Python 端的属性。

I've read through many Microsoft and other articles but can't find how to do this.我已经阅读了许多 Microsoft 和其他文章,但找不到如何执行此操作。

Thanks very much!非常感谢!

OK, based on Satya's excellent suggestion, I'm making progress.好的,根据 Satya 的出色建议,我正在取得进展。 I thought it would be easy to loop through all users to export them to one csv file, but I've got something wrong... here's the current:我认为遍历所有用户以将它们导出到一个 csv 文件会很容易,但我出了点问题......这是当前的:

$all = get-azureaduser -All $true
$all | foreach-object {
$user = $_
#Expanding only the Extension Attributes related to the user and converting the Dictionary to Custom Object so that keys can be accessed through the dot (.) operator
$Extension_Attributes = New-Object Psobject -Property $user.ExtensionProperty 

#Combining the required attributes from the user object and extension_attributes to A single object

$u_properties = [pscustomobject] @{
"UserPrincipalName" = $user.UserPrincipalName 
"Country" = $user.Country 
"Created" = $Extension_Attributes.createdDateTime 
"MemberNumber" = $Extension_Attributes.extension_60a1274a0a9d4344bd172d81b06d0f50_MemberNumber
}

# check
write-host "got $u_properties"

}
Select-object $u_properties | Export-csv -Path c:\ulist.csv -NoTypeInformation -Force

The loop works and the write-host shows each record, but the export-csv produces no records in the file.循环有效,写入主机显示每条记录,但 export-csv 在文件中不产生任何记录。 I had also tried -append but read there is some problem with it that prevents it working inside a foreach.我也试过 -append 但读到它有一些问题阻止它在 foreach 中工作。

You could try the below snippet:您可以尝试以下代码段:

As far as I researched, there might be little less possiblity of retrieving it from the Get-MSOLUser据我研究,从Get-MSOLUser检索它的可能性可能很小

I have made use of the Get-AzureAD that would meet your requirement我已经使用了满足您要求的Get-AzureAD

#Gettting the User from the AAD
$user= Get-AzureADUser -ObjectID user@domain.com

#Expanding only the Extenstion Attributes related to the user and converting the Dictionary to Custom Object so that keys can be accessed through the dot (.) operator
$Extension_Attributes = New-Object Psobject -Property $user.ExtensionProperty 

#Combining the required attributes from the user object and extension_attributes to A single object
$u_properties = [pscustomobject] @{
"UserPrincipal" = $user.UserPrincipalName
"Name" = $user.Country
"Created" = $Extension_Attributes.createdDateTime
}
#if you need more attributes you can accordingly


#Exporting the object to a file in an append fashoin
$u_properties | Export-Csv -Path D:\File.csv -Append -NoTypeInformation

Sample Output样品 Output

在此处输入图像描述

Updated Code更新代码

$all = get-azureaduser -All $true
$all | foreach-object {
$user = $_
#Expanding only the Extension Attributes related to the user and converting the Dictionary to Custom Object so that keys can be accessed through the dot (.) operator
$Extension_Attributes = New-Object Psobject -Property $user.ExtensionProperty 

#Combining the required attributes from the user object and extension_attributes to A single object

$u_properties = [pscustomobject] @{
"UserPrincipalName" = $user.UserPrincipalName 
"Country" = $user.Country 
"Created" = $Extension_Attributes.createdDateTime 
"MemberNumber" = $Extension_Attributes.extension_60a1274a0a9d4344bd172d81b06d0f50_MemberNumber
}

# check
write-host "got $u_properties"

 $u_properties | Export-csv -Path D:\File3.csv -NoTypeInformation -Force -Append

}

My goal is to export a user list from Azure AD to a csv file I can read from Python.我的目标是将用户列表从 Azure AD 导出到 csv 文件,我可以从 Python 中读取。 That's easy enough using:这很容易使用:

Get-MsolUser -All | Select-Object UserPrincipalName, WhenCreated | export-csv c:\try2.csv

But how do I include extension attributes in the output?但是如何在 output 中包含扩展属性? I tried:我试过了:

Get-MsolUser -All | Select-Object UserPrincipalName, WhenCreated, 
    extension_60a1274a0a9d4344bd172d81b06d0f50_MemberNumber | export-csv c:\try2.csv

But this exports only a blank third column但这仅导出空白的第三列

The extension attribute I want is there (extension_60a1274a0a9d4344bd172d81b06d0f50_MemberNumber), and I can see it using:我想要的扩展属性在那里(extension_60a1274a0a9d4344bd172d81b06d0f50_MemberNumber),我可以看到它使用:

$user = Get-AzureADUser -ObjectID first.last@domain.com

Then然后

Get-AzureADUserExtension -ObjectId $User.ObjectId

Which outputs:哪个输出:

Key                                                     Value
---                                                     -----
odata.metadata                                          https://graph.windows.net/3523a793-0e50-4646...
odata.type                                              Microsoft.DirectoryServices.User
createdDateTime                                         4/23/2020 10:22:17 PM
employeeId                                              12345
onPremisesDistinguishedName
thumbnailPhoto@odata.mediaEditLink                      directoryObjects/7fed7e4a-78be-4e87-9d88...
thumbnailPhoto@odata.mediaContentType                   image/Jpeg
userIdentities                                          []
extension_60a1274a0a9d4344bd172d81b06d0f50_MemberNumber 19999

But how do I export these extension attributes in a CSV file for all users (along with the regular attributes)?但是,如何在 CSV 文件中为所有用户导出这些扩展属性(以及常规属性)? I don't care if it exports just the one extension attribute I need, or all of them--I can just use what I need from the Python side.我不在乎它是否只导出我需要的一个扩展属性,或者所有这些属性——我可以只使用 Python 方面需要的东西。

I've read through many Microsoft and other articles but can't find how to do this.我已经阅读了许多 Microsoft 和其他文章,但找不到如何做到这一点。

Thanks very much!非常感谢!

OK, based on Satya's excellent suggestion, I'm making progress.好的,基于 Satya 的出色建议,我正在取得进展。 I thought it would be easy to loop through all users to export them to one csv file, but I've got something wrong... here's the current:我认为循环所有用户以将它们导出到一个 csv 文件会很容易,但我有问题......这是当前的:

$all = get-azureaduser -All $true
$all | foreach-object {
$user = $_
#Expanding only the Extension Attributes related to the user and converting the Dictionary to Custom Object so that keys can be accessed through the dot (.) operator
$Extension_Attributes = New-Object Psobject -Property $user.ExtensionProperty 

#Combining the required attributes from the user object and extension_attributes to A single object

$u_properties = [pscustomobject] @{
"UserPrincipalName" = $user.UserPrincipalName 
"Country" = $user.Country 
"Created" = $Extension_Attributes.createdDateTime 
"MemberNumber" = $Extension_Attributes.extension_60a1274a0a9d4344bd172d81b06d0f50_MemberNumber
}

# check
write-host "got $u_properties"

}
Select-object $u_properties | Export-csv -Path c:\ulist.csv -NoTypeInformation -Force

The loop works and the write-host shows each record, but the export-csv produces no records in the file.循环有效,写入主机显示每条记录,但 export-csv 在文件中不生成任何记录。 I had also tried -append but read there is some problem with it that prevents it working inside a foreach.我也尝试过 -append 但读到它有一些问题阻止它在 foreach 中工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM