PowerShell is one of the best ways to manage Azure, by using PowerShell scripts to automate a lot of work. For example, the virtual machine on Azure, you can set a timed shutdown operation, and at the appropriate time to turn it on, so that the virtual machine can reduce the running time, but also to contribute to energy saving and emission reduction.
The PowerShell Azure module provides us with a different API, an early API called ASM (Azure Service Manager). With the development of Azure, there is a new set of APIs called ARM (Azure Resource Management). Here, we'll just describe how to use the APIs in arm to automate login and manipulate resources on Azure.
The general idea of using PowerShell to automatically log on to Azure is to use the login command to log in interactively under an 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 your script later, just use this local file.
Let's take a look at the specific procedure. First, use the Login-azurermaccount command to log in
You need to check the current login status before you log in, and you can check resource group for indirect inspection.
Execute command: Get-azurermresourcegroup
If you are not currently logged on, the query fails and prompts us to log in.
Execute command: Login-azurermaccount
Log in by the dialog box that pops up:
Account information is displayed after successful login:
And then execute the get-azurermresourcegroup command again.
OK, the previous error message is gone and the output is a list of Resource Group.
Here, you have logged in successfully. Second, save the login information to the file
The Save-azurermprofile command can save the login information for the current session to a file so that other sessions can also use this file for automatic login.
Execute command: Save-azurermprofile-path "D:\test\myprofile.json"
Myprofile.json is an ordinary text file in which only authenticated information is encrypted and all other information is readable.
Note that it is important to protect the generated Myprofile.json file, if leaked out and others get your account password is the same. Three, automatic login Azure
The Select-azurermprofile command allows you to load the user's logon information from a file and set the execution context for Azure.
Execute command: Select-azurermprofile–path "D:\test\myprofile.json"
The results of the execution are the same as those of the Run Login-azurermaccount command:
An example of an automatic restart of a virtual machine
The following is a complete example of a virtual machine on the restart Azure:
$profile = "Your profile path"
$resourceGroupName = "Your resource group name"
$vmName = "Your VM name"
$LOGFI Le = "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 $profile
logwrite ("after Select-azurermprofile.")
Logwrite ("before Restart-azurermvm.")
Restart-azurermvm-resourcegroupname $resourceGroupName-name $vmName
logwrite ("after RESTART-AZURERMVM.")
Well, a simple automated restart is done.