-
Notifications
You must be signed in to change notification settings - Fork 15
/
StartAzureRMVM.txt
55 lines (38 loc) · 1.49 KB
/
StartAzureRMVM.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#开机脚本 leizha@microsoft.com
#设置用户名密码
$userName = "[YourOrgID]"
#设置密码
$securePassword = ConvertTo-SecureString -String "[Azure密码]" -AsPlainText -Force
#设置订阅名称
$subscriptionName= '[YourSubscription]'
#设置需要开机的虚拟机名称,以分号分隔
$VMNamesArray='VM01;VM02;VM03'
#资源组名称
$ResourceGroupName='YourResourceGroupName'
$cred = New-Object System.Management.Automation.PSCredential($userName, $securePassword)
Add-AzureRMAccount -Credential $cred -Environment AzureChinaCloud
Select-AzureRmSubscription -SubscriptionName $subscriptionName | Select-AzureRmSubscription
$VMNames = $VMNamesArray -split ";"
$jobs = @()
Foreach ($VMName in $VMNames)
{
$VM= Get-AzureRMVM | Where-Object {($_.ResourceGroupName -eq $ResourceGroupName) -and ($_.Name -eq $VMName)}
#判断VM是否存在
if($VM)
{
#"VM Name " + $VM.Name + " is Existing in Resource Group " + $ResourceGroupName
#Get VM Status
$vmStatus = (Get-AzureRMVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name -Status).Statuses.DisplayStatus[1]
#判断VM的开关机状态
if($vmStatus -eq 'VM deallocated')
{
$jobs += Start-AzureRMVM -ResourceGroupName $vm.ResourceGroupName -Name $VM.Name -ErrorAction Continue -asJob
}
}
else
{
"VM Name " + $VM.Name + " is NOT Existing in Resource Group " + $ResourceGroupName + " Please check the configuration"
}
}
$jobs | Wait-Job | Remove-Job -Force
"All VMs are Started"