Automatically sign in to Azure with PowerShell

Source: Internet
Author: User
Tags new set

PowerShell is one of the best ways to manage Azure, and you can automate a lot of work by using PowerShell scripting. For example, for a virtual machine on Azure, you can set a timed shutdown and turn it on at the right time, which can reduce the uptime of your virtual machines and contribute to energy savings and emissions reductions.

The Azure modules in PowerShell provide us with different APIs, and early APIs are called ASM (Azure Service Manager). With the evolution of Azure, a new set of APIs called ARM (Azure Resource Management) has emerged. Here, we'll just show you how to use the APIs in arm to automate login 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 the login authentication information to a local file. When you set up automatic login in the script later, you just need to use this local file.

Let's take a look at the specific procedure.

first, log in using the Login-azurermaccount command

You need to check the current login status before logging in, you can check the resource group for indirect checking.

Execute command: Get-azurermresourcegroup

650) this.width=650; "title=" image "src=" http://images2015.cnblogs.com/blog/139239/201606/ 139239-20160628105501468-484857552.png "alt=" image "Width=" 685 "height=" "border=" 0 "style=" margin:0px;padding:0 Px;border:0px;background-image:none; "/>

If you are not currently logged in, the query fails and prompts us to log in.

Execute command: Login-azurermaccount

Log in via the popup dialog box:

650) this.width=650; "title=" clip_image002 "src=" http://images2015.cnblogs.com/blog/139239/201606/ 139239-20160628105503577-111596567.jpg "alt=" clip_image002 "width=" 690 "height=" "border=" 0 "style=" margin:0px; Padding:0px;border:0px;background-image:none; "/>

After successful login, the account information will be displayed:

650) this.width=650; "title=" clip_image003 "src=" http://images2015.cnblogs.com/blog/139239/201606/ 139239-20160628105504796-609505948.png "alt=" clip_image003 "width=" 689 "height=" "border=" 0 "style=" margin:0px; Padding:0px;border:0px;background-image:none; "/>

Then execute the get-azurermresourcegroup command again.

650) this.width=650; "title=" clip_image005 "src=" http://images2015.cnblogs.com/blog/139239/201606/ 139239-20160628105506124-1592751652.jpg "alt=" clip_image005 "width=" 695 "height=" "border=" 0 "style=" margin:0px; Padding:0px;border:0px;background-image:none; "/>

Well, the previous error message is gone, and the output is a list of Resource Group.

Here, you are already logged in successfully.

Second, save the login information to the file

The Save-azurermprofile command can save the login information of the current session to a file so that other sessions can be automatically logged in using this file.

Execute command: Save-azurermprofile-path "D:\test\myprofile.json"

Myprofile.json is an ordinary text file in which only the authentication information is encrypted and 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.

third, automatically sign in to Azure

With the Select-azurermprofile command, you can load the user's login information from a file and set the execution context for Azure.

Execute command: Select-azurermprofile–path "D:\test\myprofile.json"

The result of the execution is the same as running the Login-azurermaccount command:

650) this.width=650; "title=" clip_image006 "src=" http://images2015.cnblogs.com/blog/139239/201606/ 139239-20160628105507890-1491926006.png "alt=" clip_image006 "width=" 699 "height=" 187 "border=" 0 "style=" margin:0px; Padding:0px;border:0px;background-image:none; "/>

An example of an automatic restart of a virtual machine

The following is a complete example of restarting a virtual machine on Azure:

$profile = "Your profile path" $resourceGroupName = "Your resource Group name" $vmName = "Your vm name" $logfile = "Log file Name

# Custom Log Method

function logwrite{    param  ([string] $logstring)      $now  = Get-Date     $logcontent  =  $now. ToShortDateString ()  +     +  $now. Toshorttimestring ()  +  ": "  +  $logstring          Add-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 restart is done!


This article is from the "Grape City Control Technology Team Blog" blog, declined to reprint!

Automatically sign in to Azure 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.