This document appears to be a slide deck for a presentation titled "Practical PowerShell Programming for Professional People" given by Ben Ten at BSidesDFW 2014. The slide deck covers an introduction to PowerShell including languages and development, PowerShell scripting, modules, Active Directory, resources and Q&A. It provides examples of PowerShell scripts and functions and discusses variables, parameters, conditional logic and other PowerShell concepts.
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Practical PowerShell Programming for Professionals
1. Practical PowerShell Programming
for
Professional People
Ben Ten
(@Ben0xA)
Slides: http://www.slideshare.net/BenTen0xA
BSidesDFW 2014
2. Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
About Me
Ben Ten (0xA)
@Ben0xA - twitter
Chicago - #burbsec
Security Consultant
Developer
PoshSec Framework Developer / Creator
Gamer
Geek
7. Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
Overview
●Languages and Development
●PowerShell Scripting
●PowerShell Modules
●ActiveDirectory
●Resources
●Q&A
}
} 2nd Hour
1st Hour
8. Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
Overview
Feel free to interrupt and ask questions!
9. Languages and Development
Before we begin, a bit of a primer!
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
●Styles of Coding
●Syntax
●Getting Help
●Starting Out
10. Languages and Development
Styles of Coding/Scripting/Development
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
●Novice
●Avid Scripter
●Full Time Developer
●Code Monkey
11. Languages and Development
Styles of Coding/Scripting/Development
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
●Novice
●Avid Scripter
●Full Time Developer
●Code Monkey
12. Languages and Development
Syntax
syn•tax (sĭnˈtăksˌ) – the rules that govern
how a script, or program, is developed in a
given language.
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
13. Languages and Development
Syntax
White Space, parens (), commas, periods,
quotes (“ vs '), tabs, braces [], curly
brackets {}, colons :, semi-colons ;, all play
an integral part in the syntax of a
language!
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
14. Languages and Development
Getting Help!
RTF Manual/Docs/Reference
Often times, the documentation will have
an answer for what you are trying to
accomplish. *NOT ALWAYS THOUGH*
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
15. Languages and Development
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
Getting Help!
Interactive Help
● ?
●F1
●Intellisense (Ctrl+Space)
●Get-Help
16. Languages and Development
Getting Help!
Search Engines FTW!
Google is not the end all in searches. For
Development I prefer DuckDuckGo!
https://duckduckgo.com
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
17. Languages and Development
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
18. PowerShell
Overview
PowerShell is a task automation and
configuration management framework
from Microsoft, consisting of a command-line
shell and associated scripting
language built on the .NET Framework.
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
19. PowerShell
Overview
PowerShell was designed by :
● Jeffrey Snover (@jsnover)
●Bruce Payette (@BrucePayette)
● James Truher
Initial release was November 14, 2006
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
20. PowerShell
Overview
PowerShell is a part of the Windows
Management Framework. WMF 5.0 was
released on April 3, 2014.
For today's scripting we will be using WMF
3.0.
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
21. PowerShell
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
You will need:
●Windows Management Framework 3.0
●Microsoft .NET Framework 4.5
●Text Editor (your choice)
●Sublime Text http://www.sublimetext.com/
●Komodo Edit http://komodoide.com/komodo-edit/
●PowerShell ISE (comes with WMF)
22. PowerShell
File Name Extensions
.ps1 – Script Files
.psm1 – Script Module Files
.psd1 – Script Manifest Files
.ps1xml – Formatting and Type Files
.dll - Cmdlet and Provider Assemblies
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
23. PowerShell
File Name Extensions
.ps1 – Script Files
.psm1 – Script Module Files
.psd1 – Script Manifest Files
.ps1xml – Formatting and Type Files
.dll - Cmdlet and Provider Assemblies
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
24. PowerShell
Cmdlets, Functions, and Scripts Oh My!
From a functional standpoint, cmdlets,
functions, and scripts are practically the
same.
They are a way to call a specific block of
code.
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
25. PowerShell
Cmdlet:
Written in a compiled .NET language.
Easier to deploy.
Help files are easier to write.
Has support for parameter validation.
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
26. PowerShell
Function:
Written in a PowerShell language.
Has to be deployed with a library.
Help is written inside the function.
Parameter validation has to be done in the
function itself.
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
27. PowerShell
Script:
Written in a PowerShell language.
Is invoked by calling the .ps1 file.
Deployed by itself or in a manifest file.
Can contain functions.
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
28. PowerShell
Set-ExecutionPolicy
Before you can run your custom scripts
you have to set the ExecutionPolicy to
RemoteSigned.
In PowerShell type:
Set-ExecutionPolicy RemoteSigned
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
30. PowerShell
HelloWorld.ps1
Enough of the primer! Let's get coding!
This is where you code along with me if
you can!
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
34. HelloWorld.ps1
Variable(s):
a symbolic name associated with a value
and whose associated value may be
changed.
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
35. HelloWorld.ps1
Hard-Coded:
Typing the value directly into your script.
Our “Hello World” text was hard-coded.
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
36. HelloWorld.ps1
PowerShell Variables:
A PowerShell variable is defined with the
dollar sign $ followed by the name of the
variable.
For example: $message is a variable.
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
37. HelloWorld.ps1
PowerShell Variables:
Let's rewrite our HelloWorld.ps1 to use a
variable $message with our text “Hello
World”.
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
42. HelloWorld.ps1
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
Strong vs Weak Typing:
$a = 1 weak type
[int]$a = 1 strong type
[String]$a = “1”
46. HelloWorld.ps1
Quotes! Single vs Double
Double Quotes (“) will attempt to resolve
any variables before anything is printed to
the screen.
Single Quotes (') will print exactly what is
typed between the quotes.
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
51. HelloWorld.ps1
Backtick `
The backtick, or grave accent, is a special
escape character. This means that you
want the next character to be printed and
not interpreted in anyway.
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
56. Getting Input
Write-Output is great. But how do you get
information from a user?
Read-Host
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
57. Getting Input
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
58. Getting Input
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
59. Conditional Logic
A Condition is:
a feature of a programming language
which perform a different set of
computations or actions depending on
whether a programmer-specified boolean
condition evaluates to true or false.
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
60. Conditional Logic
A Condition is:
Is the stop light is green? Keep going.
Is the stop light is red? Stop.
Is the stop light is yellow? Floor it!!!!
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
61. Conditional Logic
A Condition expressed:
● If - Beginning of the condition.
●Else - Evaluates only if preceding condition(s)
is(are) false.
●ElseIf – Evaluates if preceding condition(s)
is(are) false with a new condition.
●Switch – Multiple conditions for a single
variable or object.
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
62. Conditional Logic
A Conditional Operator:
-and = both conditions must be true.
-or = only one of the conditions must be
true.
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
63. Conditional Logic
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
A Conditional Operator:
-eq = Equals
-lt = Less Than
-gt = Greater Than
-ne = Not Equal
-ge = Great Than or Equal
-le = Less Than or Equal
64. Conditional Logic
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
A Conditional Operator:
-Like
-NotLike
-Match
-NotMatch
-Contains
-NotContains
-In
-NotIn
-Replace
67. Conditional Logic
Operator Precedence:
When operators have equal precedence,
Windows PowerShell evaluates them from
left to right. The exceptions are the
assignment operators, the cast operators,
and the negation operators (!, -not, -bnot),
which are evaluated from right to left.
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
68. Conditional Logic
Operator Precedence:
You can use enclosures, such as
parentheses, to override the standard
precedence order and force Windows
PowerShell to evaluate the enclosed part
of an expression before an unenclosed
part.
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
73. Parameters
A Parameter is:
A variable that allows you to pass an
object to a Cmdlet, Function, or Script.
Get-ChildItem
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
78. Objects vs Text
PowerShell is Object Based.
Even if you see text on the screen, that
text is actually a “String” object.
You can access the members of the object
using the . operator after the variable
name.
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
79. Objects vs Text
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
80. Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
Piping
Piping is:
a way of moving something, unchanged,
from one place to another.
81. Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
Piping
Piping is represented by the | (pipe)
character.
A pipe takes the object from the left side
and passes it to the right side.
Note: When passing to another cmdlet, $_
is used to reference the passed object.
82. Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
Piping
83. Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
Loops
Loops:
A way to perform the same block of code
for a specific number of times, until a
specific condition is met, or while a
specific condition exists.
84. Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
Loops
Loops:
●ForEach
●ForEach-Object
●For
●While
●Do While
●Do Until
85. Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
Loops
86. Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
Loops
87. Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
Loops
88. Comments
Comments are defined by the # symbol.
Block comments are enclosed with <# and
#>.
.SYNOPSIS
.DESCRIPTION
.PARAMETER
.EXAMPLE
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
89. Comments
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
90. Putting it all Together
The final script!
Requirements:
●Search all files.
●Find the ones that were modified in a
specific date range.
●Create a list of those files and display
them.
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
91. Short Break!
Be back in 10 minutes!
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
92. Add Parameters for Date
Use Param () block to Add Parameters.
Get-Help about_Parameters
Param(
[Parameter(Mandatory=$true)]
[Date]$FromDate,
)
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
93. Add Parameters for Date
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
94. Add Parameters for Date
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
95. Add Parameters for Date
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
96. Add Parameters for Date
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
97. PowerShell
File Name Extensions
.ps1 – Script Files
.psm1 – Script Module Files
.psd1 – Script Manifest Files
.ps1xml – Formatting and Type Files
.dll - Cmdlet and Provider Assemblies
Practical PowerShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
98. PowerShell
File Name Extensions
.ps1 – Script Files
.psm1 – Script Module Files
.psd1 – Script Manifest Files
.ps1xml – Formatting and Type Files
.dll - Cmdlet and Provider Assemblies
Practical PowerShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
103. Practical PowerShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
Module
Making Changes to Modules
●Must use -Force parameter when using
Import-Module for a module that is
already loaded into the session.
105. Practical PowerShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
Module
Note on Compiled Modules (DLLs)
●You can not import a compiled module in
an active PowerShell RunSpace after it
was already imported.
●You have to close the RunSpace and open
it again.
106. Practical PowerShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
File I/O
Get-Content <filename>
●Export-CliXML, Export-Csv, Export-
FormatData
●Out-File, Out-Csv, Out-Data
107. Practical PowerShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
File I/O
Let's create a script that will read each line
of a CSV file, and write out only the first
delimited column.
108. ActiveDirectory
ActiveDirectory PowerShell Module
●Available in the RSAT
●Comes Standard on Server (2008, 2012)
●Windows 8 Note: Must use pkgmgr to
install the .cab file.
Practical PowerShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
111. ActiveDirectory
Yes, you can do this the hard way...
Here's an example.
Practical PowerShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
112. ActiveDirectory
But why do it the hard way?
Get-Command -Module ActiveDirectory
135 Commands!
Practical PowerShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
114. ActiveDirectory
Practical PowerShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
Get-AdUser
●Query the domain controller.
●Get-Help Get-AdUser
116. ActiveDirectory
Practical PowerShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
New-ADUser
●Adds a new user to the domain.
●Disabled by default!
119. ActiveDirectory
Practical PowerShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
New-ADUser
●We can add a user with very few
parameters, but that user is not “usable”.
●Need -DisplayName -SAMAccountName
121. ActiveDirectory
Practical PowerShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
Remove-ADUser
●Uses the DN to remove the specified user.
●Remove-ADUser “CN=Ben
Ten,CN=Users,DC=dfw,DC=local”
122. Final Script
Take what you have learned and write a
PowerShell Function called Import-Users
●Imports Users from csv file Users.txt
●Must force Password Reset
Practical PowerShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
123. Error Handling
Try / Catch / Finally Blocks are used to
catch exceptions.
Try {
}
Catch [Type] {
}
Finally {
}
Practical PowerShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
124. Error Handling
ErrorVariable / ErrorAction are also used
but in a different way.
Get-Help about_CommonParameters
Do-Something -ErrorVariable $err
-ErrorAction [Continue | Ignore | Inquire |
SilentlyContinue | Stop]
Practical PowerShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
125. Homework
Go back to your Import-User function. Add
Error Handling for when:
1. The DC is not responding.
2. The line you are trying to import is not
delimited correctly.
3. The user already exists.
Practical PowerShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
126. Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
Pitfalls
Don't overuse the Pipe! Not everything has
to be done in a single line.
It's more important that you understand
the code before you try to condense it to a
single line.
127. Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
Pitfalls
With Loops, start small then open the
valve all the way!
You can get more than you wanted, or get
stuck in an endless loop.
Especially true when doing File operations!
128. Resources
Freenode (irc.freenode.net)
#PowerShell, #pssec, #poshsec channels.
Learn Windows PowerShell in a Month of
Lunches ~ Don Jones
Carlos Perez – PowerShell Workshop at
BSidesDFW.
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
129. Resources
PoshSec – https://github.com/PoshSec
PoshSec Framework
PowerSploit –
https://github.com/mattifestation/
Posh-SecMod –
https://github.com/DarkOperator/
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
132. Matt Johnson (mwjcomputing)
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
133. Follow these People!
@mwjcomputing
@securitymoey
@jaysonstreet
@BSidesDFW
@tonikjdk
@darkoperator
@mattifestation
@obscuresec
@harmj0y
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
134. Contact - Q&A
Ben Ten (0xA)
@Ben0xA - twitter
http://ben0xa.com
https://poshsec.org
web@ben0xa.com
Ben0xA – LinkedIn, Github, keybase, etc.
irc.freenode.net
#burbsec, #poshsec, #pssec
http://www.slideshare.net/BenTen0xA
QUESTIONS?!
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
135. Thank You!
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)
136. Thank You!
Practical Powe rShell Programming for Professional People
BSidesDFW - Ben Ten (@Ben0xA)