Azure Workshop contd…
- Refer Here for the Gist
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>
- cmd-lets (commandlets):
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
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
- Use pipelines to replace main.py to main.rb
childitem, recursive, where(Where-Object)
- Foreach
"AWS", "Azure", "DevOps" | foreach { "The course is $_"}
- When we use a pipe symbol
- 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
}
- 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 }