Skip to content

Instantly share code, notes, and snippets.

View cbattlegear's full-sized avatar

Cameron Battagler cbattlegear

View GitHub Profile
$filteredPolicies = New-Object System.Collections.ArrayList
$policies = Get-AzResource -ResourceType "Microsoft.Network/networkIntentPolicies"
foreach ($policy in $policies) {
$Id = $policy.ResourceId
$response = Invoke-AzRestMethod -Method "GET" -Uri "https://management.azure.com$($Id)?api-version=2021-02-01"
$responseObj = $response.Content | ConvertFrom-Json
$policyInfo = [PSCustomObject]@{ Id = $responseObj.id; Subnet = ""; Orphaned = $false; }
if ($responseObj.properties.PSObject.Properties.Match("subnets").Count -gt 0) {
@cbattlegear
cbattlegear / pause_fabric.ps1
Created February 4, 2025 15:29
Automatically Pause All Fabric Capacities
Param(
[Parameter(Mandatory=$false)]
[string]$subscriptionID = "YourSubscriptionIDHere",
[Parameter(Mandatory=$false)]
[ValidateSet("suspend", "resume")]
[string]$operation = "suspend",
[Parameter(Mandatory=$false)]
[string]$suspendAPIVersion = "api-version=2023-11-01"
select
db_name(database_id) as database_name,
[size_in_MiB] = CAST([size] * 8 / 1024.0 AS DECIMAL(10,2)),
CASE
WHEN CAST([size] * 8 / 1024.0 AS DECIMAL(10,2)) <= 132096 THEN 500 -- If less than or equal to 129 GiB
WHEN CAST([size] * 8 / 1024.0 AS DECIMAL(10,2)) > 132096 AND CAST([size] * 8 / 1024.0 AS DECIMAL(10,2)) <= 525312 THEN 2300 -- If more than 129 GiB but less than or equal to 513 GiB
WHEN CAST([size] * 8 / 1024.0 AS DECIMAL(10,2)) > 525312 AND CAST([size] * 8 / 1024.0 AS DECIMAL(10,2)) <= 1049600 THEN 5000 -- if more than 513 GiB but less than 1025 GiB
ELSE 7500 -- Any more than 1025 GiB and you get 7500
END as file_iops
from sys.master_files
CREATE EVENT SESSION [FailedQueries] ON SERVER
ADD EVENT sqlserver.error_reported
(ACTION(sqlserver.client_app_name, sqlserver.client_hostname,
sqlserver.database_name, sqlserver.sql_text, sqlserver.username)
WHERE ([package0].[greater_than_int64]([severity], (10)) AND client_app_name='SQLExternalMonitoring'))
WITH (STARTUP_STATE = ON)
GO
@cbattlegear
cbattlegear / AHUB for PaaS.kql
Last active January 26, 2024 19:53
Resource Graph Queries for AHUB on SQL VMs
resources
| where type == "microsoft.sql/servers/databases" or type == "microsoft.sql/managedinstances" or type == "microsoft.sql/servers/elasticpools"
| where properties.licenseType == "LicenseIncluded"
| extend SqlEdition = case(sku.name hasprefix_cs "GP_", "Standard", sku.name hasprefix_cs "HS_", "Standard", sku.name hasprefix_cs "BC_", "Enterprise", "Unknown")
| extend vCores = toint(sku.capacity)
| extend licensesNeeded = toint(round(vCores/2))
| project licensesNeeded, vCores, SqlEdition
| summarize sum(licensesNeeded) by SqlEdition
$allRecommendations = New-Object -TypeName "System.Collections.ArrayList"
Get-AzSubscription | ForEach-Object {
$sub = $_
Set-AzContext -SubscriptionId $_.Id
$recommendations = Get-AzSqlServer | Get-AzSqlDatabase | Where-Object Edition -ne "System" | Get-AzSqlDatabaseSensitivityRecommendation
$recommendations | ForEach-Object {
$dbinfo = $_
$_.SensitivityLabels | ForEach-Object {
$newRecommandation = $_ | Select-Object @{label = 'SubscriptionId'; expression = {$sub.Id}}, @{label = 'ResourceGroupName'; expression = {$dbinfo.ResourceGroupName}}, @{label = 'ServerName'; expression = {$dbinfo.ServerName}}, @{label = 'DatabaseName'; expression = {$dbinfo.DatabaseName}}, *
$allRecommendations.Add($newRecommandation)
@cbattlegear
cbattlegear / esuresourcegraph.kql
Last active May 14, 2024 18:58
Azure Resource Graph Query
resources
| where type == "microsoft.hybridcompute/machines"
| where properties.osName == "windows"
| extend joinId = tolower(id)
| join kind = leftouter (
resources
| where type == "microsoft.azurearcdata/sqlserverinstances"
| extend arcMachineId = tolower(tostring(properties.containerResourceId))
| extend sqlVersion = tostring(properties.version)
| extend vCores = toint(properties.vCore)
@cbattlegear
cbattlegear / gist:615ebe348add4e65d43f795f6dfb518b
Created May 3, 2023 20:33
SQL Licensing Workbook ARM Template
{
"contentVersion": "1.0.0.0",
"parameters": {
"workbookDisplayName": {
"type": "string",
"defaultValue": "SQL Licensing Summary",
"metadata": {
"description": "The friendly name for the workbook that is used in the Gallery or Saved List. This name must be unique within a resource group."
}
},
# Run this first via Cloud Shell to create your Service Principal and get all the needed information
$sp = New-AzADServicePrincipal -DisplayName "Arc-for-servers" -Role "Owner"
$tenantId= (Get-AzContext).Tenant.Id
$sp.AppId
$sp.PasswordCredentials.SecretText
$tenantId
# Run this locally after all permissions have been set.
$servicePrincipalAppId="Value of $sp.AddId"
@cbattlegear
cbattlegear / dma_csv_list_run.ps1
Created February 28, 2023 16:21
Powershell to do DMA capture for DBMI assessment
# Need two parameters when called, the input CSV list of all servers and the Output folder
# CSV File must be structured like below (Note ServerName,InstanceName header must be included)
# ServerName,InstanceName
# localhost,MSSQLSERVER
param($CsvPath, $OutFolder)
mkdir -p "$OutFolder\SqlAssessments"