您的位置:首页 > 运维架构 > Shell

WindowsAzure Powershell脚本定时启动关机Azure VM

2015-07-01 16:12 615 查看
WindowsAzure Powershell脚本定时启动关机Azure VM
说到windowsazure对于当下不是一个新鲜话题了,但是对于功能来说还是有点期待的,毕竟在云服务的世界里windowsazure还是一个菜鸟了。同样我们都知道,对于windowsazure上的服务操作我们有很多方式可以操作,比如:portal页面,powershell with azure及azure pack等,其他的都是图形界面操作,操作相对简单,今天咱们就说说通过windows azure powershell命令来管理windows azure上的部分服务,powershell操作命令及脚本相信大家都不去陌生了,扩展名为.ps1的文件为powershell脚本文件。其实说到powershell。我个人对微软的powershell有很大意见:因为什么呢,不同的服务需要安装不同的powershell程序,比如:Windows系统自带的powershell程序仅仅能对操作系统及服务进行操作,Exchange powershell(ems)对exchange相关服务进行操作管理,还有就是一个windows Azure powershell,他们之间不能互相通用,如果拿windows azure的powershell来说,如果系统自带的powershell能在线导入一个windows azure的powershell模块的话那样也行,但是对于微软来说貌似还是分开的,废话不多说了,进入今天的主题。
试验最初的目的是为了节省成本,为什么这么说呢,因为微软的azure服务收费相当高,尽管是以单位或者运行时间来计算成本,但是数量多了还是一笔不小的开销。但是前提还是需要一台稳定的服务器来通过系统自带的计划任务来定义任务条件执行程序,所以我想对windows azure上的vm进行定时关机及启动。如果要使用windows azure powershell来管理windows azure服务的话,首先需要下载windows azure账户订阅文件,然后导入到windows azurepowershell运行的系统中。具体方法见下:
我们需要首先下载对应的windowsazure 订阅
https://manage.windowsauzre.cn/publishsettings



然后下载并且安装windowsazure powershell,然后在windowsazure powershell下导入该订阅文件;访问www.windowsazure.cn ---> 文档和资源---> azure 命令行接口---->windows安装就会提示下载windowsazure powershell



我们同样可以查看上面的windows powershell的相关文档
http://www.windowsazure.cn/documentation/articles/install-configure-powershell/
具体方法可以浏览网页内部
下载后就是安装,根据提示安装完powershell即可



Import-AzurePublishSettingsFile custom.publishsettings
文件以供模块使用




因为我已经导入过了,所以再次导入会提示以下信息



如果在windowsazure powershell中导入多个订阅文件的话,我们需要选择默认的
我们首先通过
get-azuresubscrpit
查看当前powershell下已导入的订阅文件




我们发现有两个,所以我们需要通过以下命令来设置默认的即可
Select-azuresubscript -subsciptionName "xxxxx" -default



然后我们可以
Get-azurevm
查看当前订阅性的所有vm信息




我们可以通过该powershell去启动该虚拟机
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 that identifies the virtual machine to start.
Aliases
none
Required?
true
Position?
2
Default Value
none
Accept Pipeline Input?
true
Accept Wildcard Characters?
false
我们可以通过以下两种方式可以启动需要启动的虚拟机:
1. Start-AzureVM -ServiceName "myservice" -Name "MyVM"
2. Get-AzureVM -ServiceName "myservice1" -Name "Centos7" | Start-AzureVM









我们通过azure port进行查看



同样依次类推:我们可以通过以下命令进行停止
1.
Stop-AzureVM -ServiceName "myservice" -Name "MyVM"
This command shuts down 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 is the last one in a cloud service, the virtual machine is not shut down until you confirm this action 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 down the "MyVM" virtual machine running in the "myservice1" cloud service and keeps the virtual machine provisioned.









Update-AzureVM
Updates a Windows Azure virtual machine with the modications make to 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 virtual machine "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



如果需要批量启动和关闭的话怎么做呢?我们需要使用到powershell脚本了,具体见下:
启动虚拟机: 虚拟机名称: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
}
}
关闭虚拟机:
$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
}
}
启动/关闭一个订阅下的所有虚拟机:
启动虚拟机:
Get-AzureVM | Start-AzureVM
关闭虚拟机:
Get-AzureVM | Stop-AzureVM -StayProvisioned
以上操作都需要认为干预,如果想实现,定时关机及启动windowsazure上的指定VM怎么做呢,其实原理也是一样的,那就是通过编写powershell脚本来通过系统自带的计划任务程序执行脚本即可:
首先说说:start-azurevm 脚本:
Start-AzureVM -ServiceName "myservice1" -Name "MyVM"
在文本文件内填写以上代码后,保存修改扩展名为.ps1即可






再说说stop-azurevm脚本:
Stop-AzureVM -ServiceName "myservice1" -Name "MyVM" -StayProvisioned
在文本文件内填写以上代码后,保存修改扩展名为.ps1即可



注:系统安全策略默认不运行所有脚本运行,所以需要修改powershell策略进行修改
set-executionpolicy -executionpolicy unrestricted
注:以上powershell脚本需要运行在windowsazure for powershell上而不是windows系统自带的windows powershell,所以我们如果需要通过计划任务去执行该程序需要通过bat或者vbs调用执行powershell脚本文件。
所以我们需要准备一个bat或者vbs:
bat脚本
set pscmdline='C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -file d:\custom.ps1'
注:首先是要指定windowsazure with powershell安装路径(不是windows powershell脚本路径),我们可以通过右击windowsazure powershell脚本属性--打开文件路径即可,同时指定powershell脚本路径。



最后我们可以通过在一台运行稳定的windows机器上,开始运行-taskschd.msc,打开计划任务程序
创建任务:















我们同样为stop-azurevm创建一个计划任务即可:方法同上
http://www.pstips.net/bat-recieve-ps-result.html
首先是创建一个stop-azurevm.ps1的powershell脚本,然后在创建一个stop-azurevm.bat脚本去调用执行stop-azurevm的脚本,我们在下一篇文章中着重介绍,在azure自动化下执行windowsazure下的vm定时关机及启动。
本文出自 “高文龙” 博客,转载请与作者联系!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: