Skip to content

Instantly share code, notes, and snippets.

View kpatnayakuni's full-sized avatar
🏠
Working from home

Kiran Patnayakuni kpatnayakuni

🏠
Working from home
View GitHub Profile
$FolderName = "C:\Temp1"
$DestinationPath = "C:\Temp2"
$Dates = Get-ChildItem -Path $FolderName -ErrorAction SilentlyContinue | Select-Object -Property @{Name = "CreationTime"; Expression = { $_.CreationTime.Date } } -Unique
$Files = Get-ChildItem -Path $FolderName -ErrorAction SilentlyContinue
foreach ($Dt in $Dates.CreationTime) { $Files | Where-Object { $_.CreationTime.Date -eq $Dt } | Compress-Archive -DestinationPath "$DestinationPath\$($Dt.ToString("MMM_yyyy")).zip" -Force }
## On the NEW VM in which User Assigned Managed Identity enabled
# Enter-PSSession -ComputerName $vmPIP -Credential $credential
# Install NuGet package provider where Az module is availble
Install-PackageProvider -Name NuGet -Force
# Install the Az module
Install-Module -Name Az -Force
# Login to Azure with managed identity
# Get the public ip of the new VM
$vmPIP = Get-AzPublicIpAddress -ResourceGroupName $rgName -Name $vmName | % IpAddress
# Assign the identity and enable User Assigned Managed Identity on Virtual Machine.
$null = Update-AzVM -ResourceGroupName $rgName -VM $vm `
-IdentityType UserAssigned -IdentityID $identity.Id
# Grant GET permissions to secrets on Key Key Vault to managed identity
Set-AzKeyVaultAccessPolicy -ResourceGroupName $rgName -VaultName $keyVaultName `
-ServicePrincipalName $identity.ClientId -PermissionsToSecrets get
# Grant Reader role to Managed Identity on Key Vault
$null = New-AzRoleAssignment -ApplicationId $identity.ClientId `
-RoleDefinitionName Reader -Scope $keyVault.ResourceId
# Add a secret to Key Vault
$null = Set-AzKeyVaultSecret -VaultName $keyVaultName `
-Name $userName -SecretValue $securePassword
# Create Azure Key Vault
$keyVaultName = 'testakv99'
$keyVault = New-AzKeyVault -ResourceGroupName $rgName `
-Name $keyVaultName -Location $location
# Install and Import the module
$moduleName = 'Az.ManagedServiceIdentity'
Install-Module -Name $moduleName -Force
Import-Module -Name $moduleName
# Create User Assugned Managed Identity
$identityName = 'amuai'
$identity = New-AzUserAssignedIdentity -Name $identityName `
-ResourceGroupName $rgName -Location $location
# Create a Resource Group
$rgName = 'Test-RG'
$location = 'westus'
$null = New-AzResourceGroup -Name $rgName -Location $location
# Create a Virtual Machine
$vmName = 'Test-VM'
$userName = 'sysadmin'
$plainTextPassword = 'P@ssw0rd!'
$securePassword = $plainTextPassword | ConvertTo-SecureString -AsPlainText -Force