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

clip_image002

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

clip_image004

我们同样可以查看上面的windows powershell的相关文档

http://www.windowsazure.cn/documentation/articles/install-configure-powershell/

具体方法可以浏览网页内部

下载后就是安装,根据提示安装完powershell即可

clip_image006

1
Import-AzurePublishSettingsFile custom.publishsettings

文件以供模块使用

clip_image008

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

clip_image010

如果在windowsazure powershell中导入多个订阅文件的话,我们需要选择默认的

我们首先通过

1
get-azuresubscrpit

查看当前powershell下已导入的订阅文件

clip_image012

我们发现有两个,所以我们需要通过以下命令来设置默认的即可

1
Select-azuresubscript -subsciptionName  "xxxxx"  -default

clip_image014

然后我们可以

1
Get-azurevm

查看当前订阅性的所有vm信息

clip_image016

我们可以通过该powershell去启动该虚拟机

Start-AzureVM

Starts a Windows Azure virtual machine.

Parameter Set: ByName   

1
Start-AzureVM [-ServiceName] <String> [-Name] <String> [ <CommonParameters>]

Parameter Set: Input   

1
2
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
2
1. Start-AzureVM -ServiceName  "myservice"  -Name  "MyVM"
2. Get-AzureVM -ServiceName  "myservice1"  -Name  "Centos7"  | Start-AzureVM

clip_image018

clip_image020

clip_image022

我们通过azure port进行查看

clip_image024

同样依次类推:我们可以通过以下命令进行停止

1.

1
Stop-AzureVM -ServiceName  "myservice"  -Name  "MyVM"

This command shuts down the "MyVM" virtual machine running in the "myservice1" Windows Azure service.

2.

1
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.

1
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.

clip_image026

clip_image028

clip_image030

Update-AzureVM

Updates a Windows Azure virtual machine with the modications make to a virtual machine object.

1
Update-AzureVM [-ServiceName] <String> [-Name] <String> -VM <PersistentVM> [<CommonParameters>]

1.

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.

1
2
3
  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.

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
2
1. Restart-AzureVM -ServiceName  "myservice1"  –Name  "MyVM"
2. Get-AzureVM -ServiceName  "myservice1"  -Name  "MyVM"  | Restart-AzureVM

clip_image032

如果需要批量启动和关闭的话怎么做呢?我们需要使用到powershell脚本了,具体见下:

启动虚拟机: 虚拟机名称:VM01,VM02

1
2
3
4
5
6
7
8
9
10
$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
}
}

关闭虚拟机:

1
2
3
4
5
6
7
8
9
10
$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
}
}

启动/关闭一个订阅下的所有虚拟机:

启动虚拟机:

1
Get-AzureVM | Start-AzureVM

关闭虚拟机:

1
Get-AzureVM | Stop-AzureVM -StayProvisioned

以上操作都需要认为干预,如果想实现,定时关机及启动windowsazure上的指定VM怎么做呢,其实原理也是一样的,那就是通过编写powershell脚本来通过系统自带的计划任务程序执行脚本即可:

首先说说:start-azurevm 脚本:

1
Start-AzureVM -ServiceName  "myservice1"  -Name  "MyVM"

在文本文件内填写以上代码后,保存修改扩展名为.ps1即可

clip_image034

clip_image036

再说说stop-azurevm脚本:

1
Stop-AzureVM -ServiceName  "myservice1"  -Name  "MyVM"  -StayProvisioned

在文本文件内填写以上代码后,保存修改扩展名为.ps1即可

clip_image038

注:系统安全策略默认不运行所有脚本运行,所以需要修改powershell策略进行修改

1
set -executionpolicy -executionpolicy unrestricted

注:以上powershell脚本需要运行在windowsazure for powershell上而不是windows系统自带的windows powershell,所以我们如果需要通过计划任务去执行该程序需要通过bat或者vbs调用执行powershell脚本文件。

所以我们需要准备一个bat或者vbs:

bat脚本

1
set  pscmdline= 'C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -file d:\custom.ps1'

注:首先是要指定windowsazure with powershell安装路径(不是windows powershell脚本路径),我们可以通过右击windowsazure powershell脚本属性--打开文件路径即可,同时指定powershell脚本路径。

clip_image040

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

创建任务:

clip_image042

clip_image044

clip_image046

clip_image048

clip_image050

我们同样为stop-azurevm创建一个计划任务即可:方法同上

http://www.pstips.net/bat-recieve-ps-result.html

首先是创建一个stop-azurevm.ps1的powershell脚本,然后在创建一个stop-azurevm.bat脚本去调用执行stop-azurevm的脚本,我们在下一篇文章中着重介绍,在azure自动化下执行windowsazure下的vm定时关机及启动。




本文转自 高文龙 51CTO博客,原文链接:http://blog.51cto.com/gaowenlong/1669799,如需转载请自行联系原作者

相关文章
|
2月前
|
存储 安全 Windows
PowerShell系列(六):PowerShell脚本执行策略梳理
【2月更文挑战第1篇】PowerShell 脚本执行策略用于控制何时以及何种方式执行 PowerShell 脚
|
5月前
|
安全 API
Powershell脚本分析
Powershell脚本分析
|
3月前
|
运维 开发工具 Windows
PowerShell系列(五):PowerShell通过脚本方式运行笔记
【1月更文挑战第7天】方便迁移,比如在之前工作经验积累下来的运维脚本,可以保存下来。如果业务场景用的到的话,直接文件拷贝过来就可以运行。
|
4月前
|
前端开发 微服务 Windows
PowerShell 命令窗口执行 pnpm 命令报错 无法加载文件 pnpm.ps1,因为在此系统上禁止运行脚本
PowerShell 命令窗口执行 pnpm 命令报错 无法加载文件 pnpm.ps1,因为在此系统上禁止运行脚本
|
7月前
|
SQL 数据库
PowerShell 脚本必备命令
PowerShell 脚本必备命令
|
8月前
|
C# C++
PowerShell脚本中实现限时读取用户输入
突然想到之前倒腾PowerShell的时候实现了一个限时读取用户输入的函数
130 0
|
Windows
powershell配置anaconda及解决【无法加载文件C:\Users\xxx\Documents\WindowsPowerShell\profile.ps1,因为在此系统上禁止运行脚本】的问题
powershell配置anaconda及解决【无法加载文件C:\Users\xxx\Documents\WindowsPowerShell\profile.ps1,因为在此系统上禁止运行脚本】的问题
1430 0
|
资源调度
关于vscode,powershell运行yarn报错禁止运行脚本解决办法
关于vscode,powershell运行yarn报错禁止运行脚本解决办法
152 0
关于vscode,powershell运行yarn报错禁止运行脚本解决办法
|
资源调度
PowerShell yarn : 无法加载文件 C:\Users\Admin\AppData\Roaming\npm\yarn.ps1,因为在此系统因为在此系统上禁止运行脚本。
PowerShell yarn : 无法加载文件 C:\Users\Admin\AppData\Roaming\npm\yarn.ps1,因为在此系统因为在此系统上禁止运行脚本。
176 0
PowerShell yarn : 无法加载文件 C:\Users\Admin\AppData\Roaming\npm\yarn.ps1,因为在此系统因为在此系统上禁止运行脚本。

热门文章

最新文章