azureazure-active-directoryazure-cloud-servicesazure-cloud-shell

How to get New-AzureADPolicy to work in azure cloud shell


I was able to run

Install-Module -Name AzureADPreview

But when I run

$policy = New-AzureADPolicy -Definition @('{"TokenLifetimePolicy" {"Version":1,"AccessTokenLifetime":"02:00:00"}}')

It run into this error

New-AzureADPolicy: The term 'New-AzureADPolicy' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

How do I get this keyword to work in cloud shell?


Solution

  • I tried to reproduce the same in my environment and got the same error as below:

    $policy = New-AzureADPolicy -Definition @('{"TokenLifetimePolicy" {"Version":1,"AccessTokenLifetime":"02:00:00"}}') -DisplayName TestPolicy -IsTenantDefault
    

    enter image description here

    Please note that, before installing AzureADPreview you need uninstall AzureAD Module like below:

    Uninstall-Module AzureAD 
    Install-Module AzureADPreview 
    Import-Module AzureADPreview 
    Get-Module -Name AzureADPreview
    connect-AzureAd
    

    enter image description here

    I tried to create the policy by using the below command and got the results successfully like below:

    $policy = New-AzureADPolicy -Definition @('{"TokenLifetimePolicy":{"Version":1,"AccessTokenLifetime":"02:00:00"}}') -DisplayName "WebPolicyScenario" -IsOrganizationDefault $false -Type "TokenLifetimePolicy"
    

    enter image description here

    Reference:

    Set lifetimes for tokens - Microsoft Entra | Microsoft Docs