Windowsazure PowerShell script scheduled to start shutdown Azure VM
Speaking of Windowsazure for the present is not a fresh topic, but for the function is still a little look forward to, after all, in the world of cloud services Windowsazure is still a rookie. As we all know, there are many ways we can operate the service operation on Windowsazure, such as: Portal page, PowerShell with Azure and azure pack, others are graphical interface operation, the operation is relatively simple, Let's talk about it today. Manage some of the services on Windows Azure with Windows Azure PowerShell commands, PowerShell operations commands and scripts I'm sure everyone's not going to be strangers. Files with the. ps1 extension are PowerShell script files. Actually speaking of PowerShell. I personally have a big opinion about Microsoft's PowerShell: Because what, different services need to install different PowerShell programs, such as: Windows System's own PowerShell program can only operate on the operating system and services, Exchange PowerShell (EMS) operates on Exchange-related services, and there is a Windows Azure PowerShell that cannot be common to each other, and if you take Windows Azure PowerShell, If the system comes with PowerShell that can be imported into a Windows Azure PowerShell module online, that would be OK, but for Microsoft IT seems to be separate, no more nonsense, into today's theme.
The initial goal was to save costs, why, because Microsoft's Azure service charges are quite high, although the cost is calculated in terms of unit or run time, but it is a large amount of overhead. But the premise is that a stable server is needed to define the task condition execution program through the system's own scheduled tasks, so I want to shut down and start the VMS on Windows Azure on a timed basis. If you want to use Windows Azure PowerShell to manage Windows Azure Services, you first need to download your Windows Azure account subscription file and then import it into a system that is running on Windows Azurepowershell. See below for specific methods:
We need to download the corresponding Windowsazure subscription first
Https://manage.windowsauzre.cn/publishsettings
650) this.width=650; "title=" clip_image002 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image002" src= "http://s3.51cto.com/wyfs02/M02/6F/29/wKioL1WToqzALQJZAAFjM6fapEw389.jpg" height= "338"/>
Then download and install Windowsazure PowerShell, then import the subscription file under Windowsazure PowerShell, Access www.windowsazure.cn---> Documents and Resources---> Azure Command line interface---->windows installation prompts you to download Windowsazure PowerShell
650) this.width=650; "title=" clip_image004 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image004" src= "http://s3.51cto.com/wyfs02/M02/6F/29/wKioL1WToqzCkeqiAAEKoha1ilw129.jpg" height= "318"/>
We can also view related documents for Windows PowerShell above
http://www.windowsazure.cn/documentation/articles/install-configure-powershell/
Specific methods can be viewed inside the Web page
Install after download, follow the prompts to install PowerShell
650) this.width=650; "title=" clip_image006 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image006" src= "http://s3.51cto.com/wyfs02/M00/6F/29/wKioL1WToq2TSxWrAADkE0qSce4549.jpg" height= "342"/>
Import-azurepublishsettingsfile custom.publishsettings
Files for use by modules
650) this.width=650; "title=" clip_image008 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image008" src= "http://s3.51cto.com/wyfs02/M01/6F/29/wKioL1WToq3QDkNAAAD7Zom_9tY166.jpg" height= "343"/>
Because I have already imported it, the following information will be prompted for import again
650) this.width=650; "title=" clip_image010 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image010" src= "http://s3.51cto.com/wyfs02/M02/6F/2A/wKioL1WToq3CTsppAAEmWPb4omI620.jpg" height= "319"/>
If you import multiple subscription files in Windowsazure PowerShell, we need to select the default
We first pass
Get-azuresubscrpit
To view the imported subscription files under the current PowerShell
650) this.width=650; "title=" clip_image012 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image012" src= "http://s3.51cto.com/wyfs02/M00/6F/2C/wKiom1WToOny40HzAAEG4us-ilc592.jpg" height= "309"/>
We found that there are two, so we need to set the default by the following command
Select-azuresubscript-subsciptionname "xxxxx"-default
650) this.width=650; "title=" clip_image014 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image014" src= "http://s3.51cto.com/wyfs02/M00/6F/2A/wKioL1WToq2g6HGsAAB294aOXjI283.jpg" height= "/>"
Then we can
Get-azurevm
View all of the currently subscribed VM information
650) this.width=650; "title=" clip_image016 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image016" src= "Http://s3.51cto.com/wyfs02/M02/6F/2A/wKioL1WToq3grE34AABzzs5DklM399.jpg" height= "98"/>
We can use this PowerShell to start the virtual machine
Start-azurevm
Starts a Windows Azure virtual machine.
Parameter Set:byname
START-AZUREVM [-servicename] <String> [-name] <String> [<commonparameters>]
Parameter Set:input
START-AZUREVM [-servicename] <String>-vm <PersistentVM> [<CommonParameters>]-Name<String>
Specifies the name of the virtual machine to start.
Aliases |
None |
Required? |
True |
Position? |
2 |
Default Value |
None |
Accept Pipeline Input? |
True |
Accept Wildcard characters? |
False |
-servicename<string>
Specifies the name of the Windows Azure service, that contains, the virtual machine to start.
Aliases |
None |
Required? |
True |
Position? |
1 |
Default Value |
None |
Accept Pipeline Input? |
True |
Accept Wildcard characters? |
False |
-vm<persistentvm>
Specifies a virtual machine object this identifies the virtual machine to start.
Aliases |
None |
Required? |
True |
Position? |
2 |
Default Value |
None |
Accept Pipeline Input? |
True |
Accept Wildcard characters? |
False |
There are two ways we can start a virtual machine that needs to be started:
1. Start-azurevm-servicename "MyService"-name "MyVM" 2. Get-azurevm-servicename "Myservice1"-name "Centos7" | Start-azurevm
650) this.width=650; "title=" clip_image018 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image018" src= "http://s3.51cto.com/wyfs02/M00/6F/2A/wKioL1WToq3wJdOeAAB9bStv5zs751.jpg" height= "/>"
650) this.width=650; "title=" clip_image020 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image020" src= "http://s3.51cto.com/wyfs02/M00/6F/2C/wKiom1WToOmiaCZdAAB9E3zWWxg000.jpg" height= "/>"
650) this.width=650; "title=" clip_image022 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image022" src= "http://s3.51cto.com/wyfs02/M01/6F/2A/wKioL1WToq7D8jtEAADCO6jmD7U843.jpg" height= "167"/>
We look through Azure port
650) this.width=650; "title=" clip_image024 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image024" src= "http://s3.51cto.com/wyfs02/M01/6F/2C/wKiom1WToOzyO-KvAADHHvo9HrQ577.jpg" height= "368"/>
Again and again: we can stop with the following command
1.
Stop-azurevm-servicename "MyService"-name "MyVM"
This command shuts the "MyVM" virtual machine running in the "Myservice1" Windows Azure service.
2.
Get-azurevm-servicename "Myservice1"-name "Centos7" | Stop-azurevm
This command retrieves the virtual machine object for the virtual machine whose name is "MyVM". Then, it shuts down and deprovisions the virtual machine. If the virtual machine was the last one in a cloud service and the virtual machine was not shut down until your confirm this Act Ion because doing so releases the public virtual IP address associated with the cloud service.
3.
Stop-azurevm-servicename "Myservice1"-name "MyVM"-stayprovisioned
This command shuts the "MyVM" virtual machine running in the "Myservice1" cloud service and keeps the virtual machine Provisioned.
650) this.width=650; "title=" clip_image026 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image026" src= "http://s3.51cto.com/wyfs02/M02/6F/2A/wKioL1WTorGDIb9hAAC5J0ktdo8881.jpg" height= "148"/>
650) this.width=650; "title=" clip_image028 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image028" src= "http://s3.51cto.com/wyfs02/M02/6F/2C/wKiom1WToO2SRuiJAAD3xdNlxCQ510.jpg" height= "175"/>
650) this.width=650; "title=" clip_image030 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image030" src= "http://s3.51cto.com/wyfs02/M00/6F/2C/wKiom1WToO3hGNgqAAC6vFmQ83w961.jpg" height= "348"/>
Update-azurevm
Updates a Windows Azure virtual machine with the Modications-a virtual machine object.
UPDATE-AZUREVM [-servicename] <String> [-name] <String>-vm <PersistentVM> [<commonparameters ;]
1.
Get-azurevm-servicename "MySvc1"-name "MyVM3" | Set-azurevmsize–instancesize "Medium" | Update-azurevm
This example changes the size of the ' virtual machine ' MyVM3, running in ' MySvc1 ', to ' Medium '.
2.
Get-azurevm-servicename "MySvc1"-name "MyVM3" | Add-azuredatadisk-createnew '-medialocation "https://MyaccountStore1.blob.core.azure.com/vhds/MyNewDisk.vhd" '- DISKSIZEINGB 128-disklabel "Data-128"-lun 0 ' | Update-azurevm
This example adds a new data disk to the Vsan "MyVM3", Running in "MySvc1".
Remove-azurevm
Removes a Windows Azure virtual machine.
REMOVE-AZUREVM [-servicename] <String> [-name] <String> [<commonparameters>]
1.
Remove-azurevm-servicename "MySvc1"-name "MyVM"
Restart-azurevm
Restarts a Windows Azure virtual machine.
Parameter Set:byname
RESTART-AZUREVM [-servicename] <String> [-name] <String> [<commonparameters>]
Parameter Set:input
RESTART-AZUREVM [-servicename] <String>-vm <PersistentVM> [<commonparameters>]
1. Restart-azurevm-servicename "Myservice1" –Name "MyVM" 2. Get-azurevm-servicename "Myservice1"-name "MyVM" | Restart-azurevm
650) this.width=650; "title=" clip_image032 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image032" src= "http://s3.51cto.com/wyfs02/M00/6F/2C/wKiom1WToO2AQgyLAABvlD0EwBQ360.jpg" height= "/>"
What if you need to start and close in bulk? We need to use the PowerShell script, see below:
Start virtual machine: Virtual machine Name: vm01,vm02
$vmNames = New-object System.collections.arraylist$vmnames.add ("Johnsonweb") $vmNames. ADD ("JOHNSONVM") foreach ($VM In GET-AZUREVM) {if ($vmNames. Contains ($VM. Name) {Start-azurevm-name $VM. Name-servicename $VM. ServiceName}}
To shut down the virtual machine:
$vmNames = New-object System.collections.arraylist$vmnames.add ("Johnsonweb") $vmNames. ADD ("JOHNSONVM") foreach ($VM In GET-AZUREVM) {if ($vmNames. Contains ($VM. Name) {Stop-azurevm-name $VM. Name-servicename $VM. Servicename-stayprovisioned}}
To start/close all virtual machines under a subscription:
To start a virtual machine:
GET-AZUREVM | Start-azurevm
To shut down the virtual machine:
GET-AZUREVM | stop-azurevm-stayprovisioned
The above operations need to think of intervention, if you want to achieve, timed shutdown and start windowsazure on the specified VM how to do it, in fact, the same principle is the same, that is, by writing PowerShell script through the system comes with the program program to execute scripts:
First, say: START-AZUREVM script:
Start-azurevm-servicename "Myservice1"-name "MyVM"
After you fill in the above code in the text file, save the modification with the. ps1 extension.
650) this.width=650; "title=" clip_image034 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image034" src= "http://s3.51cto.com/wyfs02/M01/6F/2C/wKiom1WToO3gK9aPAACrVOUwhWU278.jpg" height= "417"/>
650) this.width=650; "title=" clip_image036 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image036" src= "http://s3.51cto.com/wyfs02/M02/6F/2D/wKiom1WToO3jJ_YwAAC6gtRf8GU831.jpg" height= "287"/>
Besides, STOP-AZUREVM script:
Stop-azurevm-servicename "Myservice1"-name "MyVM"-stayprovisioned
After you fill in the above code in the text file, save the modification with the. ps1 extension.
650) this.width=650; "title=" clip_image038 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image038" src= "http://s3.51cto.com/wyfs02/M01/6F/2A/wKioL1WTorKDmFupAADvXAIQXDA866.jpg" height= "383"/>
Note: System security policy does not run all scripts by default, so you need to modify the PowerShell policy to modify
Set-executionpolicy-executionpolicy Unrestricted
Note: The above PowerShell scripts need to be running on Windowsazure for PowerShell instead of Windows PowerShell that comes with the Windows system. So if we need to execute the program through a scheduled task, we need to execute the PowerShell script file via bat or VBS call.
So we need to prepare a bat or VBS:
BAT Script
Set pscmdline= ' C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe-file d:\custom.ps1 '
Note: First, to specify the Windowsazure with PowerShell installation path (not the Windows PowerShell script path), we can right-click the Windowsazure PowerShell Script Property--Open the file path, Specify the PowerShell script path as well.
650) this.width=650; "title=" clip_image040 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image040" src= "http://s3.51cto.com/wyfs02/M01/6F/2A/wKioL1WTorKh5K23AABYkYCk4Fc618.jpg" height= "/>"
Finally, we can start the scheduled task program by running-taskschd.msc on a stable windows machine.
To create a task:
650) this.width=650; "title=" clip_image042 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image042" src= "http://s3.51cto.com/wyfs02/M02/6F/2A/wKioL1WTorKDUB_fAAD2tOxjWhY115.jpg" height= "282"/>
650) this.width=650; "title=" clip_image044 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image044" src= "http://s3.51cto.com/wyfs02/M00/6F/2A/wKioL1WTorKC4VCQAAFOwvehu2U547.jpg" height= "371"/>
650) this.width=650; "title=" clip_image046 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image046" src= "http://s3.51cto.com/wyfs02/M01/6F/2D/wKiom1WToO6zAewCAAE7RhKcLH0669.jpg" height= "/>"
650) this.width=650; "title=" clip_image048 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image048" src= "http://s3.51cto.com/wyfs02/M01/6F/2D/wKiom1WToO6T5VduAAE8j2ggqAk605.jpg" height= "358"/>
650) this.width=650; "title=" clip_image050 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px, "border=" 0 "alt=" clip_ image050 "src=" http://s3.51cto.com/wyfs02/M02/6F/2A/wKioL1WTorLCK9TRAAE46mh2w8w845.jpg "height=" 315 "/>
We also create a scheduled task for STOP-AZUREVM: the same way
Http://www.pstips.net/bat-recieve-ps-result.html
The first is to create a Stop-azurevm.ps1 PowerShell script, and then create a Stop-azurevm.bat script to invoke the script that executes STOP-AZUREVM, which we'll focus on in the next article, executed under Azure Automation windowsazure The VM is scheduled to shut down and start up.
This article from "Gao Wenrong" blog, reproduced please contact the author!
Windowsazure PowerShell script scheduled to start shutdown Azure VM