Warm tip: This article is reproduced from serverfault.com, please click

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

(python - How to export Extension Attributes from Azure AD to csv using Powershell)

发布于 2020-11-29 12:59:27

我的目标是将用户列表从Azure AD导出到可以从Python读取的csv文件中。使用以下命令就足够简单了:

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

但是,如何在输出中包括扩展属性?我试过:

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

但这仅导出空白的第三列

我想要的扩展属性在那里(extension_60a1274a0a9d4344bd172d81b06d0f50_MemberNumber),我可以使用以下命令看到它:

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

然后

Get-AzureADUserExtension -ObjectId $User.ObjectId

哪个输出:

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

但是,如何将这些扩展名属性(以及常规属性)导出到CSV文件中供所有用户使用?我不在乎它仅导出我需要的一个扩展属性还是全部导出-我可以从Python端使用所需的扩展名。

我已经阅读了许多Microsoft和其他文章,但是找不到如何执行此操作。

非常感谢!

好的,根据萨蒂亚(Satya)的出色建议,我正在取得进步。我认为遍历所有用户以将其导出到一个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

该循环起作用,并且写主机显示每个记录,但是export-csv在文件中不产生任何记录。我也尝试过-append,但阅读它存在一些问题,阻止了它在foreach中工作。

Questioner
BrownInTown
Viewed
11
Satya V 2020-11-30 13:09:05

你可以尝试以下代码段:

据我研究,从计算机中检索数据的可能性可能会略少一些。 Get-MSOLUser

我已经利用了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

样本输出

在此处输入图片说明

更新的代码

$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

}