Skip to content

Commit

Permalink
Azure Migrate Whiteboard added
Browse files Browse the repository at this point in the history
  • Loading branch information
johnthebrit committed Nov 29, 2020
1 parent ce94d68 commit 2bf49b9
Show file tree
Hide file tree
Showing 14 changed files with 3,323 additions and 0 deletions.
128 changes: 128 additions & 0 deletions AzureVMMS/ReadScale.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#Module azTable and az.resourcegraph required via the requirements.psd1 file
#MI needs read at MG/sub level and contrib to its storage account

$statusGood = $true

#Table setup
$resourceGroupName = "RG-VMMS-Test"
$storageAccountName = "sasavvmmstst"
$tableName = "VMMSScaleLogs"
$stateTableName = "VMMSScaleLogState"
#$resourceGroupName = $env:FUNC_VMMS_STOR_RGName #"RG-VMMS-Test"
#$storageAccountName = $env:FUNC_VMMS_STOR_ActName #"sasavvmmstst"
#$tableName = $env:FUNC_VMMS_STOR_TblName #"VMMSScaleLogs"
#$stateTableName = $env:FUNC_VMMS_STOR_TblStateName #"VMMSScaleLogState"
try {
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName `
-Name $storageAccountName
$storageContext = $storageAccount.Context
$cloudTable = (Get-AzStorageTable –Name $tableName –Context $storageContext).CloudTable
$cloudTableState = (Get-AzStorageTable –Name $stateTableName –Context $storageContext).CloudTable

$lastRunRecord = Get-AzTableRow `
-table $cloudTableState `
-PartitionKey "VMMSScalePart" -RowKey "LastRun"
}
catch {
$statusGood = $false
Write-Output = "Failure connecting to table for user data, $_"
}

#Time here of last execution
#$startTime = (Get-Date).AddDays(-2) #Would read this in as the endDate from last execution
$startTime = $lastRunRecord.LastRunTime
$endTime = (Get-Date).ToUniversalTime().AddMinutes(-1) #1 minute ago

$currentSubID = (Get-AzContext).Subscription.Id

Write-Output "Finding scale actions between $startTime and $endTime"

$GraphSearchQuery = "Resources
| where type =~ 'Microsoft.Compute/virtualMachineScaleSets'
| join kind=inner (ResourceContainers | where type=='microsoft.resources/subscriptions' | project SubName=name, subscriptionId) on subscriptionId
| project VMMSName = name, RGName = resourceGroup, SubName, SubID = subscriptionId, ResID = id"
$VMMSResources = Search-AzGraph -Query $GraphSearchQuery

if($null -eq $VMMSResources)
{
$statusGood = $false
$body = "Could not find any VMMS instances in accessible subscriptions"
}
else
{
$scaleactions = @()
foreach ($VMMSResource in $VMMSResources)
{
Write-Output "Resource found $($VMMSResource.VMMSName) in RG $($VMMSResource.RGName) in sub $($VMMSResource.Subname)($($VMMSResource.SubID))"

if($currentSubID -ne $VMMSResource.SubID)
{
#Change subscription here first!
Select-AzSubscription -Subscription $VMMSResource.SubID -WarningAction SilentlyContinue
$currentSubID = $VMMSResource.SubID
}

#Get instance information about the VMMS instance
$VMMSInstanceDetail = Get-AzVmss -ResourceGroupName $VMMSResource.RGName -VMScaleSetName $VMMSResource.VMMSName
$SKUName = $VMMSInstanceDetail.Sku.Name

<#Policy
$scale = get-azautoscalesetting -ResourceGroupName $VMMSResource.RGName | where {$_.TargetResourceUri -eq $VMMSInstanceDetail.id}
$rules = $scale.profiles.rules
foreach($rule in $rules)
{
write-output "$($rule.MetricTrigger.OperatorProperty) $($rule.MetricTrigger.Threshold) for $($rule.MetricTrigger.MetricName) will $($rule.ScaleAction.Direction) by $($rule.ScaleAction.Value)"
}#>

#Find scale logs for desired time window
$scalelogs = Get-AzLog -ResourceId $VMMSResource.ResID -StartTime $startTime.ToLocalTime() -EndTime $endTime.ToLocalTime() -WarningAction SilentlyContinue |
Where-Object {$_.OperationName.Value -eq "Microsoft.Insights/AutoscaleSettings/ScaleupResult/Action" -or
$_.OperationName.Value -eq "Microsoft.Insights/AutoscaleSettings/ScaledownResult/Action"}
$scalelogs = $scalelogs | Sort-Object -Property EventTimestamp

$noOfScaleActions = 0
foreach ($scalelog in $scalelogs) {
Write-Output "$($scalelog.EventTimestamp) Scale from $($scalelog.properties.content.OldInstancesCount) to $($scalelog.properties.content.NewInstancesCount)"
$scaleactions += , @($scalelog.EventTimestamp,$scalelog.properties.content.OldInstancesCount,$scalelog.properties.content.NewInstancesCount,$VMMSResource.VMMSName,$SKUName,$VMMSResource.Subname,$VMMSResource.SubID,$VMMSResource.RGName,$VMMSResource.ResID)
$noOfScaleActions++
}

if($noOfScaleActions -eq 0)
{
Write-Output "No scale actions found, current scale count is $($VMMSInstanceDetail.Sku.Capacity)"
$scaleactions += , @($endTime,$VMMSInstanceDetail.Sku.Capacity,$VMMSInstanceDetail.Sku.Capacity,$VMMSResource.VMMSName,$SKUName,$VMMSResource.Subname,$VMMSResource.SubID,$VMMSResource.RGName,$VMMSResource.ResID)
}
}

$partitionKey = $endTime.ToString("yyyy-MM-dd") #the end time

foreach ($action in $scaleactions)
{
#File Output
#Write-Output "$($action[0]),$($action[1]),$($action[2]),$($action[3]),$($action[4]),$($action[5]),$($action[6]),$($action[7]),$($action[8])" | Out-file .\logs.csv -Append -Encoding ascii

#Table Output
$rowKey = "$($action[0].ToString("yyyyMMddHHmmss"))$($action[3])"
#Create
try {
Add-AzTableRow `
-table $cloudTable `
-partitionKey $partitionKey `
-rowKey $rowKey -property @{"ScaleEventTime"=$action[0];"OldInstanceCount"=$action[1];"NewInstanceCount"=$action[2];"VMMSInstanceName"=$action[3];"SKUName"=$action[4];"SubName"=$action[5];"SubID"=$action[6];"RGName"=$action[7];"ResID"=$action[8];}
}
catch {
$statusGood = $false
$body = "Failure creating table entry for scale event, $_"
}
}
}

