Workshops Classroomnotes 22/Dec/2022

Azure Workshop contd…

Powershell Introduction

  • Powershell:
    • cmd-lets (commandlets):
      • The output of any cmd-let is generally an object.
      • Every object has properties or members
      • Every cmd let will be in the form of <verb>-<noun>

Getting familiar with powershell cmd lets

  • What is item in Powershell?
  • Item can be file, folder, profile, registry key
  • Lets use Powershell to create, view files
  • To search any cmdlet Get-Command *-Item
    Preview
    Preview

learning activity:

  • I want to create a folder
qtecommerce
   src
        main.py  
   test
        main.py
  • Refer Here for the script created
  • Pipelines in Powershell

    • When we use a pipe symbol | the object gets transferred so we can still use Properties
      Get-Items "~\Downloads\*" | Where Length -lt 100
      Preview
    • Use pipelines to replace main.py to main.rb childitem, recursive, where(Where-Object)
    • Foreach "AWS", "Azure", "DevOps" | foreach { "The course is $_"}
  • Sample:
$python_files = Get-ChildItem -Path .\qtecommerce\ -Recurse -Depth 3 -Include '*.py'

foreach ( $python_file in $python_files ){
    $parent_path = $python_file.Directory
    Rename-Item -Path $python_file -NewName "$parent_path\main.rb"

}

Get-ChildItem -Path .\qtecommerce\ -Recurse -Depth 3 -Include '*.py' | foreach { Rename-Item -Path $_ -NewName "$($_.Directory)\main.rb" }

Azure Powershell

  • Azure Powershell is a module that will have cmdlets in the form of <verb>-Az<noun>

Activity: Apply tags to existing Linux VM

  • Lets create a free tier linux vm.
  • Post creation we would want to apply tags
    • Environment: Dev
    • Project: azurelearning
    • Release: v1.1
    • Team: qtazure
  • The first version
$all_resorces_in_group = Get-AzResource -ResourceGroupName 'activity1'

$tags = @{Environment="Dev"}
$tags += @{Project="azurelearning"}
$tags += @{Release="v1.1"}
$tags += @{Team="qtazure"}

foreach ($resource in $all_resorces_in_group) {
    Set-AzResource -ResourceGroupName $resource.ResourceGroupName -Name $resource.Name `
        -ResourceType $resource.ResourceType -Tag $tags
}

Preview

  • Refer Here for the script for performing activity 1

Activity 2: Shutdown Vms to reduce costs

  • Get all the virtual machines with tag Enviroment = Dev and shutdown
$vms = Get-AzResource -ResourceType 'Microsoft.Compute/VirtualMachines' -TagName 'Environment' -TagValue 'Dev'

foreach ($vm in $vms) {
    Stop-AzVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Force
}


# Get-AzResource -ResourceType 'Microsoft.Compute/VirtualMachines' -TagName 'Environment' '
# -TagValue 'Dev' | foreach { Start-AzVM -ResourceGroupName '
# $_.ResourceGroupName -Name $_.Name }

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner