Powershell -将组成员计数导出到excel

h79rfbju  于 2023-01-06  发布在  Shell
关注(0)|答案(2)|浏览(127)
foreach ($group in $ADMSObjects){
    
            $searchString = $group.GroupDisplayName
            $count = (Get-AzureADGroup -All $true -SearchString "$searchString" | Get-AzureADGroupMember -ALL 1).COUNT
            write-host $group.GroupDisplayName  , $count 
            
       }

我只需要函数“扩展”实际导出$group.GroupDisplayName和$count到Excel工作表。
当我试着做这样的事情时:
x一个一个一个一个x一个一个二个x
我真的不知道该怎么办。

kiayqfof

kiayqfof1#

你可以使用PSCustomObject来获得你的结果。我猜你提到的$group是组显示名称。我没有测试它的环境。所以请从你的端测试它。谢谢!

$result = foreach ($group in $ADMSObjects){
    
   $searchString = $group.GroupDisplayName
   $count = (Get-AzureADGroup -All $true -SearchString "$searchString" | Get-AzureADGroupMember -ALL 1).COUNT
   write-host $group.GroupDisplayName  , $count

    [PSCustomObject]@{
            Group = $group.GroupDisplayName 
            Count = $Count
        }
}
$result | Export-Csv -Path $path -Append
oxf4rvwz

oxf4rvwz2#

我尝试在我的环境中重现该方案:

  • 注意:要打印显示名称:* 使用***$group.DisplayName***运行

使用以下代码:

$azgroups=Get-AzureADGroup
foreach ($group in $azgroups){
$searchString = $group.GroupDisplayName
$count = (Get-AzureADGroup -All $true -SearchString "$searchString" | Get-AzureADGroupMember -ALL 1).COUNT

write-host $group.DisplayName  , $count

$group_properties = [pscustomobject] @{
"DisplayName" = $group.DisplayName
"Count" = $count
}
$group_properties | Export-csv -Path  C:\User\xxx\myexcelsheet" -NoTypeInformation -Force -Append
}

csv或excel文件创建成功:

我们可以在csv文件中看到所需的字段

相关问题