Skip to content

Commit

Permalink
Population of content
Browse files Browse the repository at this point in the history
  • Loading branch information
johnthebrit committed Feb 3, 2020
1 parent 3ceb0cc commit 7cd1d59
Show file tree
Hide file tree
Showing 8 changed files with 321 additions and 0 deletions.
Binary file added AzureADB2BWhiteboardNovember2019.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 64 additions & 0 deletions DeclarativevsImperative/ARM/CreateStorage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageAccountName": {
"type": "string",
"metadata": {
"description": "Specifies the name of the Azure Storage account."
}
},
"storageAccountType": {
"type": "string",
"defaultValue": "Standard_LRS",
"allowedValues": [
"Standard_LRS",
"Standard_GRS",
"Standard_ZRS",
"Premium_LRS"
],
"metadata": {
"description": "Storage Account type"
}
},
"containerName": {
"type": "string",
"defaultValue": "media",
"metadata": {
"description": "Specifies the name of the blob container."
}
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Specifies the location in which the Azure Storage resources should be deployed."
}
}
},
"resources": [
{
"name": "[parameters('storageAccountName')]",
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2018-07-01",
"location": "[parameters('location')]",
"kind": "StorageV2",
"sku": {
"name": "[parameters('storageAccountType')]"
},
"properties": {
"accessTier": "Hot"
},
"resources": [
{
"name": "[concat('default/', parameters('containerName'))]",
"type": "blobServices/containers",
"apiVersion": "2018-07-01",
"dependsOn": [
"[parameters('storageAccountName')]"
]
}
]
}
]
}
15 changes: 15 additions & 0 deletions DeclarativevsImperative/ARM/CreateStorage.parameters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageAccountName": {
"value": "savtecharmstorage2020"
},
"storageAccountType": {
"value": "Standard_LRS"
},
"containerName": {
"value": "images"
}
}
}
53 changes: 53 additions & 0 deletions DeclarativevsImperative/DemoScript.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
New-AzResourceGroup -Location southcentralus -Name 'RG-SCUSPSStorage'
New-AzResourceGroup -Location southcentralus -Name 'RG-SCUSARMStorage'
New-AzResourceGroup -Location southcentralus -Name 'RG-SCUSTFStorage'

#Define base
$GitBasePath = 'C:\Users\john\OneDrive\projects\GIT\RandomStuff\DeclarativevsImperative'

#ARM Template Demo

#Deploy simple template creating a storage account
New-AzResourceGroupDeployment -ResourceGroupName RG-SCUSARMStorage `
-TemplateFile "$GitBasePath\ARM\CreateStorage.json" `
-TemplateParameterFile "$GitBasePath\ARM\CreateStorage.parameters.json"

#Run same template again but override the type of the storage account
New-AzResourceGroupDeployment -ResourceGroupName RG-SCUSARMStorage `
-TemplateFile "$GitBasePath\ARM\CreateStorage.json" `
-TemplateParameterFile "$GitBasePath\ARM\CreateStorage.parameters.json" `
-StorageAccountType 'Standard_GRS'

#Run same template again but override the name of account
New-AzResourceGroupDeployment -ResourceGroupName RG-SCUSARMStorage `
-TemplateFile "$GitBasePath\ARM\CreateStorage.json" `
-TemplateParameterFile "$GitBasePath\ARM\CreateStorage.parameters.json" `
-storageAccountName 'savtecharmstoragev22020'
#Note since I didn't override the account type its back to LRS again!
#Old account still there since it's not definied in the template so ignored

New-AzResourceGroupDeployment -ResourceGroupName RG-SCUSARMStorage `
-TemplateFile "$GitBasePath\ARM\CreateStorage.json" `
-TemplateParameterFile "$GitBasePath\ARM\CreateStorage.parameters.json" `
-storageAccountName 'savtecharmstoragev22020' `
-mode complete
#now its gone since complete and RG must match the template


#Terraform Demo
#Install from https://www.terraform.io/downloads.html and added to user path
#Azure CLI installed and logged in via az login

Set-Location $GitBasePath\Terraform

terraform init
terraform plan
terraform apply -auto-approve

terraform plan -var 'replicationType=GRS'
terraform apply -var 'replicationType=GRS' -auto-approve

#To visually see
terraform graph > base.dot
# could sent directly with graphviz installed https://graphviz.gitlab.io/download/
terraform graph | dot -Tsvg > graph.svg
49 changes: 49 additions & 0 deletions DeclarativevsImperative/PowerShell/CreateStorage.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#Variables
$accountName = 'savtechpsstorage2020'
$rgName = 'RG-SCUSPSStorage'
$storageSKU = 'Standard_LRS'
$location = 'southcentralus'
$storAccount = $null
$storContext = $null

