<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>azure powershell add role assignment Archives - the Sysadmin Channel</title>
	<atom:link href="https://thesysadminchannel.com/tag/azure-powershell-add-role-assignment/feed/" rel="self" type="application/rss+xml" />
	<link>https://thesysadminchannel.com/tag/azure-powershell-add-role-assignment/</link>
	<description>Documenting My Life as a System Administrator</description>
	<lastBuildDate>Tue, 26 Oct 2021 23:11:02 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.2</generator>
<site xmlns="com-wordpress:feed-additions:1">144174110</site>	<item>
		<title>How To Add Azure AD Roles Using PowerShell With PIM</title>
		<link>https://thesysadminchannel.com/how-to-add-azure-ad-roles-using-powershell-with-pim/</link>
					<comments>https://thesysadminchannel.com/how-to-add-azure-ad-roles-using-powershell-with-pim/#comments</comments>
		
		<dc:creator><![CDATA[Paul Contreras]]></dc:creator>
		<pubDate>Wed, 13 Oct 2021 23:10:56 +0000</pubDate>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[add Azure AD Roles Using PowerShell]]></category>
		<category><![CDATA[assign a user to a role in microsoft azure active directory powershell]]></category>
		<category><![CDATA[azure powershell add role assignment]]></category>
		<category><![CDATA[powershell command to assign a user to a role in microsoft azure active directory]]></category>
		<category><![CDATA[you need to assign a user to a role in microsoft azure active directory powershell]]></category>
		<guid isPermaLink="false">https://thesysadminchannel.com/?p=3494</guid>

					<description><![CDATA[<p>If you recall a while back we published an article to Get PIM Role Assignment Status For Azure AD Using Powershell, and today we&#8217;re going to build on top of that to add Azure AD Roles Using PowerShell &#160; The&#8230; <a href="https://thesysadminchannel.com/how-to-add-azure-ad-roles-using-powershell-with-pim/" class="more-link">Continue Reading <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a href="https://thesysadminchannel.com/how-to-add-azure-ad-roles-using-powershell-with-pim/">How To Add Azure AD Roles Using PowerShell With PIM</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>If you recall a while back we published an article to <a href="https://thesysadminchannel.com/get-pim-role-assignment-status-for-azure-ad-using-powershell/" rel="noopener" target="_blank">Get PIM Role Assignment Status For Azure AD Using Powershell</a>, and today we&#8217;re going to build on top of that to <strong>add Azure AD Roles Using PowerShell</strong><br />
&nbsp;</p>
<p>The goal here is to eliminate adding people using the Azure portal and use a method that&#8217;s more scalable and automatable.  Because who doesn&#8217;t love automation right? The script I&#8217;m going to share with you today leverages the concept of just in time access using <a href="https://docs.microsoft.com/en-us/azure/active-directory/privileged-identity-management/pim-configure" rel="noopener" target="_blank">Azure AD Privilege Identity Management (PIM)</a>.<br />
&nbsp;</p>
<div id="blockquote1">
<strong>Note:</strong> PIM requires an Azure AD P2 License in your Tenant which is required for this to work.
</div>
<h2>Requirements</h2>
<p>In order for the script to run successfully you will need to have the following in place.</p>
<ul>
<li><strong>Azure AD P2</strong> License for PIM Use</li>
<li><strong>Privileged Role Administrator</strong> -or <strong>Global Administrator</strong> to grant permissions</li>
<li><strong>AzureADPreview</strong> Powershell Module</li>
</ul>
<p>Powershell 5+ would be preferred and I want to emphasize the AzureAdPreview Module (not AzureAD) as this has the commands we&#8217;re looking for.</p>
<p>&nbsp;</p>
<h2>Script Parameters</h2>
<h4>
<ul> UserPrincipalName</ul>
</h4>
<p>Specify the UserPrincipalName for the user you want to add roles for.  </p>
<h4>
<ul> RoleName</ul>
</h4>
<p>Specify the RoleName you want to add. Tab-Completion is enabled to ensure roles are accurate.</p>
<h4>
<ul> TenantId</ul>
</h4>
<p>By default it will use the TenantId from your current session.  If you&#8217;re connected to a multi-tenant, you can specify the tenant here.</p>
<h4>
<ul> DurationInMonths</ul>
</h4>
<p>Set how long you would like to add the role for. Default is 48 months (4 years)</p>
<h4>
<ul> TicketNumber</ul>
</h4>
<p>Add a ticket number if needed for auditing purposes.  &#8220;Justification&#8221; can also be used in lieu of &#8220;TicketNumber&#8221;</p>
<p>&nbsp;</p>
<h2>Add Azure AD Roles Using PowerShell With PIM Eligible Assignment</h2>
<p>Now that we know what&#8217;s needed, let&#8217;s move on to the actual script.</p>
<pre class="brush: powershell; title: ; notranslate">

Function Add-PIMRoleAssignment {
&lt;#
.Synopsis
    This add a user to a PIM Role in Azure AD.
    For updated help and examples refer to -Online version.

.NOTES
    Name: Add-PIMRoleAssignment
    Author: theSysadminChannel
    Version: 1.0
    DateCreated: 2021-Sep-13
#&gt;

    [CmdletBinding()]
    param(
        [Parameter(
            Mandatory = $false,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            Position  = 0
        )]
        [string[]]  $UserPrincipalName,


        [Parameter(
            Mandatory = $true,
            Position  = 1
        )]
        [ValidateSet(
            'Application Administrator',
            'Application Developer',
            'Attack Simulation Administrator',
            'Authentication Administrator',
            'Azure Information Protection Administrator',
            'Billing Administrator',
            'Cloud Device Administrator',
            'Compliance Administrator',
            'Conditional Access Administrator',
            'Device Managers',
            'Directory Readers',
            'Directory Writers',
            'Exchange Administrator',
            'Exchange Recipient Administrator',
            'Global Administrator',
            'Global Reader',
            'Helpdesk Administrator',
            'Intune Administrator',
            'License Administrator',
            'Message Center Privacy Reader',
            'Message Center Reader',
            'Power BI Administrator',
            'Power Platform Administrator',
            'Privileged Authentication Administrator',
            'Privileged Role Administrator',
            'Reports Reader',
            'Search Administrator',
            'Security Administrator',
            'Security Reader',
            'Service Support Administrator',
            'SharePoint Administrator',
            'Skype for Business Administrator',
            'Teams Administrator',
            'Teams Communications Administrator',
            'Teams Communications Support Engineer',
            'Teams Communications Support Specialist',
            'User Administrator'
        )]
        [string]    $RoleName,


        [Parameter(
            Mandatory = $false,
            Position  = 2
        )]
        [string]    $TenantId,


        [Parameter(
            Mandatory = $false,
            Position  = 3
        )]
        [int]   $DurationInMonths = 48,


        [Parameter(
            Mandatory = $false,
            Position  = 4
        )]
        [Alias('Justification')]
        [string]   $TicketNumber

    )

    BEGIN {
        $SessionInfo = Get-AzureADCurrentSessionInfo -ErrorAction Stop
        if (-not ($PSBoundParameters.ContainsKey('TenantId'))) {
            $TenantId = $SessionInfo.TenantId
        }

        $AdminRoles = Get-AzureADMSPrivilegedRoleDefinition -ProviderId aadRoles -ResourceId $TenantId -ErrorAction Stop | select Id, DisplayName
        $RoleId = @{}
        $AdminRoles | ForEach-Object {$RoleId.Add($_.DisplayName, $_.Id)}
    }

    PROCESS {
        $Schedule = New-Object Microsoft.Open.MSGraph.Model.AzureADMSPrivilegedSchedule
        $Schedule.Type = &quot;Once&quot;
        $Schedule.StartDateTime = (Get-Date)
        $Schedule.EndDateTime = (Get-Date).AddMonths($DurationInMonths)

        foreach ($User in $UserPrincipalName) {
            try {
                $AzureADUser = Get-AzureADUser -ObjectId $User -ErrorAction Stop | select-object UserPrincipalName, ObjectId
                Open-AzureADMSPrivilegedRoleAssignmentRequest -ProviderId Aadroles -Schedule $Schedule -ResourceId $TenantId -RoleDefinitionId $RoleId[$RoleName] `
                    -SubjectId $AzureADUser.ObjectId -AssignmentState Eligible -Type AdminAdd -Reason $TicketNumber -ErrorAction Stop | Out-Null

                [PSCustomObject]@{
                    UserPrincipalName = $AzureADUser.UserPrincipalName
                    RoleName          = $RoleName
                    DurationInMonths  = $DurationInMonths
                    Justification     = $TicketNumber
                }

            } catch {
                Write-Error $_.Exception.Message
            }
        }
    }

    END {}

}

</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2021/10/Add-Azure-AD-Roles-Using-PowerShell-and-PIM-Update.png" target="_blank" rel="noopener"><img fetchpriority="high" decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2021/10/Add-Azure-AD-Roles-Using-PowerShell-and-PIM-Update.png" alt="Add Azure AD Roles Using PowerShell and PIM-Update" width="1099" height="807" class="aligncenter size-full wp-image-3502" srcset="https://thesysadminchannel.com/wp-content/uploads/2021/10/Add-Azure-AD-Roles-Using-PowerShell-and-PIM-Update.png?v=1634161555 1099w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Add-Azure-AD-Roles-Using-PowerShell-and-PIM-Update-1024x752.png?v=1634161555 1024w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Add-Azure-AD-Roles-Using-PowerShell-and-PIM-Update-768x564.png?v=1634161555 768w" sizes="(max-width: 1099px) 100vw, 1099px" /></a><br />
&nbsp;</p>
<p>Now when I look at the Azure AD Roles for the role name I just granted, we can see that Buzz now has an eligible role for Reports Reader.  This confirms that everything worked as expected.<br />
<a href="https://thesysadminchannel.com/wp-content/uploads/2021/10/Reports-Reader-Pim-Role.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2021/10/Reports-Reader-Pim-Role.png" alt="Reports Reader Pim Role" width="1150" height="387" class="aligncenter size-full wp-image-3497" srcset="https://thesysadminchannel.com/wp-content/uploads/2021/10/Reports-Reader-Pim-Role.png?v=1634152444 1150w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Reports-Reader-Pim-Role-1024x345.png?v=1634152444 1024w, https://thesysadminchannel.com/wp-content/uploads/2021/10/Reports-Reader-Pim-Role-768x258.png?v=1634152444 768w" sizes="(max-width: 1150px) 100vw, 1150px" /></a> </p>
<h2>Conclusion</h2>
<p>So there you have it, we&#8217;re able to add Azure AD Roles Using PowerShell to ensure Just in time access with Privilege Identity Manegment (PIM).  Hopefully this was informative and you&#8217;re able to use it in your environment.</p>
<p>If you&#8217;re interested in more Powershell scripts, be sure to check out our personal <a href="https://thesysadminchannel.com/powershell/" rel="noopener" target="_blank">Powershell Gallery</a> full of real world, useful scripts just like this one.</p>
<p>The post <a href="https://thesysadminchannel.com/how-to-add-azure-ad-roles-using-powershell-with-pim/">How To Add Azure AD Roles Using PowerShell With PIM</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://thesysadminchannel.com/how-to-add-azure-ad-roles-using-powershell-with-pim/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3494</post-id>	</item>
	</channel>
</rss>
