Skip to content

Commit 00301a5

Browse files
committed
Initial testing
1 parent 00ecf0b commit 00301a5

5 files changed

Lines changed: 388 additions & 1 deletion

File tree

PSModuleDevelopment/changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## 2.2.11.100 (2021-12-17)
4+
5+
36
## 2.2.10.128 (2021-11-11)
47

58
+ New: Action: deployModule - Deploys a module to the target computer(s)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function Convert-PSMDSecureString {
2+
<#
3+
.SYNOPSIS
4+
Short description
5+
6+
.DESCRIPTION
7+
Long description
8+
9+
.PARAMETER pass
10+
Parameter description
11+
12+
.EXAMPLE
13+
PS C:\> Convert-PSMDSecureString
14+
15+
An example
16+
17+
.NOTES
18+
General notes
19+
#>
20+
[OutputType([string])]
21+
[CmdletBinding()]
22+
param (
23+
[Parameter(Mandatory=$true)]$pass
24+
)
25+
26+
$ssPtr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass)
27+
try {
28+
return [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ssPtr)
29+
}
30+
finally {
31+
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ssPtr)
32+
}
33+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
function Publish-PSMDModuleToAutomationAccount {
2+
<#
3+
.SYNOPSIS
4+
Short description
5+
6+
.DESCRIPTION
7+
Will be used
8+
Long description
9+
10+
.PARAMETER AutomationAccountSubscriptionId
11+
Parameter description
12+
13+
.PARAMETER AutomationAccountResourceGroupName
14+
Parameter description
15+
16+
.PARAMETER AutomationAccountName
17+
Parameter description
18+
19+
.PARAMETER ModuleLink
20+
Parameter description
21+
22+
.PARAMETER ModuleName
23+
Parameter description
24+
25+
.EXAMPLE
26+
PS C:\> Publish-PSMDModuleToAutomationAccount
27+
28+
An example
29+
30+
.NOTES
31+
General notes
32+
#>
33+
[OutputType([bool])]
34+
[CmdletBinding()]
35+
param (
36+
[Parameter(Mandatory=$true)][string]$AutomationAccountSubscriptionId = "ee5f8667-c5cd-4990-b756-8360bf2be985",
37+
[Parameter(Mandatory=$true)][string]$AutomationAccountResourceGroupName = "BASF_RG_RoleAssign_FirstWave",
38+
[Parameter(Mandatory=$true)][string]$AutomationAccountName = "BASF-AA-RoleAssign-FirstWave",
39+
[Parameter(Mandatory=$true)][uri]$ModuleLink = "https://samoduletesting.blob.core.windows.net/testcontainer2/RbacAutomationModule_2021-12-16_15-29-30.zip??sv=2020-08-04&ss=b&srt=sco&sp=rwlactfx&se=2021-12-31T22:58:06Z&st=2021-12-16T14:58:06Z&spr=https&sig=Il7PEsc9gdOrRGVS5lcqhTDAxXfb34uhbodYV7LwgLY%3D", #"https://samoduletesting.blob.core.windows.net/testcontainer2/RbacAutomationModule_2021-12-16_15-29-30.zip?sp=r&st=2021-12-17T10:56:40Z&se=2021-12-31T18:56:40Z&spr=https&sv=2020-08-04&sr=b&sig=C1gruzVxC%2Fqn6fHDVZwqqyjeEKACkddjdy5iUUqlma0%3D",
40+
[Parameter(Mandatory=$true)][string]$ModuleName = "ModuleTesting2"
41+
)
42+
43+
$AutomationAccountSubscriptionId = "ee5f8667-c5cd-4990-b756-8360bf2be985"
44+
$AutomationAccountResourceGroupName = "BASF_RG_RoleAssign_FirstWave"
45+
$AutomationAccountName = "BASF-AA-RoleAssign-FirstWave"
46+
[uri]$ModuleLink = "https://samoduletesting.blob.core.windows.net/testcontainer2/RbacAutomationModule_2021-12-16_15-29-30.zip?sv=2020-08-04&ss=b&srt=sco&sp=rwlactfx&se=2021-12-31T22:58:06Z&st=2021-12-16T14:58:06Z&spr=https&sig=Il7PEsc9gdOrRGVS5lcqhTDAxXfb34uhbodYV7LwgLY%3D"
47+
$ModuleName = "ModuleTesting2"
48+
49+
# https://docs.microsoft.com/en-us/azure/automation/shared-resources/modules#import-modules-by-using-powershell
50+
51+
try {
52+
$null = Set-AzContext -SubscriptionId $StorageAccountSubscriptionId `
53+
-ErrorAction Stop
54+
}
55+
catch {
56+
Throw "Please provide a valid tenant or a valid subscription"
57+
}
58+
59+
# https://docs.microsoft.com/en-us/powershell/module/az.automation/new-azautomationmodule?view=azps-7.0.0#example-1--import-a-module
60+
try {
61+
New-AzAutomationModule -AutomationAccountName $AutomationAccountName `
62+
-Name $ModuleName `
63+
-ContentLinkUri $ModuleLink `
64+
-ResourceGroupName $AutomationAccountResourceGroupName `
65+
-ErrorAction Stop
66+
}
67+
catch {
68+
Throw "New-AzAutomationModule"
69+
}
70+
}
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
function Write-PSMDFileToStorageAccount {
2+
<#
3+
.SYNOPSIS
4+
Short description
5+
6+
.DESCRIPTION
7+
This function will be used to copy the module to a StorageAccount blob container to further use it for Azure AutomationAccount or Azure Functions.
8+
9+
Long description
10+
11+
.PARAMETER Source
12+
Full path were the module is located as .zip or folder
13+
14+
Parameter description
15+
16+
.PARAMETER KeyVaultName
17+
Name of the KeyVault where the SasToken of the Storage Account is stored.
18+
Highly recommend to store the SasToken of the Storage Account in a Key Vault.
19+
20+
Parameter description
21+
22+
.PARAMETER KeyVaultSecretNameSasToken
23+
Name of the Secret where the SasToken is saved
24+
25+
Parameter description
26+
27+
.PARAMETER SasToken
28+
Instead of using a KeyVault for storing the SasToken in a secure way the SasToken can be inserted by this parameter.
29+
Not recommended, cause it is a string!
30+
31+
Parameter description
32+
33+
.PARAMETER StorageAccountSubscriptionId
34+
SubscriptionId where the StorageAccount is located
35+
36+
Parameter description
37+
38+
.PARAMETER StorageAccountName
39+
Name of the Storage Account
40+
41+
Parameter description
42+
43+
.PARAMETER StorageAccountContainer
44+
Name of the StorageAccount Container.
45+
Valid names start and end with a lower case letter or a number and has in between a lower case letter, number or dash with no consecutive dashes and is 3 through 63 characters long.
46+
By default, the inserted name will be converted to lowercase.
47+
48+
Parameter description
49+
50+
.PARAMETER StorageAccountContainerCreate
51+
If a container is not available and should be created with this function the parameter need to be $true
52+
The default parameter is $false
53+
54+
Parameter description
55+
56+
.PARAMETER StorageAccountBlobName
57+
Name of the StorageAccount Blob where the module will be copied.
58+
If nothing will be entered the file/folder-name (leaf) of the source-parameter will be used
59+
60+
Parameter description
61+
62+
.PARAMETER StorageAccountBlobOverwrite
63+
If a blob with the same name already exist it can be overwritten with setting the parameter to $true
64+
The default paramter is $false
65+
66+
Parameter description
67+
68+
.EXAMPLE
69+
PS C:\> Write-PSMDFileToStorageAccount
70+
71+
An example
72+
73+
.NOTES
74+
General notes
75+
#>
76+
[OutputType([bool])]
77+
[CmdletBinding()]
78+
param (
79+
[Parameter(Mandatory=$true)][string]$Source = "C:\Temp\RbacAutomationModule_2021-12-17_12-31-10.zip",
80+
81+
[Parameter(Mandatory=$false)][string]$KeyVaultName = "kvmoduletesting",
82+
[Parameter(Mandatory=$false)][string]$KeyVaultSecretNameSasToken = "SasToken",
83+
84+
[Parameter(Mandatory=$false)][string]$SasToken,
85+
[Parameter(Mandatory=$true)][string]$StorageAccountSubscriptionId = "ee5f8667-c5cd-4990-b756-8360bf2be985",
86+
[Parameter(Mandatory=$true)][string]$StorageAccountName = "samoduletesting",
87+
[Parameter(Mandatory=$true)][string]$StorageAccountContainer = "RbacAutomationModule",
88+
[Parameter(Mandatory=$false)][bool]$StorageAccountContainerCreate = $false,
89+
[Parameter(Mandatory=$false)][string]$StorageAccountBlobName = $(Split-Path $Source -leaf),
90+
[Parameter(Mandatory=$false)][bool]$StorageAccountBlobOverwrite = $false
91+
)
92+
93+
94+
$StorageAccountContainer = $StorageAccountContainer.ToLower()
95+
96+
try {
97+
$null = Set-AzContext -SubscriptionId $StorageAccountSubscriptionId `
98+
-ErrorAction Stop
99+
}
100+
catch {
101+
Throw "Please provide a valid tenant or a valid subscription"
102+
}
103+
104+
if($KeyVaultName -and $KeyVaultSecretNameSasToken) {
105+
try {
106+
$SasToken = Convert-PSMDSecureString -pass (Get-AzKeyVaultSecret -VaultName $KeyVaultName -Name $KeyVaultSecretNameSasToken -ErrorAction Stop).SecretValue
107+
}
108+
catch {
109+
Throw "Unable to receive the secret from the KeyVault"
110+
}
111+
}
112+
elseif (!($SasToken)) {
113+
Throw "SasToken is needed for the Storage Account context"
114+
}
115+
116+
# https://docs.microsoft.com/en-us/powershell/module/az.storage/new-azstoragecontext?view=azps-7.0.0#example-9--create-a-context-by-using-an-sas-token
117+
try {
118+
$StorageAccountContext = New-AzStorageContext -StorageAccountName $StorageAccountName `
119+
-SasToken $SasToken `
120+
-ErrorAction Stop
121+
}
122+
catch {
123+
Throw "New-AzStorageContext"
124+
}
125+
126+
# https://docs.microsoft.com/en-us/powershell/module/az.storage/get-azstoragecontainer?view=azps-7.0.0
127+
try {
128+
$StorageAccountBlob = Get-AzStorageContainer -Name $StorageAccountContainer `
129+
-Context $StorageAccountContext `
130+
-ErrorAction Ignore
131+
}
132+
catch {
133+
}
134+
finally {
135+
if($StorageAccountContainerCreate) {
136+
try {
137+
# https://docs.microsoft.com/en-us/powershell/module/az.storage/new-azstoragecontainer?view=azps-7.0.0#example-1--create-an-azure-storage-container
138+
$StorageAccountBlob = New-AzStorageContainer -Name $StorageAccountContainer `
139+
-Context $StorageAccountContext `
140+
-Permission Off `
141+
-ErrorAction Stop
142+
}
143+
catch {
144+
Throw "New-AzureStorageContainer"
145+
}
146+
}
147+
else {
148+
Throw "No container found or 'StorageAccountContainerCreate'-parameter not `$true"
149+
}
150+
}
151+
152+
# https://docs.microsoft.com/en-us/powershell/module/az.storage/get-azstorageblob?view=azps-7.0.0
153+
try {
154+
$StorageAccountBlob = Get-AzStorageBlob -Container $StorageAccountContainer `
155+
-Blob $StorageAccountBlobName `
156+
-Context $StorageAccountContext `
157+
-ErrorAction Ignore
158+
}
159+
catch {
160+
}
161+
finally {
162+
if(($StorageAccountBlob -and $StorageAccountBlobOverwrite) `
163+
-or `
164+
(!($StorageAccountBlob) -and !($StorageAccountBlobOverwrite)) `
165+
-or `
166+
(!($StorageAccountBlob) -and $StorageAccountBlobOverwrite)) {
167+
try {
168+
if(Test-Path -Path $Source -PathType Leaf) {
169+
if($StorageAccountBlob -and $StorageAccountBlobOverwrite) {
170+
# https://docs.microsoft.com/en-us/powershell/module/az.storage/set-azstorageblobcontent?view=azps-7.0.0#example-3--overwrite-an-existing-blob
171+
$StorageAccountBlob | Set-AzStorageBlobContent -File $Source `
172+
-Context $StorageAccountContext `
173+
-Force `
174+
-ErrorAction Stop
175+
}
176+
else {
177+
# https://docs.microsoft.com/en-us/powershell/module/az.storage/set-azstorageblobcontent?view=azps-7.0.0#example-1--upload-a-named-file
178+
Set-AzStorageBlobContent -Container $StorageAccountContainer `
179+
-File $Source `
180+
-Blob $StorageAccountBlobName `
181+
-Context $StorageAccountContext `
182+
-ErrorAction Stop
183+
}
184+
}
185+
elseif(Test-Path -Path $Source -PathType Container) {
186+
# https://docs.microsoft.com/en-us/powershell/module/az.storage/set-azstorageblobcontent?view=azps-7.0.0#example-2--upload-all-files-under-the-current-folder
187+
Get-ChildItem -Path $Source `
188+
-File `
189+
-Recurse `
190+
| Set-AzStorageBlobContent -Container $StorageAccountContainer `
191+
-Context $StorageAccountContext `
192+
-ErrorAction Stop
193+
}
194+
else {
195+
Throw "Unsupported source"
196+
}
197+
}
198+
catch {
199+
Throw "Set-AzStorageBlobContent"
200+
}
201+
}
202+
else {
203+
Throw "Unexpected behavior"
204+
}
205+
}
206+
}

0 commit comments

Comments
 (0)