Skip to content

Instantly share code, notes, and snippets.

View rcabr's full-sized avatar
🏛️
Solving problems

rcabr rcabr

🏛️
Solving problems
View GitHub Profile
@rcabr
rcabr / Audit-ResourcePolicies.ps1
Last active June 22, 2022 21:44
Azure Automation script to report Azure Policy violations
<#
.Synopsis
Gets all current policy violations for the specified policies,
gets the owning e-mail group by examining the managedBy tag (walks up from resource to resource group to subscription),
gets the last people who touched the resource (by examining audit events in the past 90 days),
and either:
- sends an e-mail to each user with their resources and violations
- sends the full list to a default e-mail address.
.Description
@rcabr
rcabr / Assign-InitiativeToCurrentSubscription.ps1
Created June 11, 2018 15:14
Azure PowerShell to assign the specified initiative (policy set) to the current subscription
#
# Assigns the specified initiative to the currently selected subscription
#
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, HelpMessage="The name of the initiative to assign. Example: 'Data Classification'")]
[string]$InitiativeName
)
Get-AzureRmContext # print out current context to inform operator
@rcabr
rcabr / Apply-TagsToResourcesInRg.ps1
Created June 11, 2018 15:12
Azure Powershell to apply two tags (appId, environment) to all resources in the specified resource group
# Applies these two tags (appId, environment) to all resources in the specified resource group.
# Optional: resourcePattern will be used to select a subset of resources to apply tags to.
[CmdletBinding()]
param(
[string] $resourceGroupName,
[string] $appIdValue,
[string] $environmentValue,
[string] $resourcePattern = ""
)
@rcabr
rcabr / Remove-OldAzureRmResourceGroups.ps1
Last active August 26, 2022 11:03
Delete Azure resource groups that were created n or more days ago
$days = 7
$pointInTime = [DateTime]::Now.AddDays(-$days);
$horizon = $pointInTime.AddDays(-$days);
"===Removing resource groups created between $horizon and $pointInTime==="
# Get potential log entries
$logs = @()
$logs += Get-AzureRmLog -StartTime $horizon -EndTime $pointInTime -Status "Succeeded" -ResourceProvider "Microsoft.Resources" -WarningAction "SilentlyContinue" `
| Select-Object ResourceGroupName, ResourceId, @{Name="EventNameValue"; Expression={$_.EventName.Value}}, @{Name="OperationNameValue"; Expression={$_.OperationName.Value}}, EventTimestamp, @{Name="HttpVerb"; Expression={$_.HttpRequest.Method}} `
@rcabr
rcabr / Get-AzureRmEarliestDeploymentsByResourceGroup
Created June 1, 2018 14:23
Azure PowerShell statement to find the earliest deployment in each resource group in a subscription
# Assumes the Azure RM context is already set (logged in, subscription selected).
# Returns a list of Resource Group Names and the Earliest Timestamp (from deployment activity) for each.
((Get-AzureRmResourceGroup | Select-Object ResourceGroupName) | Get-AzureRmResourceGroupDeployment)`
| Select-Object ResourceGroupName, @{Name="Created"; Expression = {$_.Timestamp}} `
| Sort-Object ResourceGroupName, Created `
| Group-Object ResourceGroupName `
| Select-Object Name, @{Name="EarliestTimestamp"; Expression={`
$_.Group.Created | Measure-Object -Minimum | Select-Object -ExpandProperty Minimum `
}}
@rcabr
rcabr / Assign-ArmResourcePolicies.ps1
Created June 14, 2017 13:30
Azure Resource Policies: Assign all resource policies (*.json files) in the current folder to a resource group
# Assigns all resource policies in the current folder to the specified resource group.
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[string]$ResourceGroupName
)
# get subscription or ask user to log in
$subscription = Get-AzureRmSubscription;
@rcabr
rcabr / PentahoDI-CreateDocumentDBAuthHeader.js
Created May 4, 2017 20:22
Pentaho - Azure DocumentDB - Access Control on Document Resources
// Problem:
// Use the DocumentDB REST API from Pentaho DI (Spoon)
// Produce an authorization header for the request
//
// Solution:
// 1. Read this document https://docs.microsoft.com/en-us/rest/api/documentdb/access-control-on-documentdb-resources
// 2. Assuming you have serialized your record to JSON and wish to publish it via a REST Client step
// 3. Add a "Modified Java Script Value" step to your Pentaho Transformation with the following body.
//Input
@rcabr
rcabr / keybase.md
Last active May 29, 2017 02:29
My Keybase Proof

Keybase proof

I hereby claim:

  • I am rcabr on github.
  • I am rcabr (https://keybase.io/rcabr) on keybase.
  • I have a public key ASAf5DroJ22Q0-O9RC4mJ7uJxGbHfwiggg2VQIW8ZfOVYQo

To claim this, I am signing this object: