Documente Academic
Documente Profesional
Documente Cultură
INTRODUCTION TO SCRIPTING
AND POWERSHELL
Win213
Agenda
Why Create Scripts?
• Purpose
• Scripting vs Compiled Programming Languages
• Essentials (basics) of Programming: (variables and control flow statements)
• Windows commands as aliases
Variables
• Purpose
• Types of Variables (Automatic, Environment, User-defined)
• Variable Naming Rules
• Un-Types vs Strongly-Typed User-Defined Variables
• Creating, Testing and Deleting Variables
They are stored in files for they can be run manually, or can be
scheduled to run automatically at a certain time on a period basis
(so the IT administrator can perform other tasks or SLEEP!)
Compiled Languages
Programmers use a set of instructions that they find easy to
understand (source code), then compile (transform) the source
code into an executable file. This executable file is in machine
language which will run fast and is understood by the computer.
9/1/2015 Variables 6
Essentials of Programming
When gaining experience over time as an IT professional you
may learn several different programming languages to help to
help automate a task or a group of tasks.
Since PowerShell supports aliases, this gives the “illusion” that you are
running Windows commands in your scripts, which are actually aliases
or “nicknames” for cmdlets.
Get-Alias
Get-Alias Windows-Command
Variables
A program would be very boring if it always did the same
predictable thing over and over, although very boring could be
useful like backing up all the VMs on your computer system.
Variables
The simple name “variable” indicates that it has the ability for change.
There is a neat type of variable (storage container) called Arrays that can
store multiple sets of data at the same time and is very useful, but we will
wait until week 6 to discuss…
9/1/2015 Variables 11
Variable Types
The data that variables contain can represent:
• Results from commands
• File or Directory Pathnames
• Usernames (account names)
• System setting information
• Etc…
• Are stored on the variable and env drives, as well as the internal
system.
NOTE: Adding folder PowerShell2 to the path means that you don’t need to
explicitly type “.\” to run executables. PowerShell now trusts the source because it
is on the “path”.
9/1/2015 Variables 15
Examples:
User-defined variable: $DisplayName , $Program
(i.e. created in script for lab2)
9/1/2015 Variables 16
• Un-Typed variables can store any type of data (e.g. numbers or strings of text), and the
type of data could change at any time when the variable is used.
• When using Un-Typed variables, the PowerShell interpreter determines (guesses) what
the data is when the variable is used in the script (e.g. text, or a number in order to
perform a mathematical calculation).
• For Example:
$var=“Murray Saul”
$num=123
9/1/2015 Variables 19
• The author of the PowerShell script wants to control what the data-type of
the variable, as opposed to the PS interpreter accepting any data to be
contained in the variable.
• For example, you may want the variable to only store numbers as opposed to
strings of text. Error checking to prevent incorrect data from being entered
from users is mandatory for creating excellent shell scripts.
• In that case, you would need to declare the data-type of the variable, either
prior-to or during the assigning a value to the variable.
9/1/2015 Variables 20
An excellent example would be to create a variable with a value, but also set it to be
read-only, so it cannot be changed when the script is running (known as a “constant” in
programming languages).
See what happens below when you try to change the value of the readonly
variable.
9/1/2015 Variables 22
Method 2:
$courseCode = “WIN213”
Get-Variable
Get-ChildItem variable:
Get-ChildItem env:
Get-ChildItem alias:
etc
9/1/2015 Variables 24
Example:
$clientAge = 20
Test-Path variable:\clientAge
Clear-Variable
Delete the data stored in a variable, but does not delete
the variable.
Remove-Variable
Deletes a variable and its value from the current
session. Note: you can’t remove a variable those option
has been previously set to “read-only”.
Examples:
$ErrorActionPreference = "Stop"
$ErrorActionPreference = "SilentlyContinue"
9/1/2015 Variables 27
Update-Help
This allows you to download from the
Internet newer versions of help that may
include revisions or corrections.
Get-Help – ShowWindow
This displays help in a graphical dialog box
instead of the console pane. To customize
what elements of help appear in the help
dialog box (eg. like parameters), you
simply click Settings.
9/1/2015 Overview 28