Skip to content

Instantly share code, notes, and snippets.

View vukasinterzic's full-sized avatar
☁️
Inspire before expire 💯

Vukasin Terzic vukasinterzic

☁️
Inspire before expire 💯
View GitHub Profile
function Update-PolicyAssignmentExclusionList {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$PolicyAssignmentName,
[Parameter(Mandatory = $true)]
[string]$ResourceGroupName,
using namespace System.Net
# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)
#Get Az Subscription and Set Context
$SubscriptionName = "Microsoft Azure Sponsorship"
Get-AzSubscription -SubscriptionName $SubscriptionName | Set-AzContext
#Get parameter Resource Group Name
#PowerShell wrapper script for WinGet
#List of apps that will be installed like this:
#silently
#scope: all users
#preferabbly from msstore
$apps = @(
@{name = "Microsoft.PowerShell"} #MicrosoftPowerShell
@{name = "Microsoft.AzureCLI" } #Azure CLI
# Get variables
$tagName = "ExpirationDate"
$prodTagValue = "Production"
$emailFrom = Get-AutomationVariable -Name "EmailFrom"
$emailTo = Get-AutomationVariable -Name "EmailTo"
$smtpServer = Get-AutomationVariable -Name "SmtpServer"
$logPath = "C:\Temp\AzureResourceGroupDeletion.log"
$daysBeforeExpiration = Get-AutomationVariable -Name "DaysBeforeExpiration"
# Get all resource groups with the ExpirationDate tag
#Azure CLI cmd of the day - January 2022:
#1 List existing subscriptions, select one to work with, list default:
az account list -o table
az account set -s '<Subscription Name>'
az account list --query "[?isDefault]" -o table
Write-Output "Connecting ..."
# Ensures you do not inherit an AzContext in your runbook
Disable-AzContextAutosave -Scope Process
# Connect to Azure with system-assigned managed identity
$AzureContext = (Connect-AzAccount -Identity).context
# Set and store context
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
$VMs = @('VM1', 'VM2', 'VM3', 'VM4')
$Tag = @{VMPasswordReset = "Yes"}
foreach ($VMName in $VMs) {
$VM = Get-AzResource -ResourceType "Microsoft.Compute/VirtualMachines" -Name $VMName
Update-AzTag -ResourceId $VM.ResourceId -Tag $Tag -Operation Merge
}
@vukasinterzic
vukasinterzic / Create-AzureKeyVault.ps1
Created January 17, 2022 22:53
Create new KeyVault with PowerShell
#Create new KeyVault with PowerShell
$KeyVault = @{
Name = "myKeyVaultName"
ResourceGroupName = "myResourceGroupName"
Location = "myLocation"
EnabledForDeployment = $false
EnabledForTemplateDeployment = $false
EnabledForDiskEncryption = $false
tags = @{
@vukasinterzic
vukasinterzic / Create-AzureAutomationAccount.ps1
Created January 17, 2022 22:08
Create an Azure Automation Account with PowerShell
#Create an Azure Automation Account with PowerShell:
$automationAccount = @{
Name = "MyAutomationAccount";
ResourceGroupName = "MyResourceGroup";
Location = "westus2";
Tags = @{
"Environment" = "Test";
"Project" = "BlogPost";
};
@vukasinterzic
vukasinterzic / Replace-AzTagValues.ps1
Last active January 14, 2022 22:47
Replace Azure Tag Value, keep the Tag Name
#Task 1: Replace Tag Value, keep Tag Name:
# Get all Az Subscriptions
$Subscriptions = Get-AzSubscription
$TagName = 'YourTagName'
$WrongValue = 'wrong tag value'
$CorrectValue = 'new tag value'