Azure Runbook - GetAzurermlog - powershell

I am unable to run the ‘Get-Azurermlog’ in Azure runbook.I get the error "The term 'Get-Azurermlog' is not recognized as the name of a cmdlet, function, script file, or operable program". It a standard powershell cmdlet and works fine on my laptop PS console. Please advise.
workflow Write-SB
{
$Cred = Get-AutomationPSCredential -Name ‘Cre’
Add-AzureAccount -Credential $Cred
Select-AzureSubscription -SubscriptionName “My subscription”
InlineScript
{
Get-Azurermlog }
}

You have to install Azure Powershell.
You can follow the instructions here:
https://azure.microsoft.com/en-us/documentation/articles/powershell-install-configure/

Related

Connect-AzAccount : The term 'Connect-AzAccount' is not recognized as the name of a cmdlet, function, script file, or operable program

I am trying to execute the following PowerShell script in Azure DevOps pipeline by using PowerShell task with inline mode.
$clientId= "xxxxxxxxx"
$clientSecret= "xxxxxxx"
$subscriptionId= "xxxxxxxx"
$tenantId= "xxxxxxxxxxxxx"
# sign in
Write-Host "Logging in...";
$SecurePassword = $clientSecret | ConvertTo-SecureString -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $clientId, $SecurePassword
Connect-AzAccount -ServicePrincipal -Credential $cred-Tenant $tenantId
# set azure context with subscriptionId
Set-AzContext -SubscriptionId $subscriptionId
# select subscription
Write-Host "Selecting subscription '$subscriptionId'";
Select-AzSubscription -SubscriptionId $subscriptionId;
But I am getting the following error:
Connect-AzAccount : The term 'Connect-AzAccount' 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.
It is possible that the module this command belongs to - 'Az' isn't present/have to be imported. In which case,
Case-1
Open Powershell as Administrator
Install module - Install-Module Az
Import-Module Az
Your command - Connect-AzAccount should work now.
For case-2
Import module using - Import-Module Az.Accounts
For me this was the issue - AzureRM and AZ both were installed.
In Windows PowerShell, check that you have AzureRM installed:
Get-InstalledModule -name AzureRM
use command Uninstall-AzureRM to remove it.
If above command doesn't work use below one
Get-Module -ListAvailable | Where-Object {$_.Name -like 'AzureRM*'} | Uninstall-Module
Next ->
Set executionPolicy to RemoteSigned for powershell
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Next ->
Install the Az PowerShell Module in Windows PowerShell
Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force
Next ->
type Connect-AzAccount and complete the signing flow.
I would recommend you to switch to AzurePowershellTask as you find there preinstalled modules:
You can also try install modules on your own as it is shown here but this is pointless since you can leverage existing task.
In my case, AZ wasn't successfully installed because some AZ modules were already installed with AzureRM. I added the parameter -AllowClobber and now all the AZ modules are now installed.
Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -AllowClobber
Uninstalling AzureRM with command Uninstall-AzureRM may also be a great solution, because you're not going to use AzureRM anymore. Microsoft is going to stop supporting it sometimes in February 2024.
Try using
Login-AzAccount
instead of
Connect-AzAccount

Trying to Log in to Azure in Powershell

