Functions/New-ARMresource.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#Requires -Version 5.0
function New-ARMresource
{
<#
.SYNOPSIS
    Create a new ARM template resource
 
.DESCRIPTION
    Create a new ARM template resource
 
.PARAMETER Name
    The name of the parameter. This is Mandatory
 
.PARAMETER Type
    The parameter type. These are the types returned by Get-ARMresourceList
 
.EXAMPLE
    $newRes = @{
        APIversion = '2016-03-30'
        Name = 'MyVM'
        Location = 'EAST-US'
        Tags = @{tag=1}
        Comments = 'hey'
        DependsOn = @("item1","item2")
        SKU = @{value="skuvalue"}
        Kind = 'storage'
        Properties = @{prop1=1}
    }
 
    New-ARMresource @newRes -Type Microsoft.Compute/virtualMachines
 
.INPUTS
    String
 
.OUTPUTS
    PSCustomObject
 
.NOTES
    Author: Tore Groneng
    Website: www.firstpoint.no
    Twitter: @ToreGroneng
#>

[cmdletbinding()]
Param(
    [string]
    $APIversion
    ,
    [string]
    $Name
    ,
    [string]
    $Location
    ,
    [hashtable]
    $Tags
    ,
    [string]
    $Comments
    ,
    [string[]]
    $DependsOn
    ,
    [hashtable]
    $SKU
    ,
    [string]
    $Kind
    ,
    [hashtable]
    $Properties
    ,
    [hashtable]
    $Resources
)
DynamicParam
{
    $Dictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary

    $NewDynParam = @{
        Name = "Type"
        Alias = "ResourceName"
        Mandatory = $true
        ValueFromPipelineByPropertyName = $true
        ValueFromPipeline = $true
        DPDictionary = $Dictionary
    }

    $all = Get-ARMresourceList -ErrorAction SilentlyContinue
    
    if ($all)
    {
        $null = $NewDynParam.Add("ValidateSet",$all)
    }

    New-DynamicParam @NewDynParam
    $Dictionary
}

Begin
{
    $f = $MyInvocation.InvocationName
    Write-Verbose -Message "$f - START"
}

Process
{
    $ResourceName = $PSBoundParameters.Type

    $propHash = [ordered]@{
        PSTypeName = "ARMresource"
        apiVersion = $APIversion
        type = $ResourceName
    }

    if ($Name)
    {
        $propHash["name"] = $Name
    }

    if ($Location)
    {
        $propHash.location = $Location
    }

    if ($DependsOn)
    {        
        $propHash.dependsOn = $DependsOn
    }

    if ($Properties)
    {        
        $propHash.properties = $Properties
    }

    if ($Tags)
    {
        $propHash.tags = $Tags
    }

    if ($Comments)
    {
        $propHash.comments = $Comments
    }       
    
    if ($Resources)
    {        
        $propHash.resources = $Resources
    }

    if ($SKU)
    {        
        $propHash.SKU = $SKU
    }

    if ($Kind)
    {        
        $propHash.kind = $Kind
    }

    [PSCustomObject]$propHash
}
}