How to stop/start an azure vm using service principle through powershell ?

JyotiPrakash Mallick 1 Reputation point
2021-12-15T04:43:15.523+00:00

Hello! Iam a new azure user and wanted to ask, is there a way to stop, restart or start an Azure VM using the service principle through PowerShell?

As a service principle I have

User Name, Secrets , Application Id, Object Id, Directory Id

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,014 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Ravi Kanth Koppala 3,391 Reputation points Microsoft Employee Moderator
    2021-12-15T05:14:13.013+00:00

    @JyotiPrakash Mallick ,
    Azure PowerShell supports several authentication methods. The easiest way to get started is with Azure Cloud Shell, which automatically logs you in. You can create an Azure service principal with Azure PowerShell and the below articles explain the different methods to achieve it.
    https://learn.microsoft.com/en-us/powershell/azure/authenticate-azureps?view=azps-7.0.0
    https://learn.microsoft.com/en-us/powershell/azure/create-azure-service-principal-azureps?view=azps-7.0.0

    Here is the link that explains the AZ commands we can use to start or stop the Azure VM.
    https://learn.microsoft.com/en-us/azure/virtual-machines/windows/ps-common-ref

    Hope this helps.

    1 person found this answer helpful.

  2. suvasara-MSFT 10,076 Reputation points Moderator
    2021-12-15T11:32:48.873+00:00

    @JyotiPrakash Mallick , Try this.

       $pscredential = New-Object System.Management.Automation.PSCredential -ArgumentList $AzureApplicationID, $passwordNEW  
    

    Note: This is not a secure method to work with credentials. Instead, convert the password to readable text and store it on your hidden files and call the file with location in your automation script.

    Edit: To implement security while calling creds,

       $secureStringText = $passwordNEW | ConvertFrom-SecureString   
         
       Set-Content "home/username/password.txt" $secureStringText  
         
       $pwdTxt = Get-Content "home/username/password.txt"  
         
       $securePwd = $pwdTxt | ConvertTo-SecureString  
         
       $credObject = New-Object System.Management.Automation.PSCredential -ArgumentList $$AzureApplicationID, $securePwd  
    

    ----------

    Please do not forget to "Accept the answer" wherever the information provided helps you to help others in the community.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.