Uploaded by Marin V

powershell fundamentals

advertisement
PowerShell
Fundamentals
Copyright © 2016-2020 – Curt Hill
Introduction
• Previously, we considered the
similarity of PowerShell to the DOS
Command Box
• Now we examine in more detail the
notion of programming
Copyright © 2016-2020 – Curt Hill
Previous Shells
• Most shells had variables that were
simple strings
• Considerable effort was given to
converting a string from one form to
another
– Such as a numeric value
• PowerShell believes in objects
– These may have types other than simple
strings
• Everything is an object
Copyright © 2016-2020 – Curt Hill
Windows Options
• Like the DOS Box you may used the up
and down cursor keys to scroll
through previous commands
– These can be modified and re-executed
• Unlike the DOS Box this is a real GUI
Window
– Highlighting, copy and paste work as they
do in any real Windows application
– A feature long desired in DOS Boxes
Copyright © 2016-2020 – Curt Hill
Expression Evaluation
• DOS did not evaluate things on the
command line
• PowerShell does
• It recognizes items and treats them
appropriately
• It uses numeric operators in intuitive
way
– Such as +, -, * and /
• Strings may also be concatenated with
the +
Copyright © 2016-2020 – Curt Hill
Try This in DOS!
Copyright © 2016-2020 – Curt Hill
Function Notation
• Did you notice that the length item is a
property of a string object?
• Even though there was no object
declaration, the string was
constructed into a string object
• Applying the length to a number
typically gives a 1
– Because it recognizes it as a single
number
• Notice the > on an invalid expression
Copyright © 2016-2020 – Curt Hill
Comments
• The use of comments in a console are
somewhat pointless
– We can use them there as we will see
• However we desperately need these in
scripts
– Which we will get to see
• There are two comments in
PowerShell
• A leading octothorp is a single line
• Multiline start with <# and end with #>
Copyright © 2016-2020 – Curt Hill
Console Comments
Copyright © 2016-2020 – Curt Hill
Variables and Assignment
• PowerShell variables always start with
a $ followed by letters/digits
– No declaration is needed
• Assignment is just the =
– Like C but terminated by the line end
rather than a semicolon
• Variables may be used in commands
– Also in quoted strings
• Output of programs could be assigned
to a variable as well
Copyright © 2016-2020 – Curt Hill
Assignment and Use
Copyright © 2016-2020 – Curt Hill
Types
• Unlike most shell scripting languages
PowerShell has types
• It will freely convert these types
• A variable may dynamically change
types without much notice
• It also may do explicit casts
Copyright © 2016-2020 – Curt Hill
PowerShell Types
• [string] Fixed-length string of Unicode
characters
• [char] A Unicode 16-bit character
• [byte] An 8-bit unsigned character
• [int] 32-bit signed integer
• [long] 64-bit signed integer
• [bool] Boolean True/False value
• [decimal] A 128-bit decimal value
Copyright © 2016-2020 – Curt Hill
More Types
• [single] Single-precision 32-bit floating
point number
• [double] Double-precision 64-bit
floating point number
• [DateTime] Date and Time
• [xml] Xml object
• [array] An array of values
• [hashtable] Hashtable object
Copyright © 2016-2020 – Curt Hill
Casts
• PowerShell casts are usually
automatic
• An explicit cast is similar to a C style
cast
– Except it uses [ ] instead of ( )
• Example of dynamically changing the
type of a variable:
$var = [int]$var
Copyright © 2016-2020 – Curt Hill
Special Characters
•
•
•
•
•
•
•
; is a statement separator
“ is for strings
| is pipeline character
` is escape
{} encloses a script block
# starts a comment
() encloses an expression
Copyright © 2016-2020 – Curt Hill
Escape Sequences
• PowerShell uses the escape sequence
the same way C does
– Just a different escape character
• Escapes
•
•
•
•
•
•
•
`a -- Alert
`b -- Backspace
`n -- New line
`r -- Carriage return
`t -- Horizontal tab
`' -- Single quote
`" -- Double quote
Copyright © 2016-2020 – Curt Hill
Scripting
• A PowerShell script has an extension
of .PS1
– Just give the name to execute
– Cannot be executed from DOS Box
• Running such a script is not quite as
easy as that
• We have to consider security and
location
Copyright © 2016-2020 – Curt Hill
Location
• PowerShell executes scripts on the
path or location is specified
• Specifically it will not execute a script
in the current directory without
preceding the command with a .\
– Recall . means current directory and the
backslash is the directory separater
• Typically scripts are put in a location
and executed with an absolute
directory specification:
– D:\pgms\temp\scriptname
Copyright © 2016-2020 – Curt Hill
Security
• Unlike the batch system, PowerShell
takes into account security
• The correct policy must be in effect for
a script to execute
• There are four execution policies
Copyright © 2016-2020 – Curt Hill
Script execution policies
• Restricted
– No scripts may run - default
• RemoteSigned
– Locally created scripts may run
– Signed scripts may run
• AllSigned
– Script must be signed and it asks you
confirm that you trust publisher
– Disallows locals that are not signed
• Unrestricted
– You guess
Copyright © 2016-2020 – Curt Hill
How is this policy set?
• It may be set in a variety of ways
– System policy configuration
– Registry editor
– Through PowerShell itself
• We will consider using PowerShell on
next screen
Copyright © 2016-2020 – Curt Hill
Setting Policy
• You must start PowerShell as an
administrator
– Only then do you have sufficient privileges
– This is usually done with a right click
• Execute the cmdlet:
Set-ExecutionPolicy RemoteSigned
– It will ask for confirmation
• Leave PowerShell and restart
PowerShell normally
• See next two screens
Copyright © 2016-2020 – Curt Hill
Right Click on Menu
Copyright © 2016-2020 – Curt Hill
Next
• You will see what happens if the policy
is not properly set
• Then the policy will be changed
Copyright © 2016-2020 – Curt Hill
Policy Change
Copyright © 2016-2020 – Curt Hill
More pictures
• The script that was to be executed will
be shown
• It was moved onto the path
• Then its execution will be shown
Copyright © 2016-2020 – Curt Hill
The Script
Copyright © 2016-2020 – Curt Hill
Commentary
•
•
•
•
This was created by NotePad++
Filename: movetosec.ps1
Now it executes
Notice directory where it starts and
where it ends
Copyright © 2016-2020 – Curt Hill
Executing
Copyright © 2016-2020 – Curt Hill
Cmdlets
• The line:
Set-ExecutionPolicy RemoteSigned
– Is a cmdlet
• We will consider these in a subsequent
presentation
Copyright © 2016-2020 – Curt Hill
Finally for Now
• Now we know the minimal amount of
PowerShell scripting
• Next we consider some flow of control
Copyright © 2016-2020 – Curt Hill
Download