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

布朗镇

我的目标是将用户列表从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 [email protected]

然后

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
[email protected]                      directoryObjects/7fed7e4a-78be-4e87-9d88...
[email protected]                   image/Jpeg
userIdentities                                          []
extension_60a1274a0a9d4344bd172d81b06d0f50_MemberNumber 19999

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

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

非常感谢!

好吧,根据萨蒂亚的出色建议,我正在取得进步。我认为遍历所有用户将它们导出到一个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中工作。

萨蒂亚五世

您可以尝试以下代码段:

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

我已利用Get-AzureAD满足您要求的

#Gettting the User from the AAD
$user= Get-AzureADUser -ObjectID [email protected]

#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

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档