Powershell-获取列表成员值

Powershell-获取列表成员值,powershell,azure-powershell,Powershell,Azure Powershell,在下面的代码片段中,我试图将外部列表$admin_角色中的$roleName(as-Role)附加到文件中。 该角色总是显示为空(其他列也可以)。我错过了什么 Connect-MsolService $output_file_location = "c:\temp\azure_admins_mfa_status_"+$(get-date -f yyyy-MM-dd-HH-mm-ss)+".csv" $admin_roles = "Company

在下面的代码片段中,我试图将外部列表$admin_角色中的$roleName(as-Role)附加到文件中。 该角色总是显示为空(其他列也可以)。我错过了什么

Connect-MsolService
$output_file_location = "c:\temp\azure_admins_mfa_status_"+$(get-date -f yyyy-MM-dd-HH-mm-ss)+".csv"
$admin_roles = "Company Administrator","Billing Administrator","Conditional Access Administrator","Exchange Service administrator","Helpdesk administrator","Password administrator","Security administrator","Sharepoint Service administrator"

# Gets all the members in the admin roles in the roles list above
# Gets the MFA status for each member
# Appends the below data points to a file specified in the $output_file_location variable 
# DisplayName,E-mail,Role,MFA-Requirements, MFA-Methods, MFA-MethodsDefault
function get-mfa-status
{
    foreach ($roleName in $admin_roles)
    {
        write-output $roleName
        $members = Get-MsolRoleMember -RoleObjectId $(Get-MsolRole -RoleName $roleName).ObjectId
        #write-output $members
        foreach ($member in $members) 
        {
            write-output $member.EmailAddress
            
        }
        
        foreach ($member in $members) 
        { 
        Get-MsolUser -UserPrincipalName $member.EmailAddress | select DisplayName, `
        @{N='E-mail';E={$_.userPrincipalName}}, `
        {N='Role';E={$roleName}}, `
        @{N='MFA-Requirements';E={(($_).StrongAuthenticationRequirements.state)}}, `
        @{N='MFA-Methods';E={(($_).StrongAuthenticationMethods.MethodType)}}, `
        @{N='MFA-MethodsDefault';E={($_.StrongAuthenticationMethods | where isdefault -eq 'true').MethodType}} `
        | select DisplayName,E-mail,Role,MFA-Requirements, MFA-Methods, MFA-MethodsDefault| Export-Csv $output_file_location -Append `
        }
        
    }
    
    
}

您的代码在
{N='Role';E={$roleName}
之前缺少
@
。通常,您会立即注意到,因为它会将列名显示为整个表达式

但是,由于第二个
选择对象
,您看不到这一点。PowerShell选择名为
Role
的列。由于它不存在,您会看到空列


旁注:我认为第二个
选择对象
是不必要的。跳过它将导致相同的输出(前提是您纠正了上面提到的损坏的表达式)。

我认为您在
{N='Role';E={$roleName}}
之前缺少了
@
谢谢@robdy。请随意添加此答案,我将接受。顺便说一句,只是一个格式注释。实际上并不需要这些墓地重音符号(反勾号),因为逗号是一个自然延续字符(就像运算符等一样),只要管道与它左边的代码在同一行上,它就是一个自然延续字符。是的,但是如果你对他们没意见,那你就是你了。请参见:。它有很多优点,尽管我并不完全同意作者所陈述的所有立场。