-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStart Azure VM.ps1
71 lines (52 loc) · 2.25 KB
/
Start Azure VM.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#Region for ExecutionPolicy
# Get Execution Policy of the current process
$Script:ProcessEP = Get-ExecutionPolicy -Scope Process
#Get the value of the Execution Policy and save it in the Variable
$Script:ValueProcessEP = ($Script:ProcessEP).value__
# Check if the Execution Policy of the process is set to Unrestricted
if ($Script:ValueProcessEP -eq 0) {
# Write the message
Write-Output "Execution Policy is already set to Unrestricted for the Process"
# Check if the Execution Policy of the process is already set
}else{
# Set the ExecutionPolicy of the Process to Unrestricted
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted -Force -Confirm:$false
# Checks if the Execution Policy has been set
if ((Get-ExecutionPolicy -Scope Process).value__ -eq 0) {
# Write the message
Write-Output "Execution Policy is now set to Unrestricted for the Process"
}
}
#EndRegion for ExecutionPolicy
#Region Start Azure VM
# Import Module for Az PowerShell
Import-Module -Name Az
#Region assign variables
# Save accesskey to this Variable
$Script:UserName = "{azureusername.value}"
# Save secretkey to this variable
$Script:PasswordString = "{azurepassword.value}"
# List of VM names
$Script:AzureVmNames = {azurevmnames.value}
#EndRegion assign variables
#Region for Connection to Azure
# Set the password and convert it to secure string to the variable
$Script:Password = ConvertTo-SecureString $Script:PasswordString -AsPlainText -Force
# set the credentials to the variable
$Script:UserCredential = New-Object System.Management.Automation.PSCredential ($Script:UserName,$Script:Password)
# Connect using set credentials to Azure
Connect-AzAccount -Credential $Script:UserCredential
#EndRegion for Connection to Azure
# Loop through the hash table for the Names of the VM
foreach ($item in $Script:AzureVmNames) {
# Get the Azure VM
$Script:AzVm = Get-AzVM -VMName ($item)
# Write the message
Write-Output "The Azure VM '$($item)' in is starting....."
# Start the Azure VM
Start-AzVM -ResourceGroupName $Script:AzVm.ResourceGroupName -Name $Script:AzVm.Name -NoWait -Confirm:$false
}
#EndRegion Start Azure VM
#Region Disconnect the Azure session
Disconnect-AzAccount
#EndRegion Disconnect the Azure session