Azure Fundamentals: Automatic login with PowerShell

Source: Internet
Author: User
Tags new set

PowerShell is the best way to manage Azure because we can use scripting to automate a lot of work. For example, put the virtual machine on Azure and turn it on at the right time, so we can save some money, and of course we also contribute to reducing carbon dioxide emissions!

The Azure modules in PowerShell provide us with different APIs, which were earlier called ASM (Azure Service Manager). With the evolution of Azure, a new set of APIs called ARM (Azure Resource Management) has emerged. We'll just show you how to use the APIs in ARM to automatically sign in and manipulate resources on Azure.

The general idea of using PowerShell to automatically sign in to Azure is that you first use the login command to log in using the interactive interface, and then use the Save-azurermprofile command to save your login credentials to a local file. You can use this local file as soon as you are automatically logged in in the script. Let's take a look at the specific operating procedures below.

log in using the Login-azurermaccount command

Check the current login status before logging in, and we will do this indirectly by querying the Resource Group.
Execute command: Get-azurermresourcegroup

Because there is no login, the query fails and prompts us to sign in.

Execute command: Login-azurermaccount
Log in via the popup dialog box:

Your account information will be displayed when the login is successful:

Okay, now let's do the Get-azurermresourcegroup command again.

The previous error message has been lost, and the result of the output is a list of Resource Group.

to save the login information to a file

The Save-azurermprofile command can save the login information of the current session to a file, which can be used for automatic login in the other session.
Execute command: Save-azurermprofile-path "D:\test\myprofile.json"
Myprofile.json is an ordinary text file, only the authentication information is encrypted, the other information is readable.

Note, be sure to protect the generated Myprofile.json file, if leaked out and others get your account password is the same.

automatically sign in to Azure

The select-azurermprofile command loads the user's login information from the file and sets the execution context for Azure.
Select-azurermprofile–path "D:\test\myprofile.json"
The execution results are the same as we run the Login-azurermaccount command:

An example of an automatic restart of a virtual machine

We complete a complete example by restarting a virtual machine on Azure:

$profile="your profile path"$resourceGroupName="your resource group name"$vmName="your VM name"$logfile="log file name"#Custom Logging MethodsFunctionlogwrite{Param([string]$logstring)    $now= get-Date$logcontent=$now. ToShortDateString () +" "+$now. Toshorttimestring () +": "+$logstringADD-content$logfile-value$logcontent}logwrite ("before Select-azurermprofile.") Select-azurermprofile-path$profileLogwrite ("After Select-azurermprofile.") Logwrite ("before RESTART-AZURERMVM.") Restart-azurermvm-resourcegroupname$resourceGroupName-name$vmNameLogwrite ("After RESTART-AZURERMVM.")

Well, a simple automated job is done!

Azure Fundamentals: Automatic login with PowerShell

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.