none
Powershell command not run RRS feed

  • Question

  • I create a runbook with below one Powershell command:

    Get-AzureRmVM -ResourceGroupName 'jrctradeweb' -Name 'testvm'

    Then it comes to error:

    Get-AzureRmVM : Run Login-AzureRmAccount to login.

    At line:1 char:1
    + Get-AzureRmVM -ResourceGroupName 'jrctradeweb' -Name 'testvm'
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [Get-AzureRmVM], PSInvalidOperationException
        + FullyQualifiedErrorId : InvalidOperation,Microsoft.Azure.Commands.Compute.GetAzureVMCommand

    Please help. Thanks.

    Joe

    Friday, October 11, 2019 2:08 AM

Answers

  • Go to the automation account
    Go to Modules under Shared Resources, search for AzureRM to check the modules your already got and their versions.
    Go to Modules gallery under Shared Resources
    Import AzureRM.Compute module (the required modules if you need for other runbooks). This will take some time and update the dependent modules. (make sure to check the option)
    Go to Modules under Shared Resources, it will show 'Azure modules have been updated' once complete.
    Go to your runbook, edit it and put your resource group name replacing myResourceGroup and save the runbook
    Go to Test pan then click start to test your runbook. You will get the following type result on completion (in my case there was no VM to show)

    Note: This will not run on a normal powershell console. If you want to run the script on a normal powershell console just make sure your you have right AzureRM module installed and add Connect-AzureRmAccount or Login-AzureRmAccount to login to Azure at the beginning to your script.

    https://docs.microsoft.com/en-us/powershell/azure/azurerm/install-azurerm-ps?view=azurermps-6.13.0

    Also, you can use the latest AZ module as well
    https://docs.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-2.7.0

    Thanks,


    Rohan Islam | Azure Architect

    • Proposed as answer by Rohan Islam Friday, October 11, 2019 12:39 PM
    • Marked as answer by UOG Admin Saturday, October 12, 2019 2:27 AM
    Friday, October 11, 2019 12:38 PM

All replies

  • Hello,

    From the error message, it is showing login error. Hence you might be getting that error.

    Please login to Azure using the command mentioned in the error, check this document for reference.

    Connect-AzAccount

    In case you are seeing same error even after logging in, then it could be issue with multiple versions of Azure Powershell on the machine. You can check with below command the versions

    Get-Module -ListAvailable

    If azure powershell modules show anywhere other than %programfiles (x86)%\MicrosoftSDKs\Azure\PowerShell, then remove them. Easy way is to unistall and reinsall the modules using the MSI installer or by below commands:

    Install-Module -Name AzureRM 
    Install-Module -Name Azure

    -----------------------------------------------------------------------------------------------------------------------

    If the suggested answer has helped you to solve your issue, do click on "Mark as Answer" and "Up-Vote" the answer that helped you for the benefit of the community.

    Thanks.


    Friday, October 11, 2019 5:15 AM
    Moderator
  • Did you mean Azure Automation Account runbook? If yes, you have to Add authentication to manage Azure resources, refer the link below.

    https://docs.microsoft.com/en-us/azure/automation/automation-first-runbook-textual-powershell

    Thanks,


    Rohan Islam | Azure Architect

    Friday, October 11, 2019 5:34 AM
  • Thanks Rohan.

    I try the code (as below) in your link.

    Disable-AzureRmContextAutosave –Scope Process
    $connection = Get-AutomationConnection -Name AzureRunAsConnection
    $logonAttempt = 0
    while(!($connectionResult) -And ($logonAttempt -le 10))
    {
        $LogonAttempt++
        $connectionResult =    Connect-AzureRmAccount `
                                   -ServicePrincipal `
                                   -Tenant $connection.TenantID `
                                   -ApplicationID $connection.ApplicationID `
                                   -CertificateThumbprint $connection.CertificateThumbprint
        Start-Sleep -Seconds 30
    }
    $AzureContext = Select-AzureRmSubscription -SubscriptionId $connection.SubscriptionID
    Get-AzureRmVM -ResourceGroupName myResourceGroup -AzureRmContext $AzureContext

    It show below errors. Do I need to import/use related cmdlet before these coding? how?

    Disable-AzureRmContextAutosave : The term 'Disable-AzureRmContextAutosave' 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.

    At line:2 char:1
    + Disable-AzureRmContextAutosave –Scope Process
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (Disable-AzureRmContextAutosave:String) [],CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    Connect-AzureRmAccount : The term 'Connect-AzureRmAccount' 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.
    At line:9 char:28
    +     $connectionResult =    Connect-AzureRmAccount `
    +                            ~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (Connect-AzureRmAccount:String) [], CommandNotFoundException

        + FullyQualifiedErrorId : CommandNotFoundException
    Thanks again.

    Friday, October 11, 2019 9:08 AM
  • Go to the automation account
    Go to Modules under Shared Resources, search for AzureRM to check the modules your already got and their versions.
    Go to Modules gallery under Shared Resources
    Import AzureRM.Compute module (the required modules if you need for other runbooks). This will take some time and update the dependent modules. (make sure to check the option)
    Go to Modules under Shared Resources, it will show 'Azure modules have been updated' once complete.
    Go to your runbook, edit it and put your resource group name replacing myResourceGroup and save the runbook
    Go to Test pan then click start to test your runbook. You will get the following type result on completion (in my case there was no VM to show)

    Note: This will not run on a normal powershell console. If you want to run the script on a normal powershell console just make sure your you have right AzureRM module installed and add Connect-AzureRmAccount or Login-AzureRmAccount to login to Azure at the beginning to your script.

    https://docs.microsoft.com/en-us/powershell/azure/azurerm/install-azurerm-ps?view=azurermps-6.13.0

    Also, you can use the latest AZ module as well
    https://docs.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-2.7.0

    Thanks,


    Rohan Islam | Azure Architect

    • Proposed as answer by Rohan Islam Friday, October 11, 2019 12:39 PM
    • Marked as answer by UOG Admin Saturday, October 12, 2019 2:27 AM
    Friday, October 11, 2019 12:38 PM
  • thanks Rohan.
    Saturday, October 12, 2019 2:27 AM
  • Anytime, happy to help :)

    Thanks,


    Rohan Islam | Azure Architect

    Saturday, October 12, 2019 4:54 AM