#Update the last execution time
try {
$lastRunRecord.LastRunTime = $endTime
$lastRunRecord | Update-AzTableRow -table $cloudTableState #commit the change
}
catch {
$statusGood = $false
$body = "Failure updating record for update time, $_"
}
Binary file added AzureVMMS/graph.xlsx
Binary file not shown.
121 changes: 121 additions & 0 deletions AzureVMMS/run.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#Module azTable and az.resourcegraph required via the requirements.psd1 file
#MI needs read at MG/sub level and contrib to its storage account

# Input bindings are passed in via param block.
param($Timer)

#Module azTable required
$statusGood = $true

#Table setup
$resourceGroupName = $env:FUNC_VMMS_STOR_RGName #"RG-VMMS-Test"
$storageAccountName = $env:FUNC_VMMS_STOR_ActName #"sasavvmmstst"
$tableName = $env:FUNC_VMMS_STOR_TblName #"VMMSScaleLogs"
$stateTableName = $env:FUNC_VMMS_STOR_TblStateName #"VMMSScaleLogState"
try {
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName `
-Name $storageAccountName
$storageContext = $storageAccount.Context
$cloudTable = (Get-AzStorageTable –Name $tableName –Context $storageContext).CloudTable
$cloudTableState = (Get-AzStorageTable –Name $stateTableName –Context $storageContext).CloudTable

$lastRunRecord = Get-AzTableRow `
-table $cloudTableState `
-PartitionKey "VMMSScalePart" -RowKey "LastRun"
}
catch {
$statusGood = $false
Write-Output = "Failure connecting to table for user data, $_"
}

#Time here of last execution
#$startTime = (Get-Date).AddDays(-2) #Would read this in as the endDate from last execution
$startTime = $lastRunRecord.LastRunTime
$endTime = (Get-Date).ToUniversalTime().AddMinutes(-1) #1 minute ago

$currentSubID = (Get-AzContext).Subscription.Id

Write-Output "Finding scale actions between $startTime and $endTime"

$GraphSearchQuery = "Resources
| where type =~ 'Microsoft.Compute/virtualMachineScaleSets'
| join kind=inner (ResourceContainers | where type=='microsoft.resources/subscriptions' | project SubName=name, subscriptionId) on subscriptionId
| project VMMSName = name, RGName = resourceGroup, SubName, SubID = subscriptionId, ResID = id"
$VMMSResources = Search-AzGraph -Query $GraphSearchQuery

if($null -eq $VMMSResources)
{
$statusGood = $false
$body = "Could not find any VMMS instances in accessible subscriptions"
}
else
{
$scaleactions = @()

foreach ($VMMSResource in $VMMSResources)
{
Write-Output "Resource found $($VMMSResource.VMMSName) in RG $($VMMSResource.RGName) in sub $($VMMSResource.Subname)($($VMMSResource.SubID))"

if($currentSubID -ne $VMMSResource.SubID)
{
#Change subscription here first!
Select-AzSubscription -Subscription $VMMSResource.SubID -WarningAction SilentlyContinue
$currentSubID = $VMMSResource.SubID
}

#Get instance information about the VMMS instance
$VMMSInstanceDetail = Get-AzVmss -ResourceGroupName $VMMSResource.RGName -VMScaleSetName $VMMSResource.VMMSName
$SKUName = $VMMSInstanceDetail.Sku.Name

#Find scale logs for desired time window
$scalelogs = Get-AzLog -ResourceId $VMMSResource.ResID -StartTime $startTime.ToLocalTime() -EndTime $endTime.ToLocalTime() -WarningAction SilentlyContinue |
Where-Object {$_.OperationName.Value -eq "Microsoft.Insights/AutoscaleSettings/ScaleupResult/Action" -or
$_.OperationName.Value -eq "Microsoft.Insights/AutoscaleSettings/ScaledownResult/Action"}
$scalelogs = $scalelogs | Sort-Object -Property EventTimestamp

$noOfScaleActions = 0
foreach ($scalelog in $scalelogs) {
Write-Output "$($scalelog.EventTimestamp) Scale from $($scalelog.properties.content.OldInstancesCount) to $($scalelog.properties.content.NewInstancesCount)"
$scaleactions += , @($scalelog.EventTimestamp,$scalelog.properties.content.OldInstancesCount,$scalelog.properties.content.NewInstancesCount,$VMMSResource.VMMSName,$SKUName,$VMMSResource.Subname,$VMMSResource.SubID,$VMMSResource.RGName,$VMMSResource.ResID)
$noOfScaleActions++
}

if($noOfScaleActions -eq 0)
{
Write-Output "No scale actions found, current scale count is $($VMMSInstanceDetail.Sku.Capacity)"
$scaleactions += , @($endTime,$VMMSInstanceDetail.Sku.Capacity,$VMMSInstanceDetail.Sku.Capacity,$VMMSResource.VMMSName,$SKUName,$VMMSResource.Subname,$VMMSResource.SubID,$VMMSResource.RGName,$VMMSResource.ResID)
}
} #end of for each scale action

$partitionKey = $endTime.ToString("yyyy-MM-dd") #the end time

foreach ($action in $scaleactions)
{
#File Output
#Write-Output "$($action[0]),$($action[1]),$($action[2]),$($action[3]),$($action[4]),$($action[5]),$($action[6]),$($action[7]),$($action[8])" | Out-file .\logs.csv -Append -Encoding ascii

#Table Output
$rowKey = "$($action[0].ToString("yyyyMMddHHmmss"))$($action[3])"
#Create
try {
Add-AzTableRow `
-table $cloudTable `
-partitionKey $partitionKey `
-rowKey $rowKey -property @{"ScaleEventTime"=$action[0];"OldInstanceCount"=$action[1];"NewInstanceCount"=$action[2];"VMMSInstanceName"=$action[3];"SKUName"=$action[4];"SubName"=$action[5];"SubID"=$action[6];"RGName"=$action[7];"ResID"=$action[8];}
}
catch {
$statusGood = $false
write-output "Failure creating table entry for scale event, $_"
}
}
} #end of if VMMS instances found

