CodeBunk- Professional AOM Training v1 #1
prabhu490730
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Note: this contains code snippets, command references or any source code blocks that are used during the training
Day
1 ReferenceIn PowerShell ISE, Prompt
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope LocalMachine
Write-Host "Welcome to the World of PowerShell" -ForegroundColor Green
script1.ps1
Write-Host "Welcome to the World of PowerShell" -ForegroundColor Green
$hostName = Read-Host -Prompt "Enter your Workstation Name ..."
Write-Host "Connecting to $hostName"
script1.ps1
Write-Host "Welcome to the World of PowerShell" -ForegroundColor Green
$hostName = Read-Host -Prompt "Enter your Workstation Name ..."
Write-Host "Connecting to $hostName" -ForegroundColor Yellow
Test-Connection -ComputerName $hostName -Count 1
script1.ps1
Write-Host "Welcome to the World of PowerShell" -ForegroundColor Green
$hostName = Read-Host -Prompt "Enter your Workstation Name ..."
Write-Host "Connecting to $hostName" -ForegroundColor Yellow
Test-Connection -ComputerName $hostName -Count 1
Test-Connection -ComputerName $hostName -Count 1 | select -Property IPV4Address,IPV6Address
script2.ps1
#using conditional statements
$x = 10
if($x -le 0)
{
Write-Host "Invalid Input" -ForegroundColor Red
}
else
{
Write-Host "Value is Accepted ..." -ForegroundColor Green
}
script3.ps1
$x = 45
switch($x)
{
1 { Write-Host "Value is One" }
2 { Write-Host "Value is Two" }
22 { Write-Host "Value is Twenty Two" }
22 { Write-Host "Value is Twenty Two Again" }
default { Write-Host "Value is Something Which I can't Understand!" -ForegroundColor Yellow }
}
script4.ps1
$names = @("Ramkumar", "Suresh", "Sandeep", "Aprajitha", "Dinesh")
for($i=0;$i -lt $names.Length;$i++)
{
$currentName = $names[$i]
}
script5.ps1
$names = @("Ramkumar", "Suresh", "Sandeep", "Aprajitha", "Dinesh")
foreach($name in $names)
{
Write-Host "Working with $name" -ForegroundColor Yellow
}
Write-Host "Different approach to looping ..."
$names | foreach { Write-Host "Working with $_" }
Write-Host "Another Approach which is quite advanced ..."
$names | % { $_ }
script6.ps1
$userprofile = @{id=1;name="Rajkumar";location="Hyderabad"}
foreach($key in $userprofile.Keys)
{
$value = $userprofile[$key]
}
Write-Host "Name of the Profile : " $userprofile["name"] -ForegroundColor Yellow
$userprofile.Keys | % { $userprofile[$_] }
@(1,2,3,4,5) | % { $_ }
@(1..5) | ? { $_ -ge 3 } | % { $_ }
script7.ps1
backtick operator
Write-Host "Wells Fargo,
nBangalore" Write-Host "ID
tNametAddress
tStatus"script8.ps1
Regular Expression Support
$phone = "99999-30303"
$pattern = "^\d{3,5}-\d{3,5}$ "
$match = $phone -match $pattern
Write-Host $match -ForegroundColor Green
Updated ...
Regular Expression Support
$phone = "99999-30303"
$pattern = "^\d{3,5}-\d{3,5}$ "
^ starts with
\d digits
{} condition
{3,5} - min of 3 and max of 5 (min of 3 digits and max of 5 digits)
$ ends with
$match = $phone -match $pattern
Write-Host $match -ForegroundColor Green
script9.ps1
PowerShell brackets
{} () []
{} - Enclosing Statements / Blocks - Dictionaries
() - Passing Arguments, Create Array
[] - Accessing Array Elements, Access to Dictionary Values and Filter using Regular Expressions
$names = @("Raj", "Ram", "Mahesh", "Uma")
foreach($name in $names)
{
if($name -match "Ra")
{
Write-Host $name
}
}
Write-Host "First Element: " $names[0]
script10.ps1
$hostsDataFilePath = "C:\0 - OAM - v1\Data\Hosts.csv"
$hosts = Import-Csv -Path $hostsDataFilePath -Encoding ASCII
foreach($hostInfo in $hosts)
{
if($hostInfo.Type -eq "Server")
{
Write-Host "Connecting to" $hostInfo.HostName " (Located in" $hostInfo.Location ")..." -ForegroundColor Yellow
}
Hosts.csv
HostName,Location,Type
RAMTHINKPAD,Bangalore,Server
RAMTHINKPAD001,Mysore,Laptop
RAMTHINKPAD002,Hyderabad,Server
RAMTHINKPAD003,Chennai,Network Storage
RAMTHINKPAD004,New Delhi,Database Server
script11.ps1
$hostsDataFilePath = "C:\0 - OAM - v1\Data\Hosts.csv"
$hostsDataOutputPath = "C:\0 - OAM - v1\Data\Host-Status.csv"
$hosts = Import-Csv -Path $hostsDataFilePath -Encoding ASCII
$outputs = @()
$serverEntries = 0
foreach($hostInfo in $hosts)
{
if($hostInfo.Type -eq "Server")
{
$serverEntries += 1
}
$outputs | Export-Csv -Path $hostsDataOutputPath -NoTypeInformation
$unavailableEntries = @($outputs | ? { $ .Status -eq "Not Available" }).Count
$availableEntries = @($outputs | ? { $.Status -eq "Available" }).Count
Write-Host "Total Available Hosts ..." $availableEntries " (Out Of)" $serverEntries
Write-Host "Total Unavailable Hosts ..." $unavailableEntries" (Out Of)" $serverEntries
Get-Process | Sort-Object -Descending -Property "CPU" | select -First 10
script11.ps1
$hostsXmlPath = "C:\0 - OAM - v1\Data\Hosts.xml"
$xmlFileContents = Get-Content -Path $hostsXmlPath
$hostsXmlData = [xml] $xmlFileContents
$outputFolder = "C:\0 - OAM - v1\Data"
foreach($hostInfo in $hostsXmlData.Hosts.Host)
{
$hostName = $hostInfo.HostName
$location = $hostInfo.Location
$priority = $hostInfo.Priority
$type = $hostInfo.Type
}
Hosts.xml
RAMTHINKPAD Bangalore 1 Server RAMTHINKPAD001 Bangalore 2 Server RAMTHINKPAD002 Hyderabad 1 Desktop RAMTHINKPAD003 Chennai 1 Server RAMTHINKPAD004 Mysore 2 Laptopscript12.ps1
param(
[string] $hostsXmlInputPath,
[string] $outputFolder
)
$xmlFileContents = Get-Content -Path $hostsXmlInputPath
$hostsXmlData = [xml] $xmlFileContents
foreach($hostInfo in $hostsXmlData.Hosts.Host)
{
$hostName = $hostInfo.HostName
$location = $hostInfo.Location
$priority = $hostInfo.Priority
$type = $hostInfo.Type
}
in the powershell prompt,
.\script12.ps1 -hostsXmlInputPath "C:\0 - OAM - v1\Data\Hosts.xml" -outputFolder "C:\0 - OAM - v1\Data"
Exercise #1
As per the requirement, you're expected to automate the following
a. Find out any files that are of type of .BAK / .TXT / .DAT
in the system, which is more than the size of 1 GB
b. Create a CSV report having the following information
c. It doesn't matter which folder in the system, it has to be recursively to be found.
d. the path should be parameterized c:\
utilities.psm1
function get-hosts {
param(
[Parameter(Mandatory = $true)] [string] $hostsFilePath,
[Parameter(Mandatory = $true)] [string] $hostType
)
}
simple-functions.ps1
Import-Module .\utilities.psm1
$hostsFilePath = "C:\0 - OAM - v1\Data\Hosts.xml"
$servers = get-hosts -hostsFilePath $hostsFilePath -hostType Server
$servers | % { $_.HostName }
hosts.xml
RAMTHINKPAD Bangalore 1 Server RAMTHINKPAD001 Bangalore 2 Server RAMTHINKPAD002 Hyderabad 1 Desktop RAMTHINKPAD003 Chennai 1 Server RAMTHINKPAD004 Mysore 2 LaptopEvnet Logs
Get-WinEvent -ListLog * | Sort-Object -Descending "RecordCount" | select LogName, RecordCount
Get-WinEvent System -Oldest | select -First 10 | Format-Table Index,Source,Message
Get-WinEvent System -Oldest | select -First 10 | Format-Table Index,Source,Message
Get-WinEvent System | Where-Object { $_.Message -match "disk" } | select -first 10 | format-list *
Get-WinEvent Microsoft-Windows-PowerShell/Operational | Where-Object { $_.Properties[2].Value -match "Invoke-WebRequest" } | format-list *
Get-WinEvent System | Group-Object Message | Sort-Object -Descending Count
New-EventLog -LogName ScriptEvents -Source PowerShellTraining
Write-EventLog -LogName ScriptEvents -Source PowerShellTraining -EventId 1234 -Message "Script Eventing Message"
PowerShell Remoting
md remoting
cd remoting
cls
dir
ssh-keygen
dir
multipass search
multipass
multipass aliases
multipass find
multipass
notepad cloud-init.yaml
users:
sudo: ALL=(ALL) NOPASSWD:ALL
ssh_authorized_keys:
notepad id_rsa
notepad id_rsa.pub
multipass launch -n trainingvm --cloud-init .\cloud-init.yaml
multipass info trainingvm
ssh -i .\id_rsa [email protected]
multipass launch --help
cls
install-docker.ps1
param(
[string] $hostName,
[string] $keyFilePath,
[string] $userName
)
$session = New-PSSession -HostName $hostName -UserName $userName -KeyFilePath $keyFilePath -SSHTransport
$session
Invoke-Command $session -ScriptBlock {
sudo apt-get update
}
Remove-PSSession $session
.\install-docker.ps1 -hostName 172.25.78.232 -userName vmadmin -keyFilePath .\id_rsa
Background Jobs
script1.ps1
Start-Job -ScriptBlock {
Start-Sleep -Seconds 10
}
Get-Job
script2.ps1
$listofJobs = 1..50
foreach($job in $listofJobs) {
while(@(Get-Job -State Running).Count -gt 10) {
Start-Sleep -Seconds 10
}
}
script3.ps1
$job = Start-Job -ScriptBlock {
1..10 | ForEach-Object {
$_
}
Write-Host 'Sleeping 2 seconds ...'
Start-Sleep -Seconds 2
$job | Receive-Job
Write-Host 'Sleeping for 5 seconds more ...'
Start-Sleep -Seconds 5
$job | Receive-Job
script4.ps1
$listOfJobs = 1..20
$jobs = foreach($job in $listOfJobs) {
while(@(Get-Job -State Running).Count -gt 10) {
Start-Sleep -Seconds 10
}
}
$jobs | Wait-Job | Receive-Job | Export-Csv output.csv -Append
$jobs | Remove-Job
Prometheus - Demonstrations
Multipass Installation
https://multipass.run/download/windows
Day 4
git clone https://github.com/iomegak12/simple-prometheus
cd simple-prometheus
docker compose up -d
(in your system, since it has older version of docker desktop,
)
docker images
docker network ls
docker ps
docker inspect simple-prometheus_p-network
try to get the gateway ip address
hostname
open the browser -> http://:8090/ping
docker run -d -t -p 1025:1025 -p 8025:8025 --name mailserver --network simple-prometheus_p-network mailhog/mailhog
docker ps
open the browser
http://:8025
open powershell in administrator mode
Send-MailMessage -SmtpServer ramthinkpad -Port 1025 -From [email protected] -To [email protected] -Subject "Test Mail" -Body "Test Mail Message" -BodyAsHtml
verify in the browser that you have received the mail ...
open the browser
http://:3000
Username: admin
password: admin
grafana dashboard should be opened up ...
open the browser
http://:9090
https://github.com/prometheus-community/windows_exporter/releases/download/v0.18.1/windows_exporter-0.18.1-amd64.exe
open the browser
http://:9182/metrics
find and replace the contents docker.yml with the following ...
global:
scrape_interval: 15s
scrape_configs:
static_configs:
static_configs:
static_configs:
labels:
purpose: training
in the command prompt,
docker-compose restart
in the browser
http://:9090/
status -> targets -> refresh
https://grafana.com/grafana/dashboards/14451
copy the ID
in the browser
http://:3000
Dashboards -> import
paste the id
select the prometheus data source -> prometheus default
click import
in the grafana dashboard
new dashboard
add new tile
select prometheus source
metric -> ping_request_count
aggregations -> sum
save the changes
End-to-End ELK Stack Case Study
10 mkdir practices
11 cd practices/
12 sudo apt-get install -y default-jre
13 java --version
14 clear
15 sudo apt-get install -y default-jdk
16 javac --version
17 clear
18 git clone https://github.com/andreaskth/gs-serving-web-content
19 cd gs-serving-web-content/
20 clear
21 ls -l
22 sudo ./mvnw spring-boot:run
23 sudo apt-get install -y nano
24 nano src/main/resources/application.properties
25 ls -l /home/ubuntu/logs
26 sudo ./mvnw spring-boot:run
27 ls -l /home/ubuntu/logs
28 cat /home/ubuntu/logs/spring.log
29 clear
30 nano /home/ubuntu/logs/spring.log
31 docker ps
32 docker logs a44
33 docker exec -it a44 sh
34 sudo ./mvnw spring-boot:run
35 clear
36 history
Beta Was this translation helpful? Give feedback.
All reactions