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

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?

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
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
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"
Reference:
Set lifetimes for tokens - Microsoft Entra | Microsoft Docs

Related

The command 'Get-AzFunctionApp ' is not working in Azure Automation Runbook

I have created a Runbook and added below cmdlet
Get-AzFunctionApp | Stop-AzFunctionApp
I get below error
The term 'Get-AzFunctionApp' is not recognized as the name of a cmdlet,
I have imported all the below modules but no luck
After adding 'Az.Functions' I get below error
Failed
Cannot validate argument on parameter 'SubscriptionId'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. (The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.)
The cmdlet Get-AzFunctionApp / Stop-AzFunctionApp are included in this module: Az.Functions.
Please import this module and then the functions related operation will work.
Here is an example(please note that add the -Force parameter for Stop-AzFunctionApp), it works as per my testing:
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Connect-AzAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
Get-AzFunctionApp -ResourceGroupName xxx | Stop-AzFunctionApp -Force
From the screenshot you posted, it looks like you are missing the Az.Functions module that contains both the Get-AzFunctionApp and Stop-AzFunctionApp cmdlets.
To import Az.Functions module into your Automation account, do the following:
From your Automation account, under Shared Resources, select Modules.
Select Browse Gallery.
In the search bar, enter the module name (Az.Functions).
On the PowerShell Module page, select Import to import the module into your Automation account.
You can also import the module through the PowerShell Gallery. Choose the Azure Automation tab and select Deploy to Azure Automation from here: Az.Functions.

Powershell Script continues to ask me to use Select-AzureSubscription although I have called it

I have an Azure runbook where I am trying to deallocate VMs. When I run the runbook I get the error
Stop-AzureVM : No default subscription has been designated. Use Select-AzureSubscription -Default <subscriptionName> to
set the default subscription.
I have used the below in my script.
Add-AzureRmAccount
Select-AzureRMSubscription
After calling the select, it prints out
PSComputerName : localhost
PSSourceJobInstanceId :
Account :
Environment :
Subscription :
Tenant :
with the correct subscrption and tenant information so it seems the select is working correctly, but for some reason I still cannot use the Stop-AzureVM cmdlet.
Any ideas?
The command Stop-AzureVM is Azure Service Management PowerShell command. It just can be used to stop Azure classic VM. But the command Add-AzureRmAccount is Azure Resource Management PowerShell command. After running the command, we just can manage Azure Resource Management resources. For more details, please refer to here and here.
So with Azure ARM VM, please use the command Stop-AzureRmVM to stop it. Meanwhile, regarding how to stop Azure classic VM, please refer to the following steps
Create Azure Classic Run As Account
Script
$ConnectionAssetName = "AzureClassicRunAsConnection"
# Get the connection
$Conn = Get-AutomationConnection -Name $ConnectionAssetName
# Authenticate to Azure with certificate
$CertificateAssetName = $Conn.CertificateAssetName
$AzureCert = Get-AutomationCertificate -Name $CertificateAssetName
Set-AzureSubscription -SubscriptionName $Conn.SubscriptionName -SubscriptionId $Conn.SubscriptionID -Certificate $AzureCert
Select-AzureSubscription -SubscriptionId $Conn.SubscriptionID
#stop VM
Stop-AzureVM -ServiceName "ContosoService01" -Name "MyVM" -Force
Besides, regarding how to check if the VM is classic, please refer to the blog
Try Running the below :
Get-Module AzureRm.Profile -ListAvailable
This issue might occur when there is multiple instances of the module. If there are multiple instance remove the older modules and retain the new module.
To remove the old module : Uninstall-Module -Name AzureRm.Profile -RequiredVersion 4.6.0#(olderversion if you have any)

The term 'Register-AzResourceProvider' is not recognized as the name of a cmdlet

