Windows PowerShell
Agenda
Introduction
PowerShell - Goals
Powershell – Basics
Working with Objects
Introduction - Chronology
• command.com –> MSMS--DOS (~1982)
• cmd.exe -> Windows NT (~1988)
• Windows Script Host -> Windows 98
• Windows PowerShell -> Exchange Server 2007
Introduction - command.com
• CLI UI for MS
MS--DOS
• Default MS-
MS-DOS shell
• First program run after boot
• Executes AUTOEXEC.BAT configuration file
Introduction – cmd.exe
• Analog of command.com in MS-
MS-DOS
• Paths completation
• Commands History
• Extensions for commands IF,FOR,SET
Introduction - WSH
• Automation technology for scripting
• Language indipendent
• Supports different 3rd party scripting engines
• Virus and malaware security issues
• Restriction Policies only for signed scripts
scripts..
Introduction – PowerShell (1)
• Command Line Interface + Scripting language
• Based on .NET Framework OOP
• Extensible Shell ( cmdlets definition )
• Combination of cmdlets
Introduction – PowerShell (2)
• Unix-like and C#-
Unix- C#-like syntax
• Shortcuts and aliases
• Iteration,, Conditional blocks
Iteration blocks,, Variables
• -WHATIF, -CONFIRM support
• Access to file system and System Registry
PowerShell - Goals
• Bridge the gap with Unix shells
• Give a complete tool for
for::
– System administration
– System configuration
– Automated administration and configuration
PowerShell - Basics
A bit of syntax
First CmdLets
CmdLets combination
Creating scripts
PowerShell – A bit of syntax (1)
• CmdLet syntax = <<verbverb>>-<noun
noun>>
e.g.: get-
get-help
• Parameters = – <parameter_name
parameter_name>>
e.g. : get
get--help –detailed
• Aliases : “get
“get--help
help”” or “help” or “man”
• Piping = <command1> | <command2>
e.g. get-
get-help * | get-
get-help –detailed
PowerShell – A bit of syntax (2)
• Output redirection :
e.g. get-
get-help * > c:/file.txt
• Wildcards :
* : zero or more characters - ( a* : an an,, anna
anna))
? : one character - (a? : an
an,, al, ai)
[a-
[a-z] : range of characters - (a[l
(a[l--n] : al,
al,am
am,,an)
an)
[bc
bc]] : specified characters - (a[
(a[lns
lns]] : al,an
al,an,,as)
as)
First CommandLets
• Get-Help -> the most important
Get- important!!
• Get--Help –Detailed -> the 2
Get 2°° most important
important!!
• Set--Location -> aliased by Cd
Set
• Get--ChildItem -> aliased by dir or ls
Get
• Set--Location e Get
Set Get--ChildItem are used also in
the SystemRegistry and in the units : env
env:: ,
Function:: , Alias: e Variable
Function Variable::
First CommandLets
• Get – <something
something>>
• Set – <something
something>>
->To Get or Set informations
informations,, properties
properties,, etc
• Import
Import––<something
something>>
• Export
Export––<something
something>>
-> To Import or Export objects from and into
specified file formats
CommandLets Combination
• Example :
Get-ChildItem – path
Get-
C:\\Users\
C: Users\Laptop\
Laptop\Desktop
CommandLets Combination
• Example :
Get-ChildItem – path
Get-
C:\\Users
C: Users\\Laptop
Laptop\\Desktop –recurse |
Measure--Object –Property Lenght –sum
Measure
CommandLets Combination
• Example :
(
Get-ChildItem -Path
Get-
C:\\Users\
C: Users\Laptop\
Laptop\Desktop -recurse |
Measure--Object -Property Length –sum
Measure
).sum / 1Gb
CommandLets Combination
• Example :
Get--ChildItem –Recurse
Get
CommandLets Combination
• Example :
Get-ChildItem –Recurse |
Get-
Where--Object {$_ -match ‘^b
Where ‘^b’}
’}
CommandLets Combination
• Example :
Get-ChildItem –Recurse |
Get-
Where--Object {$_ -match ‘^b
Where ‘^b’}
’} |
Measure--Object –Property Lenght
Measure
-Average
CommandLets Combination
• Example :
Get-ChildItem –Recurse |
Get-
Where--Object {$_ -match ‘^b
Where ‘^b’}
’} |
Measure--Object –Property Lenght
Measure
-Average > result.txt
CommandLets Combination
• Example :
Get--Process wi*
Get
CommandLets Combination
• Example :
Foreach ( $_ in ( Get
Get--Process wi* ) )
CommandLets Combination
• Example :
Foreach ( $_ in ( Get-
Get-Process wi* ) )
{
If ( $_.Name –match ‘^w
‘^w’’ )
}
CommandLets Combination
• Example :
Foreach ( $_ in ( Get-
Get-Process wi* ) )
{
If ( $_.Name –match ‘^w
‘^w’’ )
{ $_.Name, $_.Cpu }
}
PowerShell – Creating Scripts
• Scripts usually have “.ps1” extension
• We can:
– Create/
Create/Edit
Edit them with Notepad <- BAD!
– Create/
Create/Edit
Edit them with PowerShell ISE
PowerShell – Creating Scripts
• First of all : Threat Prevention
Prevention!!
• We have to change the Execution Policy
set-executionpolicy remotesigned
PowerShell Creating Scripts
• Digital Signature
• A script can be certified by
by::
– An authority
– Ourselves
Get-Help Set-AhutenticodeSignature
Working With Objects
.NET
Creating an Object
Using .NET Objects
Exporting on XML
.NET
• .NET is the Microsoft architecture software
development for Windows.
• Provides a multi
multi--language development
framework..
framework
• Desktop or Web or Mobile Applications
• Web Services
.NET
• PowerShell can work with .NET Objects:
Objects:
– Instatiate
– Set & Get Properties
– Call Methods
Creating an Object
• $date = New
New--Object DateTime 2010,05,26
• $list = New-
New-Object
System.Collections.Generic.List[<
System.Collections.Generic.List [<Type
Type>]
>]
• $list_of_date = New
New--Object
System.Collections.Generic.List[[DateTime
System.Collections.Generic.List DateTime]]
Using .NET Objects
• $date = New
New--Object DateTime 2009,11,20
• $date2 = New
New--Object DateTime 2010,05,27
• $date3 = New
New--Object DateTime 2010,04,24
• $list_of_date.Add(($date
$list_of_date.Add $date))
• $list_of_date.Add($date2)
$list_of_date.Add ($date2)
• $list_of_date.Add($date3)
$list_of_date.Add ($date3)
Using .NET Objects
• I want the ordered list
list!!
!!
• $list_of_date.Sort()
$list_of_date.Sort ()
• I want it reverse!!
• $list_of_date.Reverse()
$list_of_date.Reverse ()
Exporting on XML
• myScript.ps1 | Export
Export--Clixml <FileName>
FileName>
• $variable = myScript.ps1
$variable | Export
Export--Clixml <FileName
FileName>>
• $myObject | Export
Export--Clixml <FileName>
FileName>
THAT’S IT!