-
Notifications
You must be signed in to change notification settings - Fork 3
/
StartAllVM.ps1
60 lines (44 loc) · 1.83 KB
/
StartAllVM.ps1
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
######StartAllVM#####
<#
.DESCRIPTION
Start All VM under one subscription
#>
workflow StartAllVM
{
param(
#设置Org ID
[parameter(Mandatory=$true)]
[String]$AzureOrgId="[YourOrgID]",
#设置Org ID的密码
[Parameter(Mandatory = $true)]
[String]$Password="[YourPassword]",
#设置订阅名称
[Parameter(Mandatory = $true)]
[String]$AzureSubscriptionName="[YourSubscriptionName]"
)
$ChinaTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneByID("China Standard Time")
$Start = [System.TimeZoneInfo]::ConvertTimefromUTC((get-date).ToUniversalTime(),$ChinaTimeZone)
$day = $Start.DayOfWeek
if ($day -eq 'Saturday' -or $day -eq 'Sunday')
{
"Exit due to weekends"
exit
}
"Starting Operation at UTC+8 Time: " + $Start.ToString("HH:mm:ss.ffffzzz")
$AzurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$AzureOrgIdCredential = New-Object System.Management.Automation.PSCredential($AzureOrgId,$AzurePassword)
Add-AzureAccount -Credential $AzureOrgIdCredential -environment "AzureChinaCloud" | Write-Verbose
Select-AzureSubscription -SubscriptionName $AzureSubscriptionName
$VMS = Get-AzureVM
foreach($VM in $VMS)
{
if($VMS.Status -eq "StoppedDeallocated" -or $VMS.Status -eq "Stopped")
{
$VMName = $VM.Name
Start-AzureVM -ServiceName $VM.ServiceName -Name $VM.Name
Write-Output "Start VM :" + $VMName
}
}
$Finish = [System.TimeZoneInfo]::ConvertTimefromUTC((get-date).ToUniversalTime(),$ChinaTimeZone)
"Finished Operation at UTC+8 Time: " + $Finish.ToString("HH:mm:ss.ffffzzz")
}