#Update the last execution time
try {
$lastRunRecord.LastRunTime = $endTime
$lastRunRecord | Update-AzTableRow -table $cloudTableState #commit the change
}
catch {
$statusGood = $false
write-output "Failure updating record for update time, $_"
}
10 changes: 10 additions & 0 deletions AzureVMMS/sqlstuff.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#Setup SQL Connection
$projectRGName='RG-SQL-SCUS'
$projectSQLDatabaseServer='savtechsqldbserver'
$projectSQLDatabaseName='savtechsqldb'
$projectSQLTableName='vmmsscaleactions'

$sqlDatabaseServer = Get-AzSqlServer -ServerName $projectSQLDatabaseServer -ResourceGroupName $projectRGName
$sqlDatabase = Get-AzSqlDatabase -DatabaseName $projectSQLDatabaseName -ServerName $projectSQLDatabaseServer -ResourceGroupName $projectRGName
$sqlAdminLogin = $sqlDatabaseServer.SqlAdministratorLogin
$sqlAdminPass = $sqlDatabaseServer.SqlAdministratorPassword
Empty file added Bicep/bicepdemo.ps1
Empty file.
22 changes: 22 additions & 0 deletions Bicep/installBicep.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Create the install folder
$installPath = "$env:USERPROFILE\.bicep"
$installDir = New-Item -ItemType Directory -Path $installPath -Force
$installDir.Attributes += 'Hidden'
# Fetch the latest Bicep CLI binary
(New-Object Net.WebClient).DownloadFile("https://github.com/Azure/bicep/releases/latest/download/bicep-win-x64.exe", "$installPath\bicep.exe")
# Add bicep to your PATH
$currentPath = (Get-Item -path "HKCU:\Environment" ).GetValue('Path', '', 'DoNotExpandEnvironmentNames')
if (-not $currentPath.Contains("%USERPROFILE%\.bicep")) { setx PATH ($currentPath + ";%USERPROFILE%\.bicep") }
if (-not $env:path.Contains($installPath)) { $env:path += ";$installPath" }
# Verify you can now access the 'bicep' command.
bicep --help
# Done!

