-
Notifications
You must be signed in to change notification settings - Fork 0
/
New-TableRunSchedule.ps1
89 lines (78 loc) · 3.29 KB
/
New-TableRunSchedule.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<#
.SYNOPSIS
Creates an Azure Storage Table
.DESCRIPTION
Creates an Azure Storage Table
.PARAMETER SubscriptionId
The Azure subscription ID that will contain the Storage Table.
If no value is provided, default subscription is used.
.PARAMETER TableRGName
The name of the resource group that will contain the Storage Account for the Storage Table.
The resource group must exist.
.PARAMETER TableSAName
The name of the storage account that will contain the Storage Table.
The storage account must exist.
.PARAMETER TableName
The name of the Storage Table to be created.
.PARAMETER ApplicationId
The ApplicationId of the Service Principal that will be granted Contributor role to the Storage Account.
If no value is provided, the permissions of the Storage Account will not be altered.
.EXAMPLE
New-TableRunSchedule.ps1 -SubscriptionId "########-####-####-####-############" -TableRGName "rg-runschedule-itea" -TableSAName farunschedulecta598 -TableName RunScheduleLog -ApplicationId "########-####-####-####-############"
.OUTPUTS
[string] New-AzureStorageTableSASToken FullUri output
#>
#Requires -Version 5
#Requires -modules AzureRM.Profile, AzureRM.Resources, ARMRunSchedule
[CmdletBinding()]
Param (
[Parameter(
Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
HelpMessage="The Azure subscription ID that will contain the Storage Table."
)]
[String] $SubscriptionId,
[Parameter(
Mandatory=$true,
ValueFromPipelineByPropertyName=$false,
HelpMessage="The name of the resource group that will contain the Storage Account for the Storage Table."
)]
[string] $TableRGName,
[Parameter(
Mandatory=$true,
ValueFromPipelineByPropertyName=$false,
HelpMessage="The name of the storage account that will contain the Storage Table."
)]
[string] $TableSAName,
[Parameter(
Mandatory=$true,
ValueFromPipelineByPropertyName=$false,
HelpMessage="The name of the Storage Table to be created."
)]
[string] $TableName,
[Parameter(
Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
HelpMessage="The ApplicationId of the Service Principal that will be granted Contributor role to the Storage Account."
)]
[string] $ApplicationId
)
Connect-AzureRM
if (-not ($PSBoundParameters.ContainsKey("SubscriptionId") ) ) {
Select-Subscription
}
$TenantId = (Get-AzureRmContext).Tenant.Id
$SubId = (Get-AzureRmContext).Subscription.Id
$saContext = (Get-AzureRmStorageAccount -ResourceGroupName $TableRGName -Name $TableSAName).Context
$ctx = Set-AzureRmCurrentStorageAccount -context $saContext
$table = Get-AzureStorageTable -Name $TableName -Context $saContext -ErrorAction SilentlyContinue
if ($table) {
Write-Debug ("{0} Table exists" -f $TableName)
}
else {
$table = New-AzureStorageTable -Name $TableName -Context $saContext
}
# Assign 'Storage Account Contributor' to the service principal
$app = Get-AzureRmADApplication -ApplicationId $ApplicationId
$sp = Get-AzureRmADServicePrincipal -SearchString $app.DisplayName
New-AzureRmRoleAssignment -ObjectId $sp.Id -RoleDefinitionName 'Storage Account Contributor' -ResourceName $TableSAName -ResourceType 'Microsoft.Storage/storageAccounts' -ResourceGroupName $TableRGName