
11
MarchBash/Shell and Powershell Scripting
Shell Scripting
After going through the Bash and Powershell Course, here is a summary of what I was able to learn and demonstrated. For a more detailed summary of my work, follow the link here.
Linux Shells
- A shell is an environment in which we run our commands, programs, and shell scripts. It gathers input from you and executes programs based on that input. When a program finishes, it displays the program's output.
Types of shells
- Bourne shell (sh)
- Korn shell (ksh)
- Bourne again shell (bash)
- POSIX shell (sh)
- C shell (csh)
- TENEX/TOPS C shell (tcsh)
Shell Scripts
The basic concept of a shell script is a list of commands, which are listed in the order of execution.
.The shebang
#!/bin/sh
- This tells the system that the commands that follow are to be executed by the Bourne shell. It's called a
shebang
because the#
symbol is called ahash
and!
symbol is called abang
.
Windows PowerShell
Resources:
PowerShell Top Ten Commands
- Get-content: Get the content of the given item at the specified location.
Get-Content hosts.txt
- You can assign the output to variable
$hosts = Get-Content hosts.txt
- ForEach-Object: Performs operation against each item in a collection of input objects.
$hosts = Get-Content -Path hosts.txt
$hosts | ForEach-Object {
$_
}
$_
: represents each host object and will be output on the screen one at time.
$hosts = Get-Content -Path hosts.txt
$hosts | ForEach-Object {
Write-Host "Hostname: $($_)" -BackgroundColor white -Foregroundcolor black
}
- Out-File: Sends output to a file.
"My name is Patrick" | Out-File test.txt -Append
"My name is Cmd" | Out-File test.txt -Append
- Writing contents of hosts variable to a file
$hosts = Get-Content -Path hosts.txt
$hosts | ForEach-Object {
"Hostname: $($_)" | Out-File outputhosts.txt -Append
}
- Test-NetConnection: Displays diagnostic information for a connection.
$hosts = Get-Content -Path hosts.txt
$hosts | ForEach-Object {
Write-Host "Testing Host: $($_)"
Test-NetConnection -ComputerName $_ -port 443
Test-NetConnection -ComputerName $_ -port 80
Write-Host ""
}
- ConvertTo-Json: Converts an object to a JSON-formatted string.
$hosts = Get-Content -Path hosts.txt
$hosts | Convertto-json
$hosts = Get-Content -Path hosts.txt
$hosts[0] | ConvertTo-Json
- Get-Date: Gets the current date and time.
**For example logging tests to a file with time stamp
$hosts = Get-Content -Path hosts.txt
$hosts | ForEach-Object {
Write-Host "Testing Host: $($_)"
"$(Get-Date): Testing Host: $($_)" | Out-File host_test.log -Append
Write-Host ""
}
- Start-Sleep: Suspends the activity in a script or session for the specified period of time.
$hosts = Get-Content -Path hosts.txt
$hosts | ForEach-Object {
Write-Host "Sleeping for 2 seconds..." -BackgroundColor Black -Foregroundcolor Cyan
Start-Sleep -Seconds 2
Write-Host "Testing Host: $($_)"
"$(Get-Date): Testing Host: $($_)" | Out-File host_test.log -Append
Test-NetConnection -ComputerName $_ -port 443
Test-NetConnection -ComputerName $_ -port 80
Write-Host ""
}
- Write-Host: Writes customized output to a host.
$hosts = Get-Content -Path hosts.txt
$hosts | ForEach-Object {
Write-Host "Sleeping for 2 seconds..." -BackgroundColor Black -Foregroundcolor Cyan
Start-Sleep -Seconds 2
Write-Host "Hostname: $($_)" -BackgroundColor white -Foregroundcolor black
Write-Host ""
}
- Get-Command: Gets all commands.
Get-Command
- Shows all the powershell commands with the word
write
in it.
Get-Command **write**
- Get-Help: Displays information about PowerShell commands and concepts.
- Get help on the
Get-Content
command online
Get-Help Get-Content -online
Reviews