I am following the MS guide located here and everything goes through correctly until I have to actually log in. The pop up window appears for my creds and validates them, but powershell doesn't seem to notice.
So it goes Install Module, Import Module, Verify Version, Log in. See below for what happens on the last two steps.
PS C:\WINDOWS\system32> Get-Module AzureRM -ListAvailable | Select-Object -Property Name,Version,Path
Name Version Path
---- ------- ----
AzureRM 6.3.0 C:\Program Files\WindowsPowerShell\Modules\AzureRM\6.3.0\AzureRM.psd1
PS C:\WINDOWS\system32> Connect-AzureRmAccount
Account :
SubscriptionName :
SubscriptionId :
TenantId :
Environment :
PS C:\WINDOWS\system32>
Of course, this prevents me from doing very much else with Azure from that point forward.
Edit: Issue appears on multiple workstations
I got this same issue. I have two users like many of you: the Azure user that is the "Work" account, and then the "Personal" account which is also created automatically by Office365 and Azure. I was getting the issue when I tried to use my "personal" account, in which I have some subscriptions added (delegated).
After trying lots, what worked for me was to login to the "Work" account when the "Connect-AzureRmAccount" command asks for an username/password. Then, I again use the command "Connect-AzureRMAccount", but this time I entered the personal account, and it finally worked.
Picture here
Edit: A better way I found later was this, as I manage a lot of subscriptions/tenants from a single account (delegated access):
Put the “tenantid” into a variable (you can get this ID on the Azure Portal, in my case, on the option to change directories):
How to get your tenant's IDs quickly
$tenantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
Login specifying the TenantId:
Login-AzureRmAccount -TenantId $tenantId
Example 2
This behavior happens when you run
Clear-AzureRMContext -Scope CurrentUser
I'm not sure why and im attempting to debug on how to fix the issue. A work around is to close the powershell window and reopen a new powershell windows that does not have this command ran.
Running the command
Enable-AzureRmContextAutosave -Scope CurrentUser
Fixed the issue for me. This will autosave your context for every powershell session. If this is not desired you can run the command
Enable-AzureRmContextAutosave -Scope Process
which will save the azure context for only the process. Otherwise you will need to handle
You can try this...
Install-Module PoweshellGet -Force
Set-ExecutionPolicy -ExicutionPolicy Remotesigned
Install-Module AzureRm
Import-Module -Name AzureRm
Login-AzureRmAccount
You can use the below link to install latest PowerShell version:
https://github.com/Azure/azure-powershell/releases
And then use something like this in order to automatically pass in the username password, and skipping the UI:
$azureAccountName ="enter username here"
$azurePassword = ConvertTo-SecureString "password here" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($azureAccountName, $azurePassword)
Login-AzureRmAccount -Credential $psCred
Have you tried the following:
Import-Module Microsoft.Powershell.Security
$azureAccountName ="enter username here"
$azurePassword = ConvertTo-SecureString "password here" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($azureAccountName, $azurePassword)
$decrypt = $psCred.GetNetworkCredential()
$ptpass = $decrypt.Password
Write-Output "Logging in to Azure using $azureAccountName with a password of $ptpass"
Login-AzureRmAccount -Credential $psCred
If you receive an error with this code, please comment with the entire error message.
The only thing i can think of is the below
You maybe using Azure Service Management (Azure V1 / Azure Classic) which uses a different module to Azure Resource Manager (ARM , Azure V2).
To install Azure Service Management Module:
Install-Module Azure -AllowClobber
Allowing clobber because you already have AzureRM Module installed
Import Azure Module to PowerShell:
Import-Module Azure
Logs into ASM:
Add-AzureAccount
Shows you all subscriptions
Get-AzureSubscriptions
Selects the Subscription you allocate to work within
Select-AzureSubscription
Answered something similar in the below Thread:
Login-AzureRmAccount return subscription but Get-AzureSubscription return empty
Hope this helps
You can try logging in using the Service Principal credentials.
Service principal is an application created under Active Directory to which you can apply permission rules.
$pscredential = Get-Credential
Connect-AzureRmAccount -ServicePrincipal -ApplicationId "http://my-app" -Credential $pscredential -TenantId $tenantid
Refer here for more details.

Update-AzureRmEventGridSubscription in VSTS is not recognized as the name of a cmdlet

during the VSTS deploy I am calling the task Azure PowerShell (Preferred Azure PowerShell Version 5.1.1) where I am checking the Event Grid subscriptions and I am adding or updating:
$subscriptions = Get-AzureRmEventGridSubscription -ResourceGroupName $ResourceGroupName -TopicName $EventGridTopicName | WHERE EventSubscriptionName -eq $Subscription.Name
if (!$subscriptions)
{
Write-Host 'Add new subscription'
New-AzureRmEventGridSubscription -ResourceGroup $ResourceGroupName -TopicName $EventGridTopicName -Endpoint $Subscription.endpoint -EventSubscriptionName $Subscription.Name
Write-Host 'New subscription added'
}
else {
Write-Host 'Update endpoint'
Update-AzureRmEventGridSubscription -ResourceGroup $resourceGroupName -TopicName $eventGridTopicName -Endpoint $endpoint -EventSubscriptionName $eventGridSubscriptionName
Write-Host 'Subscription endpoint updated'
}
Running this code from local machine works fine.
Running this as a part of VSTS deploy throws exception:
[error]The term 'Update-AzureRmEventGridSubscription' 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.
[EDIT]
[section]Starting: Azure PowerShell script: New-EventGridSubscription
Task : Azure PowerShell
Description : Run a PowerShell script within an Azure environment
Version : 2.0.11
Author : Microsoft Corporation
Help : More Information
[command]Import-Module -Name C:\Modules\AzureRm_5.1.1\AzureRM\5.1.1\AzureRM.psd1 -Global
[command]Add-AzureRMAccount -ServicePrincipal -Tenant * -Credential System.Management.Automation.PSCredential -Environment AzureCloud
[command] Select-AzureRMSubscription -SubscriptionId -TenantId
[command]& 'D:\a\r1\a\Tools\scripts\New-EventGridSubscription.ps1' -ResourceGroupName -FunctionApps -EventGridTopicName *
[error]The term 'Update-AzureRmEventGridSubscription' 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: New-EventGridSubscription
Refer to these steps:
Add PowerShell task
Script:
Install-PackageProvider -Name NuGet -Force -Scope CurrentUser
Get-Module -ListAvailable| where {$_.Name -Like "*AzureRM*"} | Select Name, Version
Install-Module -Name AzureRM -RequiredVersion 6.0.1 -Force -Scope CurrentUser -AllowClobber
Import-Module AzureRM -Force -Verbose -Scope Local
Get-Module -ListAvailable| where {$_.Name -Like "*AzureRM*"} | Select Name, Version
Edit your Azure PowerShell task (Azure PowerShell Version: Specify other version; Preferred Azure PowerShell Versioin: 6.0.1)
Related thread: Upgrade AzureRM Powershell on Hosted 2017 Agent (VSTS - Visual Studio Team Services)

