Powershell Intro

In recent years it has become necessary to learn Powershell. This is for a number of reasons, but predominately because certain automations are only possible via it. More on that later.

First – how to run powershell … simply windows key + R and powershell.

Or Windows key and start typing powershell – select Windows PowerShell ISE. This provides an IDE.

Variables

$single_quoted_string = 'a '
$double_quoted_string = "a string – $x"
Write-Host $single_quote_string
Write-Host $double_quoted_string

# can enforce with [string]

## Integers
[int]$int_one = 1
$int_two = 2
$int_one + $int_two

## Arrays
[array]$arr = @(1,'str_var',5)
$arr += 'add to arr'
$array

## Hash tables
[hashtable]$htab = @{'key1' = 'value1'; 'key2' = 'value2'}
$htab

$htab.Get_Item('key1')
$htab.key1

$htab.Add('newkey',”new added variable”)

$htab.Set_Item(“key1”, “mod_val1”)
$htab.Remove('key1')

If/Then

$a = 1
if ($a -eq 1) {
Write-Host 'a equals 1'
} else {
Write-Host 'a is not equal to 1'
}

# also possible to elseif

} elseif ($a -eq 3) {

Switch

$a = 1

switch ($a) {
1 {“Value 1”}
2 {“Value 2”}
3 {“Value 3”}
default {“Value exceeds threshold.”}
}

$b = “X365”

switch -wildcard ($b) {
“Z*” {“Val Z”}
“Y*” {“Val Y”}
“X*” {“Val X”}
default {“Val outside parameters.”}
}

1 – Lessons begin

Welcome to Coding-School.com old school!

Some of the tips, techniques and code freely imparted here, I’ve searched the web previously and come back empty handed. Then set about hand crafting my own solutions – which you get completely for free! 🙂

What gives me the audacity, to think I could possible teach anyone anything Technical! Well here is my 5 line bio:

  • Over 25 years IT work experience. Working with IBM, Compaq, ICL, Dunn and Bradstreet and 2 Banks.
  • Dabbling with LDAP for around 15 years.
  • Dabbling with PHP for around 15 years.
  • Dabbling with Perl for the last 20 years.
  • Over 20 years UNIX Admin. Well you might ask, what the hell has that do to with Coding. IMHO – best Admins code all the time!! 🙂

Cheers and enjoy,

Mark

Perl Intro

Introduction to Perl

Perl is an acronym, short for Practical Extraction and Report Language. It was designed by Larry Wall as a tool for writing programs in the UNIX environment (Perl is a stable, cross platform programming language). Perl has the power and flexibility of a high-level programming language such as C. In fact many of the features of the language are borrowed from C. Like shell script languages, Perl does not require a compiler – the Perl interpreter runs your programs. This means that Perl is ideal for producing quick solutions to small programming problems and creating prototypes to test potential solutions to larger problems.

  • Perl is a stable, cross platform programming language.
  • It is used for mission critical projects in the public and private sectors.
  • Perl is Open Source software, licensed under the GNU General Public License (GPL).
  • Perl was created by Larry Wall.
  • Perl is listed in the Oxford English Dictionary


mod_perl allows the Apache web server to embed a Perl interpreter.

Perl’s DBI package makes web-database integration easy.

@INC is also printed as part of the output of
% perl -V

Example on how to read a url/web page in perl

#!/usr/bin/perl
use LWP::Simple;
my $url="http://www.gnulamp.com/perl.html";
my $webdata=get $url;
print $webdata;