Hi I am trying to teach myself Azure and I'm following this guide: https://learn.microsoft.com/en-us/learn/modules/intro-to-governance/2-azure-policy. I'm on a windows 10 with $PSVersionTable.PSEdition = Desktop I messaged Microsoft support, but no one has responded. When I try to run
# Register the resource provider if it's not already registered
Register-AzResourceProvider -ProviderNamespace 'Microsoft.PolicyInsights'
I get
Register-AzResourceProvider : The term 'Register-AzResourceProvider' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that
the path is correct and try again.
I've checked off having Azure powershell installed
if ($PSVersionTable.PSEdition -eq 'Desktop' -and (Get-Module -Name AzureRM -ListAvailable)) {
Write-Warning -Message ('Az module not installed. Having both the AzureRM and ' +
'Az modules installed at the same time is not supported.')
} else {
Install-Module -Name Az -AllowClobber -Scope CurrentUser
}
Any help with this would be greatly appreciated.
To solve the issue, try to follow the steps below.
1.Open a new powershell session via Run as administrator, then run the command below.
Install-Module -Name Az -AllowClobber -Scope AllUsers -Force
2.After installing the module, close the administrator session and open a new normal powershell session, then login your user account which has the permission to register the provider, e.g. Owner of the subscription.
Connect-AzAccount
3.Then register the provider.
Register-AzResourceProvider -ProviderNamespace 'Microsoft.PolicyInsights'
Note: If the issue is still existing, use Get-Module to check if the Az.Resources module was imported in this powershell session(normally it will be imported automatiocally), if not, you could use Import-Module -Name Az.Resources -Force to import it manually.
The command you are trying to use is not a PowerShell command. The command you are looking for should begin with either a Get- or Set- prefix.

Powershell PSPKI module is not working in azure automation account

I've imported PSPKI modules in to azure automation account, when using the cmdlets of PSPKI I always get command not recognized. I could see the module is installed successfully and could see the cmdlets in my runbook, but somehow the cmdlets are not recognized.
for example Test-WebServerSSL -URL login.live.com
I can reproduce your issue, to fix the issue, run Import-Module -Name PSPKI first.
Import-Module -Name PSPKI
Test-WebServerSSL -URL login.live.com

Azure DevOps: Powershell 'Get-AzSubscription' is not recognized

I am deploying an ARM template from azure DevOps using Azure PowerShell as shown below.
This is subscription level deployment. I am getting below error.
The term 'Get-AzSubscription' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling
of the name, or if a path was included, verify that the path is
correct and try again.
Install-Module -Name Az -AllowClobber -Scope CurrentUser
Import-Module Az
$context = Get-AzSubscription -SubscriptionId xxxxxxxx
Set-AzContext $context
New-azdeployment -Name "SKL" -Location westeurope -TemplateFile .\delegatedResourceManagement.json -TemplateParameterFile .\delegatedResourceManagement.parameters.json
Logs:
##[section]Starting: Azure PowerShell script: InlineScript
==============================================================================
Task : Azure PowerShell
Description : Run a PowerShell script within an Azure environment
Version : 3.153.0
Author : Microsoft Corporation
Help : https://learn.microsoft.com/azure/devops/pipelines/tasks/deploy/azure-powershell
==============================================================================
##[command]Import-Module -Name C:\Modules\azurerm_6.7.0\AzureRM\6.7.0\AzureRM.psd1 -Global
##[command]Clear-AzureRmContext -Scope Process
##[command]Disable-AzureRmContextAutosave -ErrorAction Stop
##[command]Add-AzureRMAccount -ServicePrincipal -Tenant *** -Credential System.Management.Automation.PSCredential -Environment AzureCloud #processScope
##[command] Select-AzureRMSubscription -SubscriptionId xxxxxxx -TenantId ***
##[command]& 'd:\a\_temp\xxxxxxd.ps1'
##[warning]User declined to install module (Az).
##[error]The specified module 'Az' was not loaded because no valid module file was found in any module directory.
##[command]Disconnect-AzureRmAccount -Scope Process -ErrorAction Stop
##[command]Clear-AzureRmContext -Scope Process -ErrorAction Stop
##[error]The term 'Get-AzSubscription' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
##[section]Finishing: Azure PowerShell script: InlineScript
You need to specify the task version: 4.* (Preview) or higher to use the Az powershell module:
Also there is an Azure resource group deployment to deploy ARM template easily:
looks like too much confusion among all az modules, MSFT have messed up az new/old modules and are conflicting with each other. az account show will list all modules with AZ CLI
az account show
Search apps and Features - Uninstall the old April 2018 Azure
Follow something like this link https://blog.atwork.at/post/The-new-Azure-PowerShell-Az-module
Then you won't have issue with
Get-AzSubscription ( after using Connect)

Resources