Azure PowerShell automation "no default subscritpion has been designated"

I am getting the following error. I am in fact setting the default subscription name.
4/27/2015 10:28:28 AM, Error: Get-AzureVM : No default subscription
has been designated. Use Select-AzureSubscription -Default
to set the default subscription. At test:9 char:9
+
+ CategoryInfo : CloseError: (:) [Get-AzureVM], ApplicationException
+ FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.GetAzureVMCommand
Here is my code:
workflow test
{
# Initial set up
$Cred = Get-AutomationPSCredential -Name "******"
Add-AzureAccount -Credential $Cred
Select-AzureSubscription -Default -SubscriptionName 'Beebunny'
$vmName = "MyMachineName"
Get-AzureVM -servicename $vmName
Write-output "All done."
}
If I try Select-AzureSubscription -Default 'SubscriptionName' it throws an error saying the syntax is invalid.
Edit: I have also tried Select-AzureSubscription -SubscriptionName 'SubscriptionName' without the Default flag.
Funny thing is that if I run this in AzurePS directly from Windows, it runs just fine. I am about 95% sure this is an Azure bug but wanted to get a second opinion first.
What version of the Azure module do you have loaded? Are you using the default module provided by the Automation service? Also, have you imported any other modules to this subscription?
Try creating a clean runbook with the following code, replacing the credential and subscription with the proper names. Can you get the credential and authenticate successfully?
workflow Test-GetVM
{
$Cred = Get-AutomationPSCredential -Name 'AdAzureCred'
if(!$Cred) {
Throw "Could not find an Automation Credential Asset named. Make sure you have created one in this Automation Account."
}
$Account = Add-AzureAccount -Credential $Cred
if(!$Account) {
Throw "Could not authenticate to Azure. Make sure the user name and password are correct."
}
Select-AzureSubscription -SubscriptionName "Visual Studio Ultimate with MSDN"
Get-AzureVM
}
UPDATE: Do you have the Resource Manager module loaded to the subscription as well?
I had the same problem and the solution was execute Add-AzureAccount, do the login process requested and once done all was working.

How to run Azure automation runbook in power shell

I am trying to run 'Connect-AzureVM' by importing it from the gallery. But no service or VM are created as I get these errors in job 'History'. The credential 'xyz' and subscription name 'ABC' both exist; I don't know why it's throwing an error.
workflow m1
{
$Cred = Get-AutomationPSCredential -Name "xyz"
Add-AzureAccount -Credential $Cred
InlineScript {
Select-AzureSubscription -SubscriptionName "ABC"
Get-AzureVM | select InstanceName
}
}
I am getting these errors:
Error: System.Management.Automation.ParameterBindingValidationException: Cannot bind argument to parameter 'Credential' because it is null
Error: The subscription named 'ABC' cannot be found. Use Set-AzureSubscription to initialize the subscription data.
It appears $Cred is null in the above runbook. Are you sure you have created the credential "xyz" as an Automation credential asset in the automation account where this runbook is running?
If you add the lines:
$CredIsNull = $Cred -eq $Null
Write-Output $Cred
Write-Output $CredIsNull
When you run the runbook, what does it output for $Cred and $CredIsNull?