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定时关机及启动。

时间: 2024-08-06 03:44:35

WindowsAzure Powershell脚本定时启动关机Azure VM的相关文章

PowerShell批量启动/关闭Azure VM

备注:以下例子中出现的JohnsonWeb, JohnsonVm均是虚拟机的名称.在运行Powershell脚本之前,请导入您的订阅文件. 根据条件启动/关闭虚拟机,例如根据虚拟机名称,批量启动/关闭 启动虚拟机 $vmNames = New-Object System.Collections.ArrayList $vmNames.Add("JohnsonWeb") $vmNames.Add("JohnsonVm") foreach($vm in Get-Azure

windows关机前执行脚本设置与关机blat自动发送邮件脚本模板

1.开始--运行--gpedit.msc2.策略--计算机配置--Windows设置--脚本(启动/关机)3.右击"关机"4.添加--浏览:选择脚本确定:5.完成设置,重启测试. 注:关机脚本启动目录:C:\Windows\System32\GroupPolicy\Machine\Scripts\Shutdown\ set name=%date:~0,4%%date:~5,2%%date:~8,2%C:\automail\close.bat >>C:\automail\c%

云服务程序在启动的时候执行Powershell脚本

如果在云服务程序启动时候,需要执行Powershell脚本,我们需要将脚本嵌入到程序中,并且编写一个cmd来执行这个脚本,具体如下: 1.编写测试的Powershell脚本:每隔10分钟 检测dns $TimeStart = Get-Date $TimeEnd = $timeStart.addminutes(1440) $name = "cnppmedia.blob.core.chinacloudapi.cn." $result = "d:\nslookuplog.txt&q

PowerShell批量配置Azure VM端点

我们可以通过PowerShell命令:Add-AzureEndpoint来向虚拟机添加端点. 请参考以下脚本,批量添加虚拟机端点: 备注:该例子针对一个虚拟机,添加了三个端口: 端口名称 协议 公用端口 私有端口 MyPort1 tcp 5001 5001 MyPort2 tcp 5002 5002 MyPort3 udp 5003 5003 该例子中,云服务名称与虚拟机名称均为:JohnsonLinux.如果需要添加更多的端口,那么可以按照相应格式,将端口配置添加到$newVmEndpoint

通过PowerShell命令给Azure VM添加CustomScriptExtension

Azure的VM提供了一种管理工具叫Azure VM Extension.它实现了一些管理虚拟机所需要的重要功能,比如:重设密码.设置RDP参数.以及许多其他关键的功能,并且Azure VM一直在添加新的Extension.默认情况下,创建的Windows VM会安装以下几个基本的VM Extension,包括IaaSDiagnostics,VMAccess和BGInfo.Linux的VM,如果开启Diagnostics功能,默认会安装LinuxDiagnostic的Extension. 通过A

Azure PowerShell (9) 使用PowerShell导出订阅下所有的Azure VM的Public IP和Private IP

<Windows Azure Platform 系列文章目录> 笔者在之前的工作中,有客户提出想一次性查看Azure订阅下的所有Azure VM的Public IP和Private IP.笔者研究了一下Azure相关的PowerShell,可以实现.在这里笔者简单分享一下: 1.将http://files.cnblogs.com/files/threestone/ExportVMList.rar文件保存至本地目录 2.安装Windows Azure PowerShell工具,下载地址是:htt

如何使用PowerShell 收集Azure VM Image列表

这次来分享一下关于Azure的脚本,目前来讲在使用Azure的时候,新创建的一些资源基本上都已经在用ARM模式了,Azure classic portal现在也已经完全被new portal取代,在使用体验上来说ARM模式大部分情况下也被ASM更方便一些,但是也有一些细节的地方因为功能越来越强大,能支持的内容越来越多,有些时候平台本身提供的功能不一定能完全满足我们的需求,所以我们会自己写一些小脚本来尽量弥补 今天要分享的内容很简单,在使用ARM模式创建虚拟机时,决定我们要创建什么操作系统的其实是

任务计划不只是linux才有哦,巧用任务计划实现WIN定时开关机

网络上提供定时关机的方法和软件很多,而关于定时开机的方法和软件却少的可怜,本文将告诉大家在Windows系统下实现定时开机的三个方法. 第一个方法,通过主板BIOS计划任务实现定时开机. 主板上有实时时钟(Real Time Clock,RTC)负责系统的计时,我们可以通过RTC指定开机的时间,就像闹钟一样.不过,由于这项功能很少被人使用,部分虽然提供了此功能的主板(如INTEL原装主板)其实并不能在指定时间开机,所以用户在正式使用前最好先进行测试. 具体操作方式为: 1.电脑开机之后根据屏幕上

Windows Azure Virtual Machine (29) 修改Azure VM 数据磁盘容量

<Windows Azure Platform 系列文章目录> 当我们使用Windows Azure管理界面,创建Azure虚拟机的时候,默认挂载的磁盘是固定大小的 1.比如我创建1个Windows Server虚拟机 2.需要挂载额外的数据磁盘,一开始我初始化数据磁盘的容量为500G 3.经过一段时间以后,500GB容量不够了,我需要将这块数据磁盘扩容 注意:Azure A系列和D系列的虚拟机,磁盘容量是用多少算多少.比如我们挂载了1TB的数据磁盘,但是实际只使用1GB的存储空间. 微软只会