#Check for the storage account
try {$storAccount = Get-AzStorageAccount -ResourceGroupName $rgName `
-Name $accountName -ErrorAction 0}
catch {write-output "Not found"}

if($null -eq $storAccount)
{


#Create the storage account
Write-Output "Creating account"
$storAccount = New-AzStorageAccount -ResourceGroupName $rgName `
-Name $accountName `
-Location $location `
-SkuName $storageSKU `
-Kind StorageV2


}

else #Check its the right type
{
Write-Output "Account already exists"
if($storAccount.SkuName -ne $storageSKU) #if not fix it
{
Write-Output "Changing account type"
Set-AzStorageAccount -ResourceGroupName $rgName `
-Name $accountName `
-SkuName $storageSKU
}
}



$storContext = New-AzStorageContext -StorageAccountName $storAccount.StorageAccountName -UseConnectedAccount


#Would need all the same checks here if it already exists before creation etc
#Create the container
New-AzStorageContainer -Name "images" `
-Context $storContext
17 changes: 17 additions & 0 deletions DeclarativevsImperative/Terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
data "azurerm_resource_group" "ResGroup" {
name = "RG-SCUSTFStorage"
}

resource "azurerm_storage_account" "StorAccount" {
name = "savtechtfstorage2020"
resource_group_name = data.azurerm_resource_group.ResGroup.name
location = data.azurerm_resource_group.ResGroup.location
account_tier = "Standard"
account_replication_type = var.replicationType
}

resource "azurerm_storage_container" "ContName" {
name = "images"
storage_account_name = azurerm_storage_account.StorAccount.name
container_access_type = "private"
}
4 changes: 4 additions & 0 deletions DeclarativevsImperative/Terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
variable "replicationType" {
type = string
default = "LRS"
}
119 changes: 119 additions & 0 deletions Install-Patch/Install-Patch.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
function Install-Patch
{
<#
.SYNOPSIS
Patches a WIM or VHD file
.DESCRIPTION
Applies downloaded patches to a VHD or WIM file
.NOTES
File Name: Install-Patch.psm1
Author: John Savill
Requires: Tests on PowerShell 3 on Windows Server 2012
Copyright (c) 2013 John Savill
.LINK
http://www.savilltech.com
.PARAMETER updateTargetPassed
File (WIM, VHD or VHDX) to be patched
.PARAMETER patchpath
Path containing the updates
.EXAMPLE
Install-Patch d:\files\test.vhd d:\updates\win2012\
Install-Patch d:\files\install.wim:4 d:\updates\win2012\
#>

[cmdletbinding()]
Param(
[Parameter(ValuefromPipeline=$false,Mandatory=$true)][string]$updateTargetPassed,
[Parameter(ValuefromPipeline=$false,Mandatory=$true)][string]$patchpath)

#$updateTargetPassed = "G:\Temp\Win2012DatacenterRTM.vhdx"
#or
#$updateTargetPassed = "d:\sources\install.wim:4"
#$patchpath = "D:\software\Windows 2012 Updates\"

if(($updateTargetPassed.ToLower().Contains(".vhd")) -eq $true) # if its VHD or VHDX. Contains is case sensitive so have to convert to lower when comparing
{
$isVHD = $true
}
else
{
$isVHD = $false
}

if($isVHD)
{
$updateTarget=$updateTargetPassed
if ((Test-Path $updateTarget) -eq $false) #if not found
{
write-output "Source not found ($updateTarget)"
break
}
else
{
mount-vhd -path $updateTarget
$disks = Get-CimInstance -ClassName Win32_DiskDrive | where Caption -eq "Microsoft Virtual Disk"
foreach ($disk in $disks)
{
$vols = Get-CimAssociatedInstance -CimInstance $disk -ResultClassName Win32_DiskPartition
foreach ($vol in $vols)
{
$updatedrive = Get-CimAssociatedInstance -CimInstance $vol -ResultClassName Win32_LogicalDisk |
where VolumeName -ne 'System Reserved'
}
}
$updatepath = $updatedrive.DeviceID + "\"
}
}
if(!$isVHD) #its a WIM file
{
#Need to extract the WIM part and the index
#extract file name and the index number
$updateTargetPassedSplit = $updateTargetPassed.Split(":")
if($updateTargetPassedSplit.Count -eq 3) #one for drive letter, one for folder and one for image number so would have been two colons in it c:\temp\install.wim:4
{
$updateTarget = $updateTargetPassedSplit[0] + ":" + $updateTargetPassedSplit[1] #There are two colons. The first is drive letter then the folder!
$updateTargetIndex = $updateTargetPassedSplit[2]
$updatepath = "c:\wimmount\"

#check if exists and if not create it
if ((Test-Path $updatepath) -eq $false) #if not found
{
Write-Host "Creating folder " + $updatepath
New-Item -Path $updatepath -ItemType directory
#could have also used [system.io.directory]::CreateDirectory($updatepath)
}

# Mount it as folder
#dism /get-wiminfo /wimfile:install.wim
dism /Mount-Wim /wimfile:$updateTarget /index:$updateTargetIndex /mountdir:$updatepath
}
else
{
write-output "Missing index number for WIM file. Example: c:\temp\install.wim:4"
break
}
}

# For WIM or VHD
$updates = get-childitem -path $patchpath -Recurse | where {($_.extension -eq ".msu") -or ($_.extension -eq ".cab")} | select fullname
foreach($update in $updates)
{
write-debug $update.fullname
$command = "dism /image:" + $updatepath + " /add-package /packagepath:'" + $update.fullname + "'"
write-debug $command
Invoke-Expression $command
}

$command = "dism /image:" + $updatepath + " /Cleanup-Image /spsuperseded"
Invoke-Expression $command

if($isVHD)
{
dismount-vhd -path $updateTarget -confirm:$false
}
else
{
dism /Unmount-Wim /mountdir:$updatepath /commit
#dism /Unmount-Wim /mountdir:$updatepath /discard
}
}

0 comments on commit 7cd1d59

Please sign in to comment.