Skip to content

Instantly share code, notes, and snippets.

@feliperomero3
Last active November 6, 2022 23:22
Show Gist options
  • Save feliperomero3/9506fff90821a317165b75913128df81 to your computer and use it in GitHub Desktop.
Save feliperomero3/9506fff90821a317165b75913128df81 to your computer and use it in GitHub Desktop.
PowerShell Az Module snippets
# This command gets the certificate named TestCert01 from the key vault named ContosoKV01.
# To download the certificate as pfx file, run following command.
# These commands access SecretId and then save the content as a pfx file.
$cert = Get-AzKeyVaultCertificate -VaultName "ContosoKV01" -Name "TestCert01"
$secret = Get-AzKeyVaultSecret -VaultName $vaultName -Name $cert.Name -AsPlainText
$secretByte = [Convert]::FromBase64String($secret)
$x509Cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($secretByte, "", "Exportable,PersistKeySet")
$type = [System.Security.Cryptography.X509Certificates.X509ContentType]::Pfx
$pfxFileByte = $x509Cert.Export($type, $password)
# Write to a file
[System.IO.File]::WriteAllBytes("KeyVault.pfx", $pfxFileByte)
# Source: https://docs.microsoft.com/en-us/archive/blogs/kv/get-started-with-azure-key-vault-certificates#retrieve-pfx-file--add-password-back
# Latest: https://docs.microsoft.com/en-us/powershell/module/az.keyvault/get-azkeyvaultcertificate?view=azps-5.9.0#example-2--get-cert-and-save-it-as-pfx
# Get your azure's costs for the current billing period
Get-AzConsumptionUsageDetail | Measure-Object -Property PretaxCost -Sum
# List Resources under ResourceGroup "MoneySmart"
Get-AzResource | Where-Object { $_.ResourceGroupName -eq 'MoneySmart' } | Format-Table -Property ResourceName, ResourceType, Location
# List Resources by Resource Type
Get-AzResource | Where-Object { $_.ResourceType -eq 'Microsoft.Network/networkSecurityGroups' } | Format-Table -Property ResourceGroupName, ResourceName, Location
# List Resources by using multiple conditions
Get-AzResource | Where-Object { $_.ResourceType -eq 'Microsoft.Storage/storageAccounts' -and $_.ResourceGroupName -like 'sf-p*' } | Format-Table -Property ResourceGroupName, ResourceName, Location
# Retrieving a network security rule config using only the name
Get-AzNetworkSecurityGroup -Name nsg1 -ResourceGroupName rg1 | Get-AzNetworkSecurityRuleConfig -Name 'rdp-rule'
# Get existing VM size
Get-AzVM -ResourceGroupName 'vmvs2019kof97' -VMName 'vsWinVM' | Select-Object -ExpandProperty HardwareProfile
# Get virtual machine sizes for a location
Get-AzVMSize -Location 'South Central US'
# Get available sizes for an existing virtual machine
Get-AzVMSize -ResourceGroupName 'vmvs2019kof97' -VMName 'vsWinVM'
# Filter available sizes for an existing virtual machine by a Name prefix
Get-AzVMSize -ResourceGroupName 'vmvs2019kof97' -VMName 'vsWinVM' | Where-Object -Property 'Name' -Match 'Standard_D4'
# Get virtual machine status
Get-AzVM -Name 'vsWinVM' -ResourceGroupName 'vmvs2019kof97' -Status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment