-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPS1_Azure_Function_GetVMStatus_Restapi
More file actions
124 lines (83 loc) · 3.67 KB
/
Copy pathPS1_Azure_Function_GetVMStatus_Restapi
File metadata and controls
124 lines (83 loc) · 3.67 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
[cmdletbinding()]
Param(
$AzFunctionDisplayName = 'Az - Demo Function',
$AzFunctionHomePage = 'https://www.AZ-Funct-Demo.azurewebsites.net/GetAZVMStatus',
$AzLocation = 'UKWest',
$AzFunctionuris = 'https://www.AZ-Funct-Demo.azurewebsites.net/GetAZVMStatus',
$AZResourceGroupName = 'AZ-Demo-Function-RG'
)
Function Create-SecureString{
[cmdletbinding()]
Param(
)
## Create the secure String and key
$key = [byte[]]::new(32)
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($Key)
[Reflection.Assembly]::LoadWithPartialName("System.Web")
$Password = [System.Web.Security.Membership]::GeneratePassword(20,1)
$SecureString = Convertto-SecureString -asplaintext -String $Password -Force
$HashedValue = ConvertFrom-SecureString -Key $key $SecureString
$object = New-Object –TypeName PSObject –Prop(@{
'Key' = $Key;
'RawPassword' = $Password;
'Hash' = $HashedValue;
'SecureString' = $SecureString
})
return $object
}
## Sign into Azure.
Login-AzureRMAccount
## Create a new Resource Group to host your Function.
$ResourceGroupConfig = @{
Location = $AzLocation
Name = $AZResourceGroupName
}
$ResourceGroup = New-AzureRMResourceGroup @ResourceGroupConfig
## Create Storage Account for Azure Function
$StorageAccConfig = @{
Location = $AzLocation
Name = ($AZResourceGroupName.replace("RG","Stgacc").replace("-","").tolower())
ResourceGroupName = $AZResourceGroupName
SkuName = 'Standard_LRS'
}
New-AzureRmStorageAccount @StorageAccConfig
## creates the function app
$WebAppConfig = @{
Kind = "Functionapp"
Location = $AzLocation
Properties = @{test = "test"}
ResourceName = ($AZResourceGroupName.replace("RG","Function"))
ResourceType = "microsoft.web/sites"
ResourceGroupName = $AZResourceGroupName
Force = $true
}
New-AzureRmResource @WebAppConfig
## Create keyvault to store credentials securely.
$KeyVaultConfig = @{
Location = $AzLocation
ResourceGroupName = $AZResourceGroupName
VaultName = ($AZResourceGroupName).replace("RG","Vault")
}
New-AzureRmKeyVault @KeyVaultConfig
## creates a random password
$KeyAndSecureString = Create-SecureString
## Uploads secure string into credential store
$KeyVaultSecret = @{
Name = $AZResourceGroupName.replace("RG","Key")
SecretValue = $KeyAndSecureString.Securestring
VaultName = ($AZResourceGroupName).replace("RG","Vault")
}
$secret = Set-AzureKeyVaultSecret @KeyVaultSecret
$AZADApplication = New-AzureRmADApplication -DisplayName $AzFunctionDisplayName -HomePage $AzFunctionHomePage -IdentifierUris $AzFunctionuris -Password $KeyAndSecureString.Securestring
## Create the App Service Principal
$ServicePrincipleName = New-AzureRmADServicePrincipal -ApplicationId $AZADApplication.ApplicationId
##
New-AzureRmRoleAssignment -RoleDefinitionName 'Virtual Machine Contributor' -ServicePrincipalName ($ServicePrincipleName.ApplicationId)
##
$tennentID = (Get-AzureRmSubscription).TenantId
$webapp = Get-AzureRmWebApp -Name $AZResourceGroupName.Replace('RG','Function')
$webapp.SiteConfig.AppSettings.Add([Microsoft.Azure.Management.WebSites.Models.NameValuePair]::new("AzureAutomationAppID",($ServicePrincipleName.ApplicationId)))
$webapp.SiteConfig.AppSettings.Add([Microsoft.Azure.Management.WebSites.Models.NameValuePair]::new("AzureAutomationTennentID",($tennentID)))
$Connection = @{}
$webapp.SiteConfig.AppSettings |foreach-object{$Connection.Add($_.name,$_.Value)}
Set-AzureRmWebApp -Name $AZResourceGroupName.Replace('RG','Function') -ResourceGroupName $AZResourceGroupName -appsettings $Connection