Skip to content

Instantly share code, notes, and snippets.

@CodeHeight
Last active October 10, 2018 21:16
Show Gist options
  • Save CodeHeight/2ab281d769a78e5b94d76d260905eee0 to your computer and use it in GitHub Desktop.
Save CodeHeight/2ab281d769a78e5b94d76d260905eee0 to your computer and use it in GitHub Desktop.
Powershell snapshots
Set-Location C:\
Clear-Host
$AzureRMModule = Find-Module AzureRM
$AllAzureRMModules = Find-Module AzureRM*
$AzureRMModule | Measure-Object
$AllAzureRMModules | Measure-Object
$AllAzureRMModules | Where-Object Name -like *Compute*
Set-location c:\
Clear-Host
Find-Module AzureRM
Find-Module AzureRM | Install-Module -Force -Scope CurrentUser -Verbose
Get-Module -ListAvailable
$tenantid = "f247d411-6e92-4d63-b5b0-4e3b9d41100c"
Login-AzureRMAccount -TenantId $tenantid
Login-AzureRMAccount
Get-AzureRmSubscription
Select-AzureRMSubscription "Visual Studio Enterprise: BizSpark"
<# cd '.\Visual Studio Enterprise: BizSpark\' #>
Set-location c:\
Clear-Host
# Context Parts:
# - Environment
# - Tenant
# - Subscription
# - Account
# - Credentials
Login-AzureRMAccount
Select-AzureRMSubscription "Visual Studio Ultimate with MSDN"
#Using contexts to access subscriptions and save credentials
Set-Location C:\
Clear-Host
# Context Parts:
# - Environment
# - Tenant
# - Subscription
# - Account
# - Credentials
Login-AzureRmAccount
#The alias is Login-AzureRMAccount
New-AzureRmResourceGroup -Name "AzurePowerShell" -Location "westus"
Get-AzureRMResourceGroup -Name "AzurePowerShell"
#In a new session, the context needs to be created again.
Set-Location C:\
Clear-Host
Get-AzureRMResourceGroup -Name "AzurePowerShell"
#By using Context Autosave, your already connected
Enable-AzureRmContextAutosave
#With autosave enabled, when we log in it will get saved
Set-Location C:\
Clear-Host
Get-AzureRMResourceGroup -Name "AzurePowerShell"
Disable-AzureRmContextAutoSave
Get-AzureRMSubscription
$Ent = Set-AzureRmContext -Subscription "Visual Studio Enterprise" -Name "VSEnterprise"
$MSDN = Set-AzureRmContext -Subscription "Visual Studio Ultimate with MSDN" -Name "MSDN"
Get-AzureRMResourceGroup -Name "AzurePowerShell" -DefaultProfile $MSDN
Get-AzureRMResourceGroup -Name "AzurePowerShell" -DefaultProfile $Ent
New-AzureRmResourceGroup -Name "MyMSDNResourceGroup" -Location "westus" -DefaultProfile $MSDN
Remove-AzureRMResourceGroup -Name "MyMSDNResourceGroup" -DefaultProfile $MSDN
Set-Location C:\
Clear-Host
# Finding cmdlets in the Azure modules
# What are the modules in Azure RM?
Get-Module AzureRM*Storage* -ListAvailable
Get-Command *Disk* -Module AzureRM*
#Structure of the cmdlets - LOOOONG Nouns
Get-Command Get-AzureRmVM
Get-Service
# Get-VM? No...
# Get-AzureVM? Not quite...
# Get-AzureRMVM A-ha!
#Get a better understanding of PowerShell through practice with AzureRM IaaS Resources
Get-Command New-AzureRMVM -Syntax
Get-Help New-AzureRMVM -Online
New-AzureRMVM -
#Storage
Get-Command -Module AzureRM.Storage
#Network
Get-Command -Module AzureRM.Network
Get-Command -Module AzureRM.Network | Measure-Object
#Virtual Machines
Get-Command -Module AzureRM.Compute
Get-Command -Module AzureRM.Compute | Measure-Object
#Reading simple and complex object properties
Set-Location C:\
Clear-Host
#Reviewing - working with simple and complex properties in PowerShell
Get-AzureRMVM -Status
# Display only one property value
Get-AzureRMVM -Status | Select-Object -Property PowerState
# Select multiple object properties
Get-AzureRMVM -Status | Select-Object Name, PowerState
#Works great for simple properties, but what about complex?
Get-AzureRMVM -Status | Select-Object Name, VMSize, PowerState
Get-AzureRMVM -Status | Select-Object Size -First 1
Get-AzureRMVM -Status | Get-Member
Get-AzureRMVM | Select-Object -ExpandProperty HardwareProfile -First 1
Get-AzureRMVM | Select-Object -Property * -First 1
$VM = Get-AzureRMVM -Status | Select-Object -First 1
$VM
#Mix of simple and complex object properties
$VM | Select-Object -Property *
#Expanding a complex object property
$VM | Select-Object -ExpandProperty HardwareProfile
#Or... use the '.' for an object reference. Intellisense also helps (CTRL + Enter)
$VM.HardwareProfile.VmSize
#
$VM | Select-Object -Property *
Clear-Host
$VM.StorageProfile | ConvertTo-Json -Depth 1
$VM.StorageProfile | ConvertTo-Json -Depth 2
$VM.StorageProfile
$VM.StorageProfile.OsDisk.ManagedDisk.Id
#Filtering and Formatting Output of Object
Set-Location C:\
Clear-Host
# Pick a VM... any VM will do...
$AllVMs = Get-AzureRMVM
$FirstVM = $AllVMs | Select-Object -First 1
$FirstVM
$EastUSVMs = $AllVMs | Where-Object Location -eq "eastus2"
$EastUSVMs
#Why doesn't this work?
$D_SeriesVMs = $AllVMs | Where-Object VMSize -like "Standard_D*"
if (-not $D_SeriesVMs) {Write-Host -ForegroundColor Cyan "That didn't work"} else {$D_SeriesVMs}
#We do have AllVMS variable populated, and we have the VMSize showing in this table...
$AllVMs
$D_SeriesVMs = $AllVMs | Where-Object HardwareProfile.VMSize -like "Standard_D*"
if ($D_SeriesVMs -eq $Null) {Write-Host -ForegroundColor Cyan "Not yet..."}
$AorD_SeriesVMs_withPremiumStorage = $AllVMs | Where-Object {$_.HardwareProfile.VMSize -match "_(D|A).*s.*_"}
$RandomVM = Get-AzureRMVM -Status | Get-Random
Get-AzureRMVM -Status | Get-Random -Count 3 | Select-Object Name
$RandomVM
#Remember this from our last video
$RandomVM | Select-Object Name, VMSize, PowerState
$RandomVM | Select-Object Name, @{Name="Size of the VM";Expression={$_.HardwareProfile.VMSize}}, PowerState
#Formatting that output
$RandomVM | Format-Custom
$RandomVM | Format-Custom -Property diagnosticsprofile
$RandomVM | Select-Object Name, Location, resourcegroupname, vmid
$RandomVM | Select-Object Name, Location, resourcegroupname, vmid | Format-List
$RandomVM | Select-Object Name, location, resourcegroupname, vmid, powerstate
$RandomVM | Select-Object Name, location, resourcegroupname, vmid, powerstate | Format-Table
# Tenants, Subscriptions and Accounts are all important...
# But once we're logged in... now what?
Set-Location C:\
Clear-Host
# Let's start with Resource Groups
# Resource Groups are a logical grouping of resources
# Useful for separating out applications, use cases, teams or environments
# Group the resources that you want into a single resource group...
# Delete the resource group and you're sure to not leave any forgotten resources behind
New-AzureRmResourceGroup -Name "AzurePowerShellCourse" -Location "westus2" -Tag @{Use="Training";Publisher="PacktPub"}
Get-AzureRmResourceGroup -Name "AzurePowerShellCourse"
# 3 Useful concepts - passing the resource group through the pipeline and using the -Whatif parameter
$ResGroup = Get-AzureRmResourceGroup -Name "AzurePowerShellCourse"
$ResGroup | Remove-AzureRmResourceGroup -WhatIf
# Another pattern with AzureRM: Save object as a variable, change variable properties, save with "set"
$ResGroup.Tags
$ResGroup.Tags = $null
$ResGroup.Tags += @{Author="Michael Simmons"}
$ResGroup.Tags += @{Course="PowerShell for AzureRM (IaaS)"}
$ResGroup | Set-AzureRmResourceGroup
# Locations are an important property. Many of the commands don't work without it...
Get-AzureRmLocation
# And not all resources are available in every location.
Get-AzureRmLocation | Where-Object {$_.Providers -contains "Microsoft.Automation" -and $_.DisplayName -like "* US*"}
Get-AzureRmLocation | Where-Object {$_.Providers -notcontains "Microsoft.Automation" -and $_.DisplayName -like "* US*"}
#It's important to know that each resource has a unique Resource ID.
# So even though you can have multiple VMs named "Web1", each of those still has
# a unique identifier associated with it.
$RandomVM = Get-AzureRMVM | Get-Random
# So the VM has a unique ID
$RandomVM.ID
#And when a resource (like the VM) uses another resource (like a network card)...
# BOTH of the resources have their own resource ID
$RandomVM | Get-AzureRmResource
$RandomVM.Id
$RandomVM.StorageProfile.OsDisk.ManagedDisk.Id
$NICId = $RandomVM.NetworkProfile.NetworkInterfaces[0].Id
$NICId
Get-AzureRmResource -ResourceId $NICId
$AdminAccount = Get-Credential "LocalAdmin"
$VMCreateTime = Measure-Command { New-AzureRMVM -Name "testVM" -Credential $AdminAccount}
$VMCreateTime
$VMAsJobWaitTime = Measure-Command {New-AzureRMVM -AsJob -Name "testVMAsJob" -Credential $AdminAccount}
$VMAsJobWaitTime
$JobStatusDuringBuild = Get-Job
$JobStatusDuringBuild | Receive-Job
#Using locks to keep resources from deletion
New-AzureRmResourceLock -LockName "testVMNoDelete" -LockLevel CanNotDelete -ResourceGroupName "testvm" -LockNotes "Remove Lock after testing"
New-AzureRmResourceLock -Scope
Get-AzureRmResourceGroup "testvm" | Remove-AzureRMResourceGroup -AsJob -Force
Get-Job -Id 3 | Format-List
Get-Job 2 | Select-Object -ExpandProperty error
Get-AzureRMResourceLock | Remove-AzureRmResourceLock
Get-AzureRmResourceGroup "testvm" | Remove-AzureRMResourceGroup -AsJob -Force
#Finding out more about the Azure PowerShell modules and what's coming next
https://github.com/Azure/azure-powershell/projects
#Keeping your modules up to date
Update-Module AzureRM
#Checking if there's a newer module
# To find the latest version of a module, use Find-Module
# To find the currently installed version of a module, use Get-Module (-listavailable)
# To update to the latest module, use Update-Module
Function Compare-ModuleVersion {
Param ($Name, [switch]$Autoupdate)
$InstalledVersion = (Get-Module -Name $Name -ListAvailable).Version
$OnlineVersion = (Find-Module -Name $Name ).Version
if ($OnlineVersion -gt $InstalledVersion ) {
Write-Host "Newer version of $Name is available. (Installed: $InstalledVersion) (Online: $OnlineVersion)"
if ($Autoupdate) {
Update-Module $Name -Verbose
}
} else {
Write-Host "Installed: $InstalledVersion"
Write-Host "Online: $OnlineVersion"
}
}
Compare-ModuleVersion -Name AzureRM -Autoupdate
#Storage Account Overview and Intro to the Cmdlets
#Rules for creating your storage account
# names:
# - have to be unique
# - only lowercase letters, numbers, and hyphens
# Securing the storage accounts
# keys:
# - Access keys are basically ridiculously long passwords associated with the account.
# - There are 2 keys for a storage account
# - You can regenerate the access keys
# network rules:
# - Default is "allow from Internet"
# - You can be more specific to allow only from select networks or IP ranges
# Working with storage content
# blobs:
# - "binary large objects"
# - BLOCK blobs: "Files" (MOST)
# - APPEND blobs: "Logs"
# - PAGE blobs: "Azure VM disks"
# files:
# - File shares (SMB)
# tables:
# - A NoSQL database instance
# queues:
# - A simple message queue
# There are 2 modules that help us to manage all of our storage resources:
# - The AzureRM.Storage is used for creating and managing the storage accounts
Get-Command -module AzureRM.Storage
# - The Azure.Storage module holds cmdlets used to manage shares, files and blobs
Get-Command -module Azure.Storage
#Creating a new storage account
Set-Location c:\
Clear-Host
# Creating a BLOB storage
# - Needs: Blog Storage Account, a Container (like "Folder") and some content
$RG = New-AzureRMResourceGroup Storage -Location 'West US'
$BlobAccount = $RG | New-AzureRmStorageAccount -Name "azurestorageblobyates" `
-SkuName Standard_GRS `
-Kind BlobStorage `
-AccessTier Hot
# Now we need a container to store our blobs. Extra bonus: since we will need it again, save it as a variable
$PhotosContainer = New-AzureStorageContainer -Name "photos" -Permission Blob -Context $BlobAccount.Context
# Adding some content to the container
Set-AzureStorageBlobContent -File "C:\TEMP\heliospherere.jpg" `
-Container $PhotosContainer.Name `
-Blob "heliospherere.jpg" `
-Context $BlobAccount.Context
# Made easier with using the pipeline, either for referencing the storage account of files
$PhotosContainer | Set-AzureStorageBlobContent -File "C:\TEMP\IOTA.jpg" -Blob "IOTA.jpg"
# Another popular option is AzCopy
# https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azcopy
# Lots of helpful parameters for AzCopy too,
# https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azcopy#azcopy-parameters
# After installation, it's like Robocopy for storage accounts
$Keys = $BlobAccount| Get-AzureRmStorageAccountKey
$Key1 = $Keys[0].Value
$PhotoURL = $PhotosContainer.CloudBlobContainer.Uri
Push-Location 'C:\Program Files (x86)\Microsoft SDKs\Azure\AzCopy'
& .\AzCopy.exe /Source:C:\TEMP /Dest:$PhotoURL /DestKey:$Key1 /S /Y
# Getting (listing) the blob
$PhotosContainer | Get-AzureStorageBlob
# Getting (downloading) the blob content
Test-Path C:\TEMP\azure\dl.jpg
$PhotosContainer | Get-AzureStorageBlob | Get-Random | Get-AzureStorageBlobContent -Destination C:\TEMP\azure\dl.jpg -Force
Test-Path C:\TEMP\azure\dl.jpg
$PhotosContainer | Get-AzureStorageBlob | Remove-AzureStorageBlob -WhatIf
# Using the Azure File Shares with Windows PowerShell
Set-Location C:\
Clear-Host
$RG = Get-AzureRMResourceGroup Storage -Location 'West US'
$GeneralAccount = $RG | New-AzureRmStorageAccount -Name "azurestorageyates" `
-SkuName Standard_LRS `
-Kind StorageV2
# With the storage account created, we can now create one or more file shares
$Departments = @("Sales","Marketing","Operations")
Foreach ($Dept in $Departments) {
$GeneralAccount | New-AzureStorageShare -name "$($Dept.ToLower())"
}
#Setting the quota (size) for the shares
$GeneralAccount | Get-AzureStorageShare | Set-AzureStorageShareQuota -Quota 500
#Adding content: Several options
# Upload a file with
$sales = $GeneralAccount | Get-AzureStorageShare "sales"
$sales | New-AzureStorageDirectory -Path "Pictures"
$sales | Set-AzureStorageFileContent -Source C:\TEMP\IOTA.jpg -Path "Pictures\IOTA.jpg"
# Mapping the drive
# You need to know the storageAccount and Share name
$StorageAccountName = $GeneralAccount.StorageAccountName
$ShareName = $sales.Name
$SMBSharePath = "\\$StorageAccountName.file.core.windows.net\$ShareName"
#You need to create a credential object
# You need a storage account key
$rawkey = $GeneralAccount | Get-AzureRmStorageAccountKey | Select-Object -First 1 -ExpandProperty Value
$secKey = $rawkey | ConvertTo-SecureString -AsPlainText -Force
# And a username - the storage account name
$AccountName = "Azure\$StorageAccountName"
# To create the credential object
$salesCredential = New-Object pscredential -ArgumentList $AccountName, $secKey
# Now you have everything you need to map the drive:
New-PSDrive -Name Z -PSProvider FileSystem -Root $SMBSharePath -Credential $salescredential -Persist
# Let's finish up with finding and downloading content from our SMB share with PowerShell
New-Item -ItemType directory D:\localPhotos\FromShare
$Sales | Get-AzureStorageFile
$Pictures = $Sales | Get-AzureStorageFile
$Pictures | Get-AzureStorageFile
$PictureFiles = $Pictures | Get-AzureStorageFile
$PictureFiles | Get-AzureStorageFileContent -Destination D:\localPhotos\FromShare
Set-Location C:\
Clear-Host
# Tables
# Creating a Table
# Using the "general" Storage Account
$StorageAccount = Get-AzureRmStorageAccount -ResourceGroupName "Storage" -Name azurestorageyates
$v2Context = $StorageAccount.Context
$Table = New-AzureStorageTable –Name Table -Context $v2Context
# Retrieve the list of tables to verify the table has been removed.
$StorageAccount | Get-AzureStorageTable -Name Table
Get-AzureStorageTable –Context $v2Context -Name Table
#Removing the entire table
Remove-AzureStorageTable –Name Table –Context $v2Context -WhatIf
$v2Context | Remove-AzureStorageTable -Name Table -WhatIf
$Table | Remove-AzureStorageTable -WhatIf
# Options for populating tables:
# AZCopy https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azcopy
# REST API https://docs.microsoft.com/en-us/rest/api/storageservices/table-service-rest-api
# AzureRMStorageTable Module https://blogs.technet.microsoft.com/paulomarques/2017/01/17/working-with-azure-storage-tables-from-powershell/
# Queues
# Creating a Queue
$v2Context | New-AzureStorageQueue -Name messagequeue123
$MQ = Get-AzureStorageQueue -Name "messagequeue123" -Context $StorageAccount.Context
$MQ | Remove-AzureStorageQueue
# Snapshots
$RG = Get-AzureRMResourceGroup -Name "Storage"
$v2Context = ($RG | Get-AzureRmStorageAccount -Name "azurestorageyates").Context
$Sales = Get-AzureStorageShare -Name "sales" -Context $v2Context
$Sales
$Sales | Get-AzureStorageFile -path \pictures | Get-AzureStorageFile
$Snapshot = $Sales.SnapshotAsync().Result
Get-AzureStorageShare -Context $v2Context
$Sales | Get-AzureStorageFile
$Sales | Get-AzureStorageFile -Path "Pictures\IOTA.jpg"
# Accidently delete??
$Sales | Remove-AzureStorageFile -Path "Pictures\IOTA.jpg"
# Restore SampleUpload.txt from the share snapshot
Start-AzureStorageFileCopy `
-SrcShare $Snapshot `
-SrcFilePath "Pictures\IOTA.jpg" `
-DestShareName "sales" `
-DestContext $v2Context `
-DestFilePath "Pictures\IOTA.jpg"
#Lots of variations on using Filecopy to get files from one place to another
# https://docs.microsoft.com/en-us/powershell/module/Azure.Storage/Start-AzureStorageFileCopy
$Sales | Get-AzureStorageFile -Path "Pictures\IOTA.jpg"
# Key management
## Recreate a key
$v2Context | New-AzureRmStorageAccountKey -KeyName key1 -ResourceGroupName "Storage"
$v2Context | Get-AzureRmStorageAccountKey -ResourceGroupName "Storage"
## Change a key to read-only
$Keys = $v2Context | Get-AzureRmStorageAccountKey -ResourceGroupName "Storage"
$Keys
$Keys[0]
Set-Location C:\
Get-Command -Module AzureRM.Network | Measure-Object
Get-Command -Module AzureRM.Network | Group-Object -Property Noun `
| Sort-Object -Property Name | Select-Object -Property Count, Name
Get-Command -Module AzureRM.Network -Noun AzureRmVirtualNetwork
Get-Command -Module AzureRM.Network -Noun AzureRmVirtualNetworkSubnetConfig
Get-Command -Module AzureRM.Network -Noun AzureRmNetworkSecurityGroup
Get-Command -Module AzureRM.Network -Noun AzureRmNetworkSecurityRuleConfig
Set-Location c:\
Clear-Host
#Building a Virtual Network with 2 subnets
#Start with a resource group for the virtual network
$NetRG = Get-AzureRMResourceGroup | Where-Object {$_.ResourceGroupName -eq "NetRG"}
if (-not $NetRG) {
$NetRG = New-AzureRmResourceGroup -Location 'West US' -Name "NetRG"
}
$NetRG
get-command New-AzureRMVirtualNetwork -Syntax
# Now to create a virtual network called "demoNet"
$vNet = $NetRG | New-AzureRMVirtualNetwork -Name "demoNet" -AddressPrefix "10.100.0.0/16"
$vNet
$vNet.Tag = @{Reason="Demo"}
$vNet | Set-AzureRMVirtualNetwork
$vNet = $NetRG | Get-AzureRMVirtualNetwork
$vNet | Remove-AzureRmVirtualNetwork -WhatIf
# Next up, adding two subnets to the network.
$vNet | Add-AzureRmVirtualNetworkSubnetConfig -Name "sub0" -AddressPrefix "10.100.0.0/24"
$vNet | Add-AzureRmVirtualNetworkSubnetConfig -Name "sub1" -AddressPrefix "10.100.1.0/24"
$vNetInAzure = $NetRG | Get-AzureRmVirtualNetwork
#local
$vNet.Subnets | Select-Object Name, AddressPrefix
#azure
$vNetInAzure.Subnets | Select-Object Name, AddressPrefix
$vNet | Set-AzureRmVirtualNetwork
$vNet | Set-AzureRmVirtualNetworkSubnetConfig -Name "sub1" -AddressPrefix "10.100.100.0/24"
$Sub1 = Get-AzureRmVirtualNetworkSubnetConfig -Name "sub1" -VirtualNetwork $vNet
$vNet | Remove-AzureRmVirtualNetworkSubnetConfig -Name "sub1"
$vNet | Foreach-Object {
$_ | Add-AzureRmVirtualNetworkSubnetConfig -Name $sub1.Name -AddressPrefix $sub1.AddressPrefix
$_ | Set-AzureRMVirtualNetwork
}
$NetRG | Remove-AzureRmResourceGroup -Whatif
Set-Location C:\
Clear-Host
$NetRG = Get-AzureRMResourceGroup | Where-Object {$_.ResourceGroupName -eq "NetRG"}
if (-not $NetRG) {
$NetRG = New-AzureRmResourceGroup -Location 'West US' -Name "NetRG"
}
$NetRG
# Define some application security groups. Great for categorizing network roles
Get-Command New-AzureRMApplicationSecurityGroup -Syntax
$WebGroup = $NetRG | New-AzureRmApplicationSecurityGroup -Name "WebRole"
$SQLGroup = $NetRG | New-AzureRMApplicationSecurityGroup -Name "SQLRole"
# Don't worry about how to get computers added to those groups. I'll show that when we're working
# with Virtual Machines in the next section
# Now let's set the ground rules...
## Webs can receive traffic on 80 and 443 from the Internet.
## SQL can receive traffic on port 1433, but only from the Webs.
## We'll add in a rule to allow remote PowerShell from a jumpbox...
### It will allow Remote Desktop in (3389) and allow 5985 (or 5986 for encrypted, which is recommended) out
$webRule = New-AzureRmNetworkSecurityRuleConfig -Name "Allow-Web" -Description "Normal web traffic" `
-Direction Inbound -Access Allow `
-Protocol Tcp `
-SourceAddressPrefix Internet -SourcePortRange * `
-DestinationApplicationSecurityGroup $WebGroup -DestinationPortRange 80,443 `
-Priority 200
$sqlRule = New-AzureRmNetworkSecurityRuleConfig -Name "Allow-Sql" -Description "Webs to Sql traffic" `
-Direction Inbound -Access Allow `
-Protocol Tcp `
-SourceApplicationSecurityGroup $WebGroup -SourcePortRange * `
-DestinationApplicationSecurityGroup $SQLGroup -DestinationPortRange 1433 `
-Priority 300
$jumpBoxRDPRule = New-AzureRMNetworkSecurityRuleConfig -Name "Allow-Jumpbox-RDP" -Description "RDP to jump server only" `
-Direction Inbound -Access Allow `
-Protocol Tcp `
-SourceAddressPrefix Internet -SourcePortRange * `
-DestinationAddressPrefix 10.0.0.100 -DestinationPortRange 3389 `
-Priority 400
$winRMRule = New-AzureRmNetworkSecurityRuleConfig -Name "Allow-WinRM" -Description "PowerShell Remoting" `
-Direction Inbound -Access Allow `
-Protocol Tcp `
-SourceAddressPrefix 10.0.0.100 -SourcePortRange * `
-DestinationAddressPrefix VirtualNetwork -DestinationPortRange 5985-5986 `
-Priority 410
Get-Command Get-AzureRmNetworkSecurityRuleConfig -Syntax
Get-Command New-AzureRmNetworkSecurityGroup -Syntax
$NetRG | New-AzureRmNetworkSecurityGroup -Name "AppEnvironment" -SecurityRules $webRule, $sqlRule
$AppNSG = $NetRG | Get-AzureRmNetworkSecurityGroup -Name "AppEnvironment"
$AppNSG.SecurityRules | Select-Object Name, Description, ProvisioningState
$AppNSG.SecurityRules += $winRMRule, $jumpBoxRDPRule
$AppNSG = $AppNsg | Set-AzureRmNetworkSecurityGroup
$AppNSG.SecurityRules | Select-Object Name, Description, ProvisioningState
# We have a really great network security group now. I'm so excited!! Let's apply it to the network!
# Let's get the virtual network:
$NetRG | Get-AzureRmVirtualNetwork -Name "demoNet" | Tee-Object -Variable vNet
$vnet = $null
Remove-Variable -Name vNet
Set-Variable -Name vNet -Value ""
$vnet
$vNet.Subnets | Select-Object Name, AddressPrefix, NetworkSecurityGroup
# And apply the network security group to the subnets
$vNet.Subnets | Foreach-Object {
$vNet | Set-AzureRMVirtualNetworkSubnetConfig -Name $_.name -AddressPrefix $_.AddressPrefix -NetworkSecurityGroup $AppNSG
}
$vNet.Subnets | Select-Object Name, AddressPrefix, NetworkSecurityGroup | Format-Table -AutoSize
$vNet = $vNet | Set-AzureRmVirtualNetwork
$vNet.Subnets[0]
$NetRG | Remove-AzureRMResourceGroup
Set-Location C:\
Clear-Host
Get-Command -Module AzureRM* -Name *VM* | Measure-Object
# Descriptions of the VM
# Image - the OS, Version, SKU, etc.
# Size - CPUs, RAM, etc.
# Location, Resource Group, Tags
# Status - Is it provisioned and running
# Resources
# ---------
# Network - network cards, public and private IP addresses
# Disks - OS and Data disks
# Software Configuration
# ---------------------
# OS Configuration
# - Admin Username
# Extensions
# - Desired State Configuration
# - Anti-Virus
Get-Command -Noun AzureRmVM
Get-Command -Noun AzureRmVMConfig
Get-Command -Noun AzureRmVMImage*
Get-Command -Noun AzureRmVMNetworkInterface
Get-Command -Noun AzureRmVMDataDisk
Set-Location C:\
Clear-Host
# Choosing a resource group and location
If (-not (Get-AzureRMResourceGroup | Where-Object {$_.ResourceGroupName -eq "VMRG"})) {
$VMRG = New-AzureRMResourceGroup -Name "VMRG" -Location 'East US'
} else {
$VMRG = Get-AzureRMResourceGroup -Name "VMRG"
}
# Creating an Azure Virtual Machine
# Choose your OS: Windows or Linux
# Choose your Publisher: Microsoft, Centos, Redhat, Ubuntu,
Get-AzureRmVMImagePublisher -Location 'East US' | Select-Object -First 10
Get-AzureRmVMImageOffer -Location "East US" -PublisherName "MicrosoftSQLServer"
Get-AzureRMVMImageSku -Location "East US" -PublisherName "MicrosoftSQLServer" -Offer "SQL2017-WS2016"
Get-AzureRmVMImage -Location "East US" -PublisherName "MicrosoftSQLServer" -Offer "SQL2017-WS2016" -Skus "SQLDEV"
# Choose a size for the VM
Get-AzureRMVMSize -Location 'East US'
Get-AzureRMVMSize -Location 'East US' | Where-Object {$_.MemoryInMB -eq 8GB/1MB -and $_.NumberOfCores -eq 2}
$VMSize = Get-AzureRMVMSize -Location "eastus" | Out-GridView -Title "Choose a VM Size" -PassThru
$VMSize
# Choose other options
# - Username / password for admin account
$AdminLogin = Get-Credential "LocalAdmin"
# - Network settings
$NetRG = Get-AzureRMResourceGroup "NetRG"
$DemoNet = $NetRG | Get-AzureRMVirtualNetwork -Name "DemoNet"
$Sub0 = $DemoNet.Subnets[0]
$SQLGroup = $NetRG | Get-AzureRMApplicationSecurityGroup -Name "SQLRole"
$SQLGroup.Id
$NIC = $VMRG | New-AzureRmNetworkInterface -ApplicationSecurityGroupId $SQLGroup.Id -Name "SQLVM-Nic" -SubnetId $Sub0.Id
$NIC
# Building a VM with the fewest possible inputs
New-AzureRmVM -Name "SimpleVM" -Credential $AdminLogin
# Building a VM with a full selection of options
# Start with a VM Configuration
$VMSet = New-AzureRmVMConfig -VMName "SQL2017" -VMSize $VMSize.Name
# Configure the operating system
Set-AzureRmVMOperatingSystem -VM $VMSet -Windows -ComputerName "SQL2017" -Credential $AdminLogin
# Configure the network
Add-AzureRmVMNetworkInterface -VM $VMSet -Id $NIC.Id
# Choose the image
Set-AzureRmVMSourceImage -VM $VMSet -PublisherName "MicrosoftSQLServer" -Offer "SQL2017-WS2016" -Skus "Express" -Version "latest"
#
$diskSet = New-AzureRmDiskConfig -Location "eastus" -DiskSizeGB 1024 -CreateOption Empty
$sqlDisk = $VMRG | New-AzureRMDisk -DiskName "SqlDataDisk" -Disk $diskSet
Add-AzureRmVMDataDisk -VM $VMSet -Name "sqlDataDisk" -CreateOption Attach -ManagedDiskId $sqlDisk.Id -Lun 1
#Use the resource group variable to set the resource group and location... VM Configuration holds the rest
$VMRG | New-AzureRMVM -VM $VMSet
New-AzureRMVM -ResourceGroupName "VMRG" -Location "eastus" -Name "JumpBox" `
-VirtualNetworkName "demoNet" -SubnetName "sub1" -AllocationMethod Static -DomainNameLabel "demojumpbox2018" `
-OpenPorts 3389 -ImageName "MicrosoftWindowsServer:WindowsServer:2016-Datacenter:latest" `
-Credential $AdminLogin -Size $VMSize.Name -DataDiskSizeInGb 200 `
-AsJob
$VMRG | Remove-AzureRMResourceGroup -Whatif
Get-Help Get-AzureRmVMImagePublisher -Online
Get-Help New-AzureRmVMConfig
Get-Help New-AzureRMVM -Online
Get-Help Get-AzureRMVMSize -Online
Set-Location C:\
Clear-Host
# Finding a Virtual Machine by name
Get-AzureRMVM -Name SimpleVM
Clear-Host
Get-AzureRMVM -Name SimpleVM -ResourceGroupName SimpleVM
# OR
$VMList = Get-AzureRMVM
$VMFromList = $VMList | Where-Object {$_.Name -eq "SimpleVM"}
# The Difference between VM Instance Object and List Object
$VM = Get-AzureRMVM -Name SimpleVM -ResourceGroupName SimpleVM
$VMList.Count
$VM.Count
$VMFromList.Count
$VM
$VMFromList
$VM | Format-Table
$VM.GetType()
$VMFromList.GetType()
# Getting all VMs from a Resource Group
$VMRG_VMs = Get-AzureRMVM -ResourceGroupName VMRG -Status
$VMRG_VMs
$VMRG_VMs[0].GetType()
$VMRG_VMs | Where-Object {$_.PowerState -eq "running"}
$VMRG_VMs | Get-Member -Name *power*
$VMRG_VMs | Select-Object Name, PowerState
$VMRG_VMs
$VMRG_VMs | Where-Object {$_.PowerState -eq "VM running"}
$VM
$VM.NetworkProfile
$VM.NetworkProfile.NetworkInterfaces
$VM.NetworkProfile.NetworkInterfaces[0].Id
$NicID = $VM.NetworkProfile.NetworkInterfaces[0].Id
$Nic = Get-AzureRMResource -ResourceId $NicId | Get-AzureRmNetworkInterface
$Nic.IpConfigurations[0].PrivateIpAddress
$Nic.IpConfigurations[0].PublicIpAddress
$VM.StorageProfile
$VM.StorageProfile.OsDisk
$VM.OSProfile
Function Get-MyAzureResourceGroupNetworkInfo {
Param ($ResourceGroupName)
if (Get-AzureRMResourceGroup | Where-Object ResourceGroupName -eq $ResourceGroupName) {
$VMList = Get-AzureRMVM -ResourceGroupName $ResourceGroupName
Foreach ($VM in $VMList) {
$VMNicID = $VM.NetworkProfile.NetworkInterfaces[0].Id
$VMNic = Get-AzureRMResource -ResourceId $VMNicId | Get-AzureRmNetworkInterface
$OutObject = [PSCustomObject]@{
Name = $VM.Name
PublicIP = $VMNic.IpConfigurations[0].PublicIpAddress.IpAddress
PrivateIP = $VMNic.IpConfigurations[0].PrivateIpAddress
}
$OutObject
} #End Foreach
} #End If
} #End Function
Clear-Host
Get-MyAzureResourceGroupNetworkInfo -ResourceGroupName VMRG
Set-Location C:\
Clear-Host
# Adding resources to a virtual machine
$SimpleVM = Get-AzureRMVM -ResourceGroupName SimpleVM -Name SimpleVM
# Same basic code as from the video on creating a virtual machine
$NetRG = Get-AzureRMResourceGroup "NetRG"
$DemoNet = $NetRG | Get-AzureRMVirtualNetwork -Name "DemoNet"
$Sub1 = $DemoNet.Subnets[1]
$NIC2 = $VMRG | New-AzureRmNetworkInterface -Name "Nic2" -SubnetId $Sub1.Id
$NIC2
Add-AzureRmVMNetworkInterface -VM $SimpleVM -NetworkInterface $NIC2
$SimpleVM.NetworkProfile.NetworkInterfaces
$SimpleVMInAzure = Get-AzureRMVM -ResourceGroupName SimpleVM -Name SimpleVM
$SimpleVMInAzure.NetworkProfile.NetworkInterfaces
# Resizing a virtual machine
$SimpleVM.HardwareProfile.VmSize
$NewSize = "Standard_DS2_v2"
$SimpleVM.HardwareProfile.VmSize = $NewSize
$SimpleVM.HardwareProfile.VmSize
$SimpleVMInAzure = Get-AzureRMVM -ResourceGroupName SimpleVM -Name SimpleVM
$SimpleVMInAzure.HardwareProfile.VmSize
# Increasing the capacity of a data disk
$diskOptions = New-AzureRmDiskConfig -SkuName Premium_LRS -DiskSizeGB 100 -Location "eastus" -CreateOption "Empty"
$disk = New-AzureRmDisk -Disk $diskOptions -ResourceGroupName VMRG -DiskName "DataDisk1"
Add-AzureRmVMDataDisk -VM $SimpleVM -Name "DataDisk1" -CreateOption Attach -ManagedDiskId $disk.Id -Lun 1
$SimpleVMInAzure = Get-AzureRMVM -ResourceGroupName SimpleVM -Name SimpleVM
$SimpleVMInAzure.StorageProfile.DataDisks
$SimpleVM.StorageProfile.DataDisks
$ExistingDisk = Get-AzureRMDisk -ResourceGroupName VMRG -DiskName DataDisk1
$ExistingDisk.DiskSizeGB = 200
$ExistingDisk | Update-AzureRmDisk
# Expanding the OS Disk
$SimpleVM.StorageProfile.OsDisk
Set-AzureRmVMOSDisk -VM $SimpleVM -DiskSizeInGB 100
$SimpleVMInAzure = Get-AzureRMVM -ResourceGroupName SimpleVM -Name SimpleVM
$SimpleVMInAzure.StorageProfile.OsDisk.DiskSizeGB
$SimpleVM.StorageProfile.OsDisk.DiskSizeGB
Clear-Host
$SimpleVM | Update-AzureRmVM -AsJob
Set-Location C:\
Clear-Host
# Adding a VM extension
# Two variations... Some extensions have their own cmdlets.
Set-AzureRMVMBGInfoExtension -ResourceGroupName "VMRG" -VMName "JumpBox" -Location "eastus" `
-Name "BGInfo" -TypeHandlerVersion "2.1"
# Some extensions don't have their own cmdlets.
# Here, for example is the Octopus Deploy tentacle extension installation
# Configure the settings that the extension requires
$Extensions = Get-AzureRmVmImagePublisher -Location "eastus" | Get-AzureRmVMExtensionImageType
$Extensions | Select-Object PublisherName, Type
$Extensions | Where-Object {$_.type -match "Octopus"}
Get-AzureRMVMExtensionImage -Location "eastus" -PublisherName "OctopusDeploy.Tentacle" -Type "OctopusDeployWindowsTentacle" | Select-Object -Last 1
# Where to find exactly what each extension requires? Start with the vendor for support.
# Octopus Deploy Tentacle specifics were published at:
# https://octopus.com/docs/infrastructure/windows-targets/azure-virtual-machines/via-powershell
$Settings = @{
OctopusServerUrl = "https://octopus.example.com";
Environments = @("Demo");
Roles = @("Db");
CommunicationMode = "Listen";
Port = 10933
}
$Secrets = @{"ApiKey" = "Enter a valid API Key from the Octopus Server..."}
Set-AzureRmVMExtension -ResourceGroupName "VMRG" -Location "eastus" -VMName "SQL2017" `
-Name "OctopusDeployWindowsTentacle" -Publisher "OctopusDeploy.Tentacle" -ExtensionType "OctopusDeployWindowsTentacle" `
-TypeHandlerVersion "2.0.164" `
-Settings $Settings `
-ProtectedSettings $Secrets `
-AsJob
# What about if you don't know the name?
Get-AzureRmVMExtension -ResourceGroupName VMRG -VMName JumpBox
# Don't forget our already established tools for finding extensions on Virtual Machines:
Get-AzureRMVM -ResourceGroupName VMRG -Name JumpBox | Select-Object -ExpandProperty extensions | Select-Object -Property name
Get-AzureRmVMExtension -ResourceGroupName VMRG -VMName JumpBox -Name BGInfo | Remove-AzureRmVMExtension -WhatIf
Set-Location C:\
Clear-Host
# Stop and Start a Virtual Machine
$JumpBox = Get-AzureRMVM -ResourceGroupName VMRG -Name JumpBox -Status
$JumpBox.Statuses | Where-Object {$_.Code -match "PowerState"} | Select-Object -ExpandProperty DisplayStatus
$JumpBox | Stop-AzureRMVM -StayProvisioned -Force -WhatIf
$JumpBox | Start-AzureRMVM -AsJob -WhatIf
$JumpBox | Restart-AzureRMVM -PerformMaintenance
# Snapshot an OS disk, create a new VM from snapshot
# Get the OS Disk to snapshot...
$SqlBox = Get-AzureRMVM -ResourceGroupName VMRG -Name SQL2017
$SqlDisk = $SqlBox.StorageProfile.OsDisk.ManagedDisk.Id
# Take the snapshot...
$QuickSnapConfig = New-AzureRmSnapshotConfig -SourceUri $SqlDisk -Location 'eastus' -CreateOption copy
$QuickSnap = New-AzureRmSnapshot -ResourceGroupName VMRG -Snapshot $QuickSnapConfig -SnapshotName "SqlOSDiskSnapshot"
# Create a new disk from the snapshot...
$NewDiskConfig = New-AzureRmDiskConfig -Location "eastus" -SourceResourceId $QuickSnap.Id -CreateOption Copy
$NewDisk = New-AzureRmDisk -Disk $NewDiskConfig -ResourceGroupName VMRG -DiskName "SQL2017-FromSnapshot"
# Create a new VM that uses the disk!
$NewVMConfig = New-AzureRmVMConfig -VMName "NewSql" -VMSize $SqlBox.HardwareProfile.VmSize
Set-AzureRmVMOSDisk -VM $NewVMConfig -ManagedDiskId $NewDisk.Id -CreateOption "Attach" -Windows
# Finish the OS Config (Local Admin), Add data disks (if required) and a networking profile to the $NewVMConfig.
New-AzureRMVM -VM $NewVMConfig -AsJob
# Moving a Virtual Machine to a different resource group.
$MgmtRG = New-AzureRMResourceGroup -Name "ManagementServers" -Location "East US"
$JumpBox = Get-AzureRMVM -ResourceGroupName "VMRG" | Where-Object Name -eq "JumpBox"
Move-AzureRmResource -DestinationResourceGroupName $MgmtRG.ResourceGroupName -ResourceId $JumpBox.Id
Set-Location c:\
Clear-Host
# Editing the Azure Resource Manager Templates in VSCode: Load the Azure Resource Manager Tools
# Start with a large collection of sample templates:
# https://github.com/Azure/azure-quickstart-templates
Set-Location D:\
New-Item Repos -ItemType Directory
Set-Location D:\Repos
git clone https://github.com/Azure/azure-quickstart-templates
Set-Location D:\Repos\azure-quickstart-templates
Get-Childitem
# Get-Help New-AzureRmResourceGroupDeployment -Online
# Get-Help Save-AzureRmResourceGroupDeploymentTemplate -Online
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"adminUsername": {
"type": "string",
"metadata": {
"description": "Username for the Virtual Machine."
}
},
"adminPassword": {
"type": "securestring",
"metadata": {
"description": "Password for the Virtual Machine."
}
},
"dnsLabelPrefix": {
"type": "string",
"metadata": {
"description": "Unique DNS Name for the Public IP used to access the Virtual Machine."
}
},
"windowsOSVersion": {
"type": "string",
"defaultValue": "2016-Datacenter",
"allowedValues": [
"2008-R2-SP1",
"2012-Datacenter",
"2012-R2-Datacenter",
"2016-Nano-Server",
"2016-Datacenter-with-Containers",
"2016-Datacenter"
],
"metadata": {
"description": "The Windows version for the VM. This will pick a fully patched image of this given Windows version."
}
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Location for all resources."
}
}
},
"variables": {
"storageAccountName": "[concat(uniquestring(resourceGroup().id), 'sawinvm')]",
"nicName": "myVMNic",
"addressPrefix": "10.0.0.0/16",
"subnetName": "Subnet",
"subnetPrefix": "10.0.0.0/24",
"publicIPAddressName": "myPublicIP",
"vmName": "SimpleWinVM",
"virtualNetworkName": "MyVNET",
"subnetRef": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetName'))]"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"name": "[variables('storageAccountName')]",
"apiVersion": "2016-01-01",
"location": "[parameters('location')]",
"sku": {
"name": "Standard_LRS"
},
"kind": "Storage",
"properties": {}
},
{
"apiVersion": "2016-03-30",
"type": "Microsoft.Network/publicIPAddresses",
"name": "[variables('publicIPAddressName')]",
"location": "[parameters('location')]",
"properties": {
"publicIPAllocationMethod": "Dynamic",
"dnsSettings": {
"domainNameLabel": "[parameters('dnsLabelPrefix')]"
}
}
},
{
"apiVersion": "2016-03-30",
"type": "Microsoft.Network/virtualNetworks",
"name": "[variables('virtualNetworkName')]",
"location": "[parameters('location')]",
"properties": {
"addressSpace": {
"addressPrefixes": [
"[variables('addressPrefix')]"
]
},
"subnets": [
{
"name": "[variables('subnetName')]",
"properties": {
"addressPrefix": "[variables('subnetPrefix')]"
}
}
]
}
},
{
"apiVersion": "2016-03-30",
"type": "Microsoft.Network/networkInterfaces",
"name": "[variables('nicName')]",
"location": "[parameters('location')]",
"dependsOn": [
"[resourceId('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]",
"[resourceId('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]"
],
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"publicIPAddress": {
"id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]"
},
"subnet": {
"id": "[variables('subnetRef')]"
}
}
}
]
}
},
{
"apiVersion": "2017-03-30",
"type": "Microsoft.Compute/virtualMachines",
"name": "[variables('vmName')]",
"location": "[parameters('location')]",
"dependsOn": [
"[resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
"[resourceId('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
],
"properties": {
"hardwareProfile": {
"vmSize": "Standard_A2"
},
"osProfile": {
"computerName": "[variables('vmName')]",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPassword')]"
},
"storageProfile": {
"imageReference": {
"publisher": "MicrosoftWindowsServer",
"offer": "WindowsServer",
"sku": "[parameters('windowsOSVersion')]",
"version": "latest"
},
"osDisk": {
"createOption": "FromImage"
},
"dataDisks": [
{
"diskSizeGB": 1023,
"lun": 0,
"createOption": "Empty"
}
]
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces',variables('nicName'))]"
}
]
},
"diagnosticsProfile": {
"bootDiagnostics": {
"enabled": true,
"storageUri": "[reference(resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))).primaryEndpoints.blob]"
}
}
}
}
],
"outputs": {
"hostname": {
"type": "string",
"value": "[reference(variables('publicIPAddressName')).dnsSettings.fqdn]"
}
}
}
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"adminUsername": {
"value": "LocalAdmin"
},
"adminPassword": {
"value": "Hey! Nice Password!"
},
"dnsLabelPrefix": {
"value": "vmfromtemplate072018"
}
}
}
Clear-Host
New-AzureRMResourceGroup -Name "ARMTemplateDemo" -Location "East US"
New-AzureRmResourceGroupDeployment -Name "VMFromTemplate" -ResourceGroupName ARMTemplateDemo `
-TemplateParameterFile .\6.2-azuredeploy.parameters.json -TemplateFile .\6.2-azuredeploy.json `
-Verbose
#Now that the template is completed, the VM is ready!
Get-AzureRMVM -ResourceGroupName ARMTemplateDemo -Status
# Viewing the deployments made in a resource group
Get-AzureRmResourceGroupDeployment -ResourceGroupName "ARMTemplateDemo"
Get-AzureRmResourceGroupDeployment -ResourceGroupName "ARMTemplateDemo" -Name "VMFromTemplate"
# Saving one of the deployments for a resource group as a template
Save-AzureRmResourceGroupDeploymentTemplate -ResourceGroupName "ARMTemplateDemo" -DeploymentName "VMFromTemplate" -Path .\6.3-SavedTemplate
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"adminUsername": {
"type": "String",
"metadata": {
"description": "Username for the Virtual Machine."
}
},
"adminPassword": {
"type": "SecureString",
"metadata": {
"description": "Password for the Virtual Machine."
}
},
"dnsLabelPrefix": {
"type": "String",
"metadata": {
"description": "Unique DNS Name for the Public IP used to access the Virtual Machine."
}
},
"windowsOSVersion": {
"defaultValue": "2016-Datacenter",
"allowedValues": [
"2008-R2-SP1",
"2012-Datacenter",
"2012-R2-Datacenter",
"2016-Nano-Server",
"2016-Datacenter-with-Containers",
"2016-Datacenter"
],
"type": "String",
"metadata": {
"description": "The Windows version for the VM. This will pick a fully patched image of this given Windows version."
}
},
"location": {
"defaultValue": "[resourceGroup().location]",
"type": "String",
"metadata": {
"description": "Location for all resources."
}
}
},
"variables": {
"storageAccountName": "[concat(uniquestring(resourceGroup().id), 'sawinvm')]",
"nicName": "myVMNic",
"addressPrefix": "10.0.0.0/16",
"subnetName": "Subnet",
"subnetPrefix": "10.0.0.0/24",
"publicIPAddressName": "myPublicIP",
"vmName": "SimpleWinVM",
"virtualNetworkName": "MyVNET",
"subnetRef": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetName'))]"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"sku": {
"name": "Standard_LRS"
},
"kind": "Storage",
"name": "[variables('storageAccountName')]",
"apiVersion": "2016-01-01",
"location": "[parameters('location')]",
"properties": {}
},
{
"type": "Microsoft.Network/publicIPAddresses",
"name": "[variables('publicIPAddressName')]",
"apiVersion": "2016-03-30",
"location": "[parameters('location')]",
"properties": {
"publicIPAllocationMethod": "Dynamic",
"dnsSettings": {
"domainNameLabel": "[parameters('dnsLabelPrefix')]"
}
}
},
{
"type": "Microsoft.Network/virtualNetworks",
"name": "[variables('virtualNetworkName')]",
"apiVersion": "2016-03-30",
"location": "[parameters('location')]",
"properties": {
"addressSpace": {
"addressPrefixes": [
"[variables('addressPrefix')]"
]
},
"subnets": [
{
"name": "[variables('subnetName')]",
"properties": {
"addressPrefix": "[variables('subnetPrefix')]"
}
}
]
}
},
{
"type": "Microsoft.Network/networkInterfaces",
"name": "[variables('nicName')]",
"apiVersion": "2016-03-30",
"location": "[parameters('location')]",
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"publicIPAddress": {
"id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]"
},
"subnet": {
"id": "[variables('subnetRef')]"
}
}
}
]
},
"dependsOn": [
"[resourceId('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]",
"[resourceId('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]"
]
},
{
"type": "Microsoft.Compute/virtualMachines",
"name": "[variables('vmName')]",
"apiVersion": "2017-03-30",
"location": "[parameters('location')]",
"properties": {
"hardwareProfile": {
"vmSize": "Standard_A2"
},
"osProfile": {
"computerName": "[variables('vmName')]",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPassword')]"
},
"storageProfile": {
"imageReference": {
"publisher": "MicrosoftWindowsServer",
"offer": "WindowsServer",
"sku": "[parameters('windowsOSVersion')]",
"version": "latest"
},
"osDisk": {
"createOption": "FromImage"
},
"dataDisks": [
{
"diskSizeGB": 1023,
"lun": 0,
"createOption": "Empty"
}
]
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces',variables('nicName'))]"
}
]
},
"diagnosticsProfile": {
"bootDiagnostics": {
"enabled": true,
"storageUri": "[reference(resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))).primaryEndpoints.blob]"
}
}
},
"dependsOn": [
"[resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
"[resourceId('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
]
}
],
"outputs": {
"hostname": {
"type": "String",
"value": "[reference(variables('publicIPAddressName')).dnsSettings.fqdn]"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment