Skip to content

Instantly share code, notes, and snippets.

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

Vukašin Terzić vukasinterzic

☁️
Inspire before expire 💯
View GitHub Profile
@vukasinterzic
vukasinterzic / Gist-From-PowerShell.ps1
Created October 18, 2023 04:00
this script shows how to execute code stored in Gist from PowerShell.
# Replace with your Gist's raw URL
$gistUrl = "https://gist.githubusercontent.com/yourusername/raw/yourgistID/yourscript.ps1"
#Option 1
Invoke-RestMethod -Uri $gistUrl | Invoke-Expression
#Option 2
Invoke-Expression ((New-Object System.net.WebClient).DownloadString('$gistUrl'))
@vukasinterzic
vukasinterzic / Remove-PublicIPFromNSG.ps1
Last active October 18, 2023 04:34
Adds last Public IP from the list of allowed IPs in NSG for RDP
$SubscriptionName = ""
$RGName = ""
$NSGName = ""
$RuleName = ""
# Ensure Azure modules are installed
if (-not (Get-Module -ListAvailable -Name Az.Network)) {
Install-Module -Name Az.Network -AllowClobber -Scope CurrentUser
}
@vukasinterzic
vukasinterzic / Add-PublicIPToNSG.ps1
Last active October 18, 2023 01:19
Adds your current Public IP to list of allowed IPs in NSG for RDP
$SubscriptionId = ""
$RGName = ""
$NSGName = ""
$RuleName = ""
# Ensure Azure modules are installed
if (-not (Get-Module -ListAvailable -Name Az.Network)) {
Install-Module -Name Az.Network -AllowClobber -Scope CurrentUser
}
Function Manage-AzureRoleAssignment {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[ValidateSet('Get', 'Set')]
[string]$Action,
[string]$Scope,
[string]$Role,
[string]$UserOrServicePrincipal,
[ValidateSet('User', 'ServicePrincipal')]
<#
This script will collect all policy assignments in available subscriptions and list them as a table.
#>
# Log in to your Azure account
#Connect-AzAccount
function Get-ScopeName($scopeId) {
if ($scopeId -match "/providers/Microsoft.Management/managementGroups/") {
$mgId = $scopeId -replace "/providers/Microsoft.Management/managementGroups/", ""
@vukasinterzic
vukasinterzic / Get-ResourceGroupCost.ps1
Created March 12, 2023 18:30
PowerShell script to retrieve Resource Group cost.
function Get-ResourceGroupCost {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$ResourceGroupName,
[Parameter(Mandatory = $false)]
[int]$DaysBack = 30
)
#To simplify this example, we can define hashtable with naming standard:
$ResourceNamesHash = @{}
$ResourceNamesHash.Add("Microsoft.Compute/virtualMachines", "VM-`$Subscription-`$Description")
$ResourceNamesHash.Add("Microsoft.KeyVault/vault", "kv-`$Environment-`$Description")
$ResourceNamesHash.Add("Microsoft.Network/vpnGateways", "vpng-`$VNetName-`$Description")
# Define a function to get the abbreviation for a given resource type
function Get-ResourceNamingConvention {
param(
[Parameter(Mandatory=$true)]
function Get-ExistingResourceTypeAndAbbreviation {
param(
[Parameter(Mandatory=$true)]
[string]$ResourceName
)
# Check if the resource exists
$resource = Get-AzResource -Name $ResourceName -ErrorAction SilentlyContinue
if (!$resource) {
Write-Error "Resource '$ResourceName' not found."
function Get-ResourceTypeAbbreviation {
param(
[Parameter(Mandatory=$true)]
[string]$ResourceType
)
if ($tableData.Resource -contains $ResourceType) {
$Answer = $tableData | Where-Object Resource -like $ResourceType | Select-Object -ExpandProperty Abbreviation
Write-Host "Correct abbreviation for $ResourceType is: $Answer"
# Specify the URL of the website and the ID of the table we want to scrape
$url = "https://learn.microsoft.com/en-us/azure/cloud-adoption-framework/ready/azure-best-practices/resource-abbreviations"
# Use Invoke-WebRequest with -UseBasicParsing to download the webpage and extract the HTML code
$html = Invoke-WebRequest -Uri $url -UseBasicParsing | Select-Object -ExpandProperty Content
#If everything else fails user regex!
#Use regular expressions to extract the table data from the HTML code
$tableRegex = '<table>.*?<tbody>(.*?)</tbody>.*?</table>'
$rowRegex = '<tr>.*?<td>(.*?)</td>.*?<td>(.*?)</td>.*?<td>(.*?)</td>.*?</tr>'