# Fetch the latest Bicep VSCode extension
$vsixPath = "$env:TEMP\vscode-bicep.vsix"
(New-Object Net.WebClient).DownloadFile("https://github.com/Azure/bicep/releases/latest/download/vscode-bicep.vsix", $vsixPath)
# Install the extension
code --install-extension $vsixPath
# Clean up the file
Remove-Item $vsixPath
# Done!
15 changes: 15 additions & 0 deletions Bicep/storageaccount.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
param location string = 'eastus'
param name string = 'sasavilleus007' // must be globally unique

var storageSku = 'Standard_LRS' // declare variable and assign value

resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' = {
name: name
location: location
kind: 'Storage'
sku: {
name: storageSku // reference variable
}
}

output storageId string = stg.id // output resourceId of storage account
36 changes: 36 additions & 0 deletions Bicep/storageaccount.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string",
"defaultValue": "eastus"
},
"name": {
"type": "string",
"defaultValue": "sasavilleus007"
}
},
"functions": [],
"variables": {
"storageSku": "Standard_LRS"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2019-06-01",
"name": "[parameters('name')]",
"location": "[parameters('location')]",
"kind": "Storage",
"sku": {
"name": "[variables('storageSku')]"
}
}
],
"outputs": {
"storageId": {
"type": "string",
"value": "[resourceId('Microsoft.Storage/storageAccounts', parameters('name'))]"
}
}
}
13 changes: 13 additions & 0 deletions DeclarativevsImperative/DSC/IISInstall.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Configuration WebConfig
{
param([string[]]$computerName="localhost") #optional parameters

Node $computerName #zero or more node blocks
{
WindowsFeature WebServer #one or more resource blocks
{
Ensure = "Present" # To uninstall the role, set Ensure to "Absent"
Name = "Web-Server"
}
}
}
Loading

0 comments on commit 2bf49b9

Please sign in to comment.