使用 PowerShell 的 Try/Catch 在两个 cmdlet gro 组成员之间切换?

问题描述 投票:0回答:1

使用PowerShell脚本If Else或Try catch,如何修改以下脚本,如果组成员类型=计算机,则使用Get-MGDevice显示显示名称。

$GROUP_LIST = Get-MgGroup -Filter "groupTypes/any(c:c eq 'DynamicMembership')" -All:$true | Select-Object Id, DisplayName, Description, GroupTypes

$users = $GROUP_LIST | ForEach-Object {
    $Group = $_
    Write-Host "Processing $($Group.DisplayName)" -ForegroundColor Cyan
    $members = Get-MgGroupMember -GroupId $Group.Id -Property "id,displayName,userPrincipalName,companyName"

    Write-Host "`t [$($Group.DisplayName)] Member count [$($members.Count)]" -ForegroundColor Yellow

    $members | ForEach-Object {
        $member = $_
        Try {
            $user = Get-MgUser -UserId $member.Id -Property Id, DisplayName, Mail, UserPrincipalName, CompanyName | 
                    Select-Object Id, DisplayName, Mail, UserPrincipalName, CompanyName

            [PSCustomObject]@{
                Group            = $Group.DisplayName
                Name             = $user.DisplayName
                USERPRINCIPALNAME = $user.Mail
                CompanyName      = $user.CompanyName
            }
        } Catch {
            Try {
                $device = Get-MgDevice -DeviceId $member.Id -Property Id, DisplayName | 
                          Select-Object Id, DisplayName

                [PSCustomObject]@{
                    Group            = $Group.DisplayName
                    Name             = $device.DisplayName
                    USERPRINCIPALNAME = "N/A"
                    CompanyName      = "N/A"
                }
            } Catch {
                Write-Host "Unable to find user or device for member ID $($member.Id)" -ForegroundColor Red
                [PSCustomObject]@{
                    Group            = $Group.DisplayName
                    Name             = "Unknown"
                    USERPRINCIPALNAME = "N/A"
                    CompanyName      = "N/A"
                }
            }
        }
    }
}

$users | Out-GridView

上面的 PowerShell 脚本对于 Get-MgUser 部分工作得很好,但是,Get-MgDevice 没有返回任何值或为空。

感谢您的协助。

powershell microsoft-graph-api azure-powershell
1个回答
0
投票

要获取组的用户(用户/设备),请按照

@iRon
的建议,使用 if/else 条件修改如下脚本:

$GROUP_LIST = Get-MgGroup -Filter "groupTypes/any(c:c eq 'DynamicMembership')" -All:$true | Select-Object Id, DisplayName, Description, GroupTypes

$users = $GROUP_LIST | ForEach-Object {
    $Group = $_
    Write-Host "Processing $($Group.DisplayName)" -ForegroundColor Cyan
    $members = Get-MgGroupMember -GroupId $Group.Id -Property "id,displayName,userPrincipalName,companyName"

    Write-Host "`t [$($Group.DisplayName)] Member count [$($members.Count)]" -ForegroundColor Yellow

    $members | ForEach-Object {
        $member = $_
        $isUser = $false
        $isDevice = $false
        $user = $null
        $device = $null

        Try {
            $user = Get-MgUser -UserId $member.Id -Property Id, DisplayName, Mail, UserPrincipalName, CompanyName -ErrorAction Stop
            $isUser = $true
        } Catch {
            Write-Host "User not found for member ID $($member.Id), trying as device..." -ForegroundColor Magenta
        }

        If (!$isUser) {
            Try {
                $device = Get-MgDevice -DeviceId $member.Id -Property Id, DisplayName -ErrorAction Stop
                $isDevice = $true
            } Catch {
                Write-Host "Device not found for member ID $($member.Id)" -ForegroundColor Red
            }
        }

        If ($isUser) {
            [PSCustomObject]@{
                Group             = $Group.DisplayName
                Name              = $user.DisplayName
                USERPRINCIPALNAME = $user.Mail
                CompanyName       = $user.CompanyName
            }
        } ElseIf ($isDevice) {
            [PSCustomObject]@{
                Group             = $Group.DisplayName
                Name              = $device.DisplayName
                USERPRINCIPALNAME = "N/A"
                CompanyName       = "N/A"
            }
        } Else {
            [PSCustomObject]@{
                Group             = $Group.DisplayName
                Name              = "Unknown"
                USERPRINCIPALNAME = "N/A"
                CompanyName       = "N/A"
            }
        }
    }
}

$users | Out-GridView

enter image description here

enter image description here

我在 testrukdevicegrp 中将设备添加为组成员:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.