首页 > 解决方案 > 带有 CSV 的 Powershell 脚本从 DisplayName 获取 objectID

问题描述

我想制作一个新的 CSV 文件,该文件仅包含来自 CSV 文件的 ObjectID,其中包含计算机名称 (DisplayName) 列表

connect-azuread
$csv = Import-Csv C:\Tools\PCname.csv
foreach ($DisplayName in $csv){
    $ObejctID = get-AzureADDevice -Filter "DisplayName eq '$._DisplayName'" | Select ObjectID 
}
$ObejctID
$Content.objectID | Out-File 'C:\Tools\BulkObjectID.csv'

我从 ISE 收到以下错误:

***get-AzureADDevice : Le terme «get-AzureADDevice» n'est pas reconnu comme nom d'applet de commande, fonction, fichier de script ou 
programme exécutable. Vérifiez l'orthographe du nom, ou si un chemin d'accès existe, vérifiez que le chemin d'accès est correct et 
réessayez.
Au caractère Ligne:11 : 17
+     $ObejctID = get-AzureADDevice -Filter "DisplayName eq '$._Display ...
+                 ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (get-AzureADDevice:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException***

任何人都可以帮助我吗?

标签: azurepowershellcsv

解决方案


除了让您的 Get-AzureADDevice 正常工作。看起来您没有在列表中添加任何内容。我会做这样的事情。创建一个空数组 ObjectID。然后在 foreach 中将元素添加到数组中。在您的 foreach 之后,您调用数组并将其输出到 export-csv。

$csv = Import-Csv C:\Tools\PCname.csv
$ObjectID=@()
foreach ($DisplayName in $csv){
    $ObejctID += get-AzureADDevice -Filter "DisplayName eq '$._DisplayName'" | Select ObjectID 
}
$ObejctID
$ObjectID | export-csv 'C:\Tools\BulkObjectID.csv'

推荐阅读