Skip to content

Instantly share code, notes, and snippets.

@GustavoAmerico
Last active July 29, 2025 13:44
Show Gist options
  • Select an option

  • Save GustavoAmerico/9726b03880e13474d0a7c064dfb32372 to your computer and use it in GitHub Desktop.

Select an option

Save GustavoAmerico/9726b03880e13474d0a7c064dfb32372 to your computer and use it in GitHub Desktop.
Esse arquivo centraliza alguns comandos muito utilizados, por mim, que exigem uma montagem verbosa
function Get-PublicIPV6 {
#This function return the public IP of the machine
#It is used to register the IP on Azure SQL Server firewall rules
try {
$mypublicip = (Invoke-WebRequest https://v6.api.ipinfo.io/ip | Select-Object -Property Content).Content.Trim();
}
catch {
}
return $mypublicip;
}
function Get-PublicIPV4 {
#This function return the public IP of the machine
#It is used to register the IP on Azure SQL Server firewall rules
try {
$mypublicip = (Invoke-WebRequest https://ipinfo.io/ip | Select-Object -Property Content).Content.Trim();
}
catch {
$mypublicip = (Resolve-DnsName myip.opendns.com -server resolver1.opendns.com -type A | select -expand IP4Address);
}
return $mypublicip;
}
function Az-SqlServer-RegisterIP {
#This script is a help for create firewall rule on azure sql servers on all subscription
param([Parameter(Mandatory = $true)][System.String]$PcName, [Parameter(ValueFromPipeline = $true)]$subscriptionName)
#Read public ip
$mypublicip = Get-PublicIPV4;
#Read all Sql Server from context subscription
$servers = (az sql server list --subscription $subscriptionName --query "[].{id:id,name:name,resourceGroup:resourceGroup}" | ConvertFrom-Json)
$RuleName = "$PcName";
#Create firewall rule for all sql server
$servers | % { az sql server firewall-rule create --name "$RuleName" --subscription $subscriptionName --resource-group $_.resourceGroup --server $_.name --end-ip-address "$myPublicIp" --start-ip-address "$myPublicIp" }
}
function Az-ImportAwsRoute53 {
param(
[Parameter(Mandatory = $true)][System.String]$resourceGroup ,
[Parameter(Mandatory = $true)][System.String]$zoneName ,
[Parameter(Mandatory = $true)][System.String]$subscriptionName
)
$AllZones = Get-R53HostedZones | Where-Object Name -EQ "$zoneName."
foreach ($domain in $AllZones) {
$Zone = (Get-R53ResourceRecordSet -HostedZoneId $($Domain.id)).ResourceRecordSets
foreach ($Record in $Zone) {
foreach ($RecordItem in $($($Record.ResourceRecords).Value).Split(' ')) {
$recordName = $($Record.Name.Replace(".$zoneName.", "").Replace("$zoneName.", ""))
if (!$recordName) { $recordName = "@" }
$er = (Invoke-Expression -Command "az network dns record-set $($Record.type.ToString().ToLower()) show -n `"$($recordName)`" -g $resourceGroup -z $zoneName --subscription $subscriptionName") 2>&1
if ($lastexitcode) {
echo "Name=$recordName Type=$($Record.Type.ToString())"
switch ($Record.Type.ToString().ToLower()) {
'a' {
$command = "az network dns record-set $($record.type.ToString().ToLower()) add-record -a `"$RecordItem`" -n `"$($recordName)`" -g $resourceGroup -z $zoneName --ttl $($record.Ttl) --subscription $subscriptionName" ;
}
'ns' {
if (-not $recordName.Equals("@") ) {
$command = "az network dns record-set $($record.type.ToString().ToLower()) add-record -d `"$RecordItem`" -n `"$($recordName)`" -g $resourceGroup -z $zoneName --ttl $($record.Ttl) --subscription $subscriptionName" ;
}
}
'cname' {
$command = "az network dns record-set $($record.type.ToString().ToLower()) set-record -c `"$RecordItem`" -n `"$($recordName)`" -g $resourceGroup -z $zoneName --ttl $($record.Ttl) --subscription $subscriptionName" ;
}
}
echo $command
if ($command) { Invoke-Expression -Command $command ; }
}
#
# echo $command;
#Invoke-Expression -command $command;
}
}
}
}
function Az-CleanupACR {
#Essa função deve ser utilizada para apagar imagens não utilizadas
param(
[Parameter(Mandatory = $true)] $acrAccountName,
$takeLastImages = 5,
$likeRepositories = '',
$selectTagPattern = '^([0-9]+(\.)?){3,4}$'
)
#Get all repository where the name like with parameter $likeRepositories
$repositories = az acr repository list --name $acrAccountName --query ("[?contains(@, '" + $likeRepositories + "')]") | ConvertFrom-Json ;
#Get all image tags for repositories
$allTagsObjects = ( $repositories | % { az acr repository show-tags -n $acrAccountName --repository $_ --orderby time_desc --output 'json' --detail --query "map(&{ID: @.digest, Tag: @.name, repository: '$_' },@)" | Join-String | ConvertFrom-Json | Select-Object -Skip $takeLastImages } )
# Filtering the images ID with only one tag
$uniqueTags = $allTagsObjects | Where -FilterScript { $id = $_.ID; ($allTagsObjects | Where-Object ID -EQ $id).Count -EQ 1 } | Where-Object Tag -Match $selectTagPattern
$uniqueTags | % { $imageToRemove = ($_.repository + ":" + $_.Tag); write-host $imageToRemove; az acr repository delete -n $acrAccountName --image $imageToRemove -y }
}
function Az-Add-StorageAccount-As-WindDisk {
# This script help you setting an azure storage account as a network folder or windows disk
# Is need Azure CLI installed
param(
[System.String][Parameter(Mandatory = $true)]
$resourceGroupName ,
[System.String][Parameter(Mandatory = $true)]
$storageAccountName ,
[System.String][Parameter(Mandatory = $true)]
$subscription ,
[System.String][Parameter(Mandatory = $true)]
$folder
)
# Get the storage file host
$storageFileHost = (az storage account show -g $resourceGroupName -n $storageAccountName -o tsv --subscription $subscription --query 'primaryEndpoints.file' | % { ([System.Uri]::new($_).Host) });
# Get the first key for access storage
$storageKey = (az storage account keys list -g $resourceGroupName -n $storageAccountName --subscription $subscription --query '[0].value' -o tsv);
#Save credentials on windows vault
Invoke-Expression -Command ("cmdkey /add:$storageFileHost /user:AZURE\$storageAccountName /pass:$storageKey" )
# Register folder as network directory
Invoke-Expression -Command("net use \\$storageFileHost\$folder");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment