SlideShare une entreprise Scribd logo
1  sur  136
Télécharger pour lire hors ligne
Practical PowerShell Programming 
for 
Professional People 
Ben Ten 
(@Ben0xA) 
Slides: http://www.slideshare.net/BenTen0xA 
BSidesDFW 2014
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
Practical PowerShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA) 
About Me
SecurityFail 
Practical PowerShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
Practical PowerShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA) 
About Me
Practical PowerShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA) 
About Me
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
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA) 
Overview 
Feel free to interrupt and ask questions!
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
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
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
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)
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)
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)
Languages and Development 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA) 
Getting Help! 
Interactive Help 
● ? 
●F1 
●Intellisense (Ctrl+Space) 
●Get-Help
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)
Languages and Development 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
PowerShell 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
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)
HelloWorld.ps1 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
HelloWorld.ps1 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
HelloWorld.ps1 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
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)
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)
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)
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)
HelloWorld.ps1 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
HelloWorld.ps1 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
HelloWorld.ps1 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
HelloWorld.ps1 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
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”
HelloWorld.ps1 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
HelloWorld.ps1 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
HelloWorld.ps1 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
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)
HelloWorld.ps1 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
HelloWorld.ps1 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
HelloWorld.ps1 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
HelloWorld.ps1 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
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)
HelloWorld.ps1 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
HelloWorld.ps1 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
HelloWorld.ps1 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
HelloWorld.ps1 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
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)
Getting Input 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
Getting Input 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
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)
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)
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)
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)
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
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
Conditional Logic 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
Conditional Logic 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
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)
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)
Conditional Logic 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
Conditional Logic 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
Conditional Logic 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
Conditional Logic 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
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)
Parameters 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
Parameters 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA) 
Get-Help Get-ChildItem 
Get-ChildItem [[-Path] <String[]>] [[-Filter] <String>] [-Exclude <String[]> 
[-Name] [-Recurse] [-UseTransaction [<SwitchParameter>]] 
[<CommonParameters> 
Get-ChildItem [[-Filter] <String>] [-Exclude <String[]>] [-Force] [-Include 
-LiteralPath <String[]> [-UseTransaction [<SwitchParameter>]] 
[<CommonParame 
Get-ChildItem [-Attributes <FileAttributes]>] [-Directory] [-File] [-Force] 
[-UseTransaction] [<CommonParameters>]
Parameters 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
Parameters 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
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)
Objects vs Text 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
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.
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.
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA) 
Piping
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.
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA) 
Loops 
Loops: 
●ForEach 
●ForEach-Object 
●For 
●While 
●Do While 
●Do Until
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA) 
Loops
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA) 
Loops
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA) 
Loops
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)
Comments 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
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)
Short Break! 
Be back in 10 minutes! 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
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)
Add Parameters for Date 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
Add Parameters for Date 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
Add Parameters for Date 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
Add Parameters for Date 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
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)
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)
Practical PowerShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA) 
Module
Practical PowerShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA) 
Module
Practical PowerShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA) 
Module
Practical PowerShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA) 
Module
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.
Import-Module -Force 
Practical PowerShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
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.
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
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.
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)
ActiveDirectory 
Practical PowerShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
ActiveDirectory 
Practical PowerShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
ActiveDirectory 
Yes, you can do this the hard way... 
Here's an example. 
Practical PowerShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
ActiveDirectory 
But why do it the hard way? 
Get-Command -Module ActiveDirectory 
135 Commands! 
Practical PowerShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
ActiveDirectory 
Practical PowerShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
ActiveDirectory 
Practical PowerShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA) 
Get-AdUser 
●Query the domain controller. 
●Get-Help Get-AdUser
ActiveDirectory 
Practical PowerShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
ActiveDirectory 
Practical PowerShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA) 
New-ADUser 
●Adds a new user to the domain. 
●Disabled by default!
ActiveDirectory 
Practical PowerShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
ActiveDirectory 
Practical PowerShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
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
ActiveDirectory 
Practical PowerShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
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”
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)
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)
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)
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)
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.
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!
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)
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)
Resources 
http://www.slideshare.net/BenTen0xA/ 
practical-powershell-programming-for-professional- 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA) 
people
Resources 
http://www.slideshare.net/BenTen0xA/ 
practical-powershell-programming-for-professional- 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA) 
people
Matt Johnson (mwjcomputing) 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
Follow these People! 
@mwjcomputing 
@securitymoey 
@jaysonstreet 
@BSidesDFW 
@tonikjdk 
@darkoperator 
@mattifestation 
@obscuresec 
@harmj0y 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
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)
Thank You! 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)
Thank You! 
Practical Powe rShell Programming for Professional People 
BSidesDFW - Ben Ten (@Ben0xA)

Contenu connexe

Tendances

WordPress for the modern PHP developer
WordPress for the modern PHP developerWordPress for the modern PHP developer
WordPress for the modern PHP developerChris Sherry
 
Zend expressive workshop
Zend expressive workshopZend expressive workshop
Zend expressive workshopAdam Culp
 
Composer - The missing package manager for PHP
Composer - The missing package manager for PHPComposer - The missing package manager for PHP
Composer - The missing package manager for PHPTareq Hasan
 
DBI for Parrot and Perl 6 Lightning Talk 2007
DBI for Parrot and Perl 6 Lightning Talk 2007DBI for Parrot and Perl 6 Lightning Talk 2007
DBI for Parrot and Perl 6 Lightning Talk 2007Tim Bunce
 
Zend con 2016 bdd with behat for beginners
Zend con 2016   bdd with behat for beginnersZend con 2016   bdd with behat for beginners
Zend con 2016 bdd with behat for beginnersAdam Englander
 
Theory and practice – migrating your legacy code into our modern test drive...
Theory and practice – migrating your  legacy code into our modern test  drive...Theory and practice – migrating your  legacy code into our modern test  drive...
Theory and practice – migrating your legacy code into our modern test drive...Lars Jankowfsky
 
TypeScript와 Flow: 
자바스크립트 개발에 정적 타이핑 도입하기
TypeScript와 Flow: 
자바스크립트 개발에 정적 타이핑 도입하기TypeScript와 Flow: 
자바스크립트 개발에 정적 타이핑 도입하기
TypeScript와 Flow: 
자바스크립트 개발에 정적 타이핑 도입하기Heejong Ahn
 
Ancient To Modern: Upgrading nearly a decade of Plone in public radio
Ancient To Modern: Upgrading nearly a decade of Plone in public radioAncient To Modern: Upgrading nearly a decade of Plone in public radio
Ancient To Modern: Upgrading nearly a decade of Plone in public radioCristopher Ewing
 
mod_php vs FastCGI vs FPM vs CLI
mod_php vs FastCGI vs FPM vs CLImod_php vs FastCGI vs FPM vs CLI
mod_php vs FastCGI vs FPM vs CLIJacques Woodcock
 
Alfanous Quran Search Engine API
Alfanous Quran Search Engine APIAlfanous Quran Search Engine API
Alfanous Quran Search Engine APIAssem CHELLI
 
Let's creating your own PHP (tejimaya version)
Let's creating your own PHP (tejimaya version)Let's creating your own PHP (tejimaya version)
Let's creating your own PHP (tejimaya version)Kousuke Ebihara
 
RESTFul API Design and Documentation - an Introduction
RESTFul API Design and Documentation - an IntroductionRESTFul API Design and Documentation - an Introduction
RESTFul API Design and Documentation - an IntroductionMiredot
 

Tendances (13)

WordPress for the modern PHP developer
WordPress for the modern PHP developerWordPress for the modern PHP developer
WordPress for the modern PHP developer
 
Zend expressive workshop
Zend expressive workshopZend expressive workshop
Zend expressive workshop
 
Composer - The missing package manager for PHP
Composer - The missing package manager for PHPComposer - The missing package manager for PHP
Composer - The missing package manager for PHP
 
DBI for Parrot and Perl 6 Lightning Talk 2007
DBI for Parrot and Perl 6 Lightning Talk 2007DBI for Parrot and Perl 6 Lightning Talk 2007
DBI for Parrot and Perl 6 Lightning Talk 2007
 
Zend con 2016 bdd with behat for beginners
Zend con 2016   bdd with behat for beginnersZend con 2016   bdd with behat for beginners
Zend con 2016 bdd with behat for beginners
 
Theory and practice – migrating your legacy code into our modern test drive...
Theory and practice – migrating your  legacy code into our modern test  drive...Theory and practice – migrating your  legacy code into our modern test  drive...
Theory and practice – migrating your legacy code into our modern test drive...
 
Automate Yo' Self
Automate Yo' SelfAutomate Yo' Self
Automate Yo' Self
 
TypeScript와 Flow: 
자바스크립트 개발에 정적 타이핑 도입하기
TypeScript와 Flow: 
자바스크립트 개발에 정적 타이핑 도입하기TypeScript와 Flow: 
자바스크립트 개발에 정적 타이핑 도입하기
TypeScript와 Flow: 
자바스크립트 개발에 정적 타이핑 도입하기
 
Ancient To Modern: Upgrading nearly a decade of Plone in public radio
Ancient To Modern: Upgrading nearly a decade of Plone in public radioAncient To Modern: Upgrading nearly a decade of Plone in public radio
Ancient To Modern: Upgrading nearly a decade of Plone in public radio
 
mod_php vs FastCGI vs FPM vs CLI
mod_php vs FastCGI vs FPM vs CLImod_php vs FastCGI vs FPM vs CLI
mod_php vs FastCGI vs FPM vs CLI
 
Alfanous Quran Search Engine API
Alfanous Quran Search Engine APIAlfanous Quran Search Engine API
Alfanous Quran Search Engine API
 
Let's creating your own PHP (tejimaya version)
Let's creating your own PHP (tejimaya version)Let's creating your own PHP (tejimaya version)
Let's creating your own PHP (tejimaya version)
 
RESTFul API Design and Documentation - an Introduction
RESTFul API Design and Documentation - an IntroductionRESTFul API Design and Documentation - an Introduction
RESTFul API Design and Documentation - an Introduction
 

En vedette

Powershell Seminar @ ITWorx CuttingEdge Club
Powershell Seminar @ ITWorx CuttingEdge ClubPowershell Seminar @ ITWorx CuttingEdge Club
Powershell Seminar @ ITWorx CuttingEdge ClubEssam Salah
 
Office 365 & PowerShell - A match made in heaven
Office 365 & PowerShell - A match made in heavenOffice 365 & PowerShell - A match made in heaven
Office 365 & PowerShell - A match made in heavenSébastien Levert
 
Windows Server 2008 (PowerShell Scripting Uygulamaları)
Windows Server 2008 (PowerShell Scripting Uygulamaları)Windows Server 2008 (PowerShell Scripting Uygulamaları)
Windows Server 2008 (PowerShell Scripting Uygulamaları)ÇözümPARK
 
PowerShell Plus v4.7 Overview
PowerShell Plus v4.7 OverviewPowerShell Plus v4.7 Overview
PowerShell Plus v4.7 OverviewRichard Giles
 
Power on, Powershell
Power on, PowershellPower on, Powershell
Power on, PowershellRoo7break
 
Better, Faster, Stronger! Boost Your Team-Based SharePoint Development Using ...
Better, Faster, Stronger! Boost Your Team-Based SharePoint Development Using ...Better, Faster, Stronger! Boost Your Team-Based SharePoint Development Using ...
Better, Faster, Stronger! Boost Your Team-Based SharePoint Development Using ...Richard Calderon
 
PowerShell from *nix user perspective
PowerShell from *nix user perspectivePowerShell from *nix user perspective
PowerShell from *nix user perspectiveJuraj Michálek
 
Managing Virtual Infrastructures With PowerShell
Managing Virtual Infrastructures With PowerShellManaging Virtual Infrastructures With PowerShell
Managing Virtual Infrastructures With PowerShellguesta849bc8b
 
PowerShell 101
PowerShell 101PowerShell 101
PowerShell 101Thomas Lee
 
Incorporating PowerShell into your Arsenal with PS>Attack
Incorporating PowerShell into your Arsenal with PS>AttackIncorporating PowerShell into your Arsenal with PS>Attack
Incorporating PowerShell into your Arsenal with PS>Attackjaredhaight
 
Getting Started With PowerShell Scripting
Getting Started With PowerShell ScriptingGetting Started With PowerShell Scripting
Getting Started With PowerShell ScriptingRavikanth Chaganti
 
Windows - Having Its Ass Kicked by Puppet and PowerShell Since 2012
Windows - Having Its Ass Kicked by Puppet and PowerShell Since 2012Windows - Having Its Ass Kicked by Puppet and PowerShell Since 2012
Windows - Having Its Ass Kicked by Puppet and PowerShell Since 2012Puppet
 
Introduction to PowerShell
Introduction to PowerShellIntroduction to PowerShell
Introduction to PowerShellSalaudeen Rajack
 
Geek Sync | Using PowerShell with Python and SQL Server
Geek Sync | Using PowerShell with Python and SQL ServerGeek Sync | Using PowerShell with Python and SQL Server
Geek Sync | Using PowerShell with Python and SQL ServerIDERA Software
 
Network Mapping with PowerShell
Network Mapping with PowerShellNetwork Mapping with PowerShell
Network Mapping with PowerShellCostin-Alin Neacsu
 
Workshop: PowerShell for Penetration Testers
Workshop: PowerShell for Penetration TestersWorkshop: PowerShell for Penetration Testers
Workshop: PowerShell for Penetration TestersNikhil Mittal
 
PowerShell 101 - What is it and Why should YOU Care!
PowerShell 101 - What is it and Why should YOU Care!PowerShell 101 - What is it and Why should YOU Care!
PowerShell 101 - What is it and Why should YOU Care!Thomas Lee
 
44CON London 2015 - Old Dog, New Tricks: Forensics With PowerShell
44CON London 2015 - Old Dog, New Tricks: Forensics With PowerShell44CON London 2015 - Old Dog, New Tricks: Forensics With PowerShell
44CON London 2015 - Old Dog, New Tricks: Forensics With PowerShell44CON
 
Some PowerShell Goodies
Some PowerShell GoodiesSome PowerShell Goodies
Some PowerShell GoodiesCybereason
 

En vedette (20)

Powershell Seminar @ ITWorx CuttingEdge Club
Powershell Seminar @ ITWorx CuttingEdge ClubPowershell Seminar @ ITWorx CuttingEdge Club
Powershell Seminar @ ITWorx CuttingEdge Club
 
Office 365 & PowerShell - A match made in heaven
Office 365 & PowerShell - A match made in heavenOffice 365 & PowerShell - A match made in heaven
Office 365 & PowerShell - A match made in heaven
 
Windows Server 2008 (PowerShell Scripting Uygulamaları)
Windows Server 2008 (PowerShell Scripting Uygulamaları)Windows Server 2008 (PowerShell Scripting Uygulamaları)
Windows Server 2008 (PowerShell Scripting Uygulamaları)
 
PowerShell Plus v4.7 Overview
PowerShell Plus v4.7 OverviewPowerShell Plus v4.7 Overview
PowerShell Plus v4.7 Overview
 
Power on, Powershell
Power on, PowershellPower on, Powershell
Power on, Powershell
 
Better, Faster, Stronger! Boost Your Team-Based SharePoint Development Using ...
Better, Faster, Stronger! Boost Your Team-Based SharePoint Development Using ...Better, Faster, Stronger! Boost Your Team-Based SharePoint Development Using ...
Better, Faster, Stronger! Boost Your Team-Based SharePoint Development Using ...
 
PowerShell from *nix user perspective
PowerShell from *nix user perspectivePowerShell from *nix user perspective
PowerShell from *nix user perspective
 
Managing Virtual Infrastructures With PowerShell
Managing Virtual Infrastructures With PowerShellManaging Virtual Infrastructures With PowerShell
Managing Virtual Infrastructures With PowerShell
 
PowerShell UIAtomation
PowerShell UIAtomationPowerShell UIAtomation
PowerShell UIAtomation
 
PowerShell 101
PowerShell 101PowerShell 101
PowerShell 101
 
Incorporating PowerShell into your Arsenal with PS>Attack
Incorporating PowerShell into your Arsenal with PS>AttackIncorporating PowerShell into your Arsenal with PS>Attack
Incorporating PowerShell into your Arsenal with PS>Attack
 
Getting Started With PowerShell Scripting
Getting Started With PowerShell ScriptingGetting Started With PowerShell Scripting
Getting Started With PowerShell Scripting
 
Windows - Having Its Ass Kicked by Puppet and PowerShell Since 2012
Windows - Having Its Ass Kicked by Puppet and PowerShell Since 2012Windows - Having Its Ass Kicked by Puppet and PowerShell Since 2012
Windows - Having Its Ass Kicked by Puppet and PowerShell Since 2012
 
Introduction to PowerShell
Introduction to PowerShellIntroduction to PowerShell
Introduction to PowerShell
 
Geek Sync | Using PowerShell with Python and SQL Server
Geek Sync | Using PowerShell with Python and SQL ServerGeek Sync | Using PowerShell with Python and SQL Server
Geek Sync | Using PowerShell with Python and SQL Server
 
Network Mapping with PowerShell
Network Mapping with PowerShellNetwork Mapping with PowerShell
Network Mapping with PowerShell
 
Workshop: PowerShell for Penetration Testers
Workshop: PowerShell for Penetration TestersWorkshop: PowerShell for Penetration Testers
Workshop: PowerShell for Penetration Testers
 
PowerShell 101 - What is it and Why should YOU Care!
PowerShell 101 - What is it and Why should YOU Care!PowerShell 101 - What is it and Why should YOU Care!
PowerShell 101 - What is it and Why should YOU Care!
 
44CON London 2015 - Old Dog, New Tricks: Forensics With PowerShell
44CON London 2015 - Old Dog, New Tricks: Forensics With PowerShell44CON London 2015 - Old Dog, New Tricks: Forensics With PowerShell
44CON London 2015 - Old Dog, New Tricks: Forensics With PowerShell
 
Some PowerShell Goodies
Some PowerShell GoodiesSome PowerShell Goodies
Some PowerShell Goodies
 

Similaire à Practical PowerShell Programming for Professionals

Возможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OSВозможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OSCisco Russia
 
Python programming msc(cs)
Python programming msc(cs)Python programming msc(cs)
Python programming msc(cs)KALAISELVI P
 
Visual Studio .NET2010
Visual Studio .NET2010Visual Studio .NET2010
Visual Studio .NET2010Satish Verma
 
Introducing Language-Oriented Business Applications - Markus Voelter
Introducing Language-Oriented Business Applications - Markus VoelterIntroducing Language-Oriented Business Applications - Markus Voelter
Introducing Language-Oriented Business Applications - Markus VoelterJAXLondon2014
 
resume-jbarr-linkedin-2016
resume-jbarr-linkedin-2016resume-jbarr-linkedin-2016
resume-jbarr-linkedin-2016Jason Barr
 
Holy PowerShell, BATman! - dogfood edition
Holy PowerShell, BATman! - dogfood editionHoly PowerShell, BATman! - dogfood edition
Holy PowerShell, BATman! - dogfood editionDave Diehl
 
Entrepreneur’s guide to programming
Entrepreneur’s guide to programmingEntrepreneur’s guide to programming
Entrepreneur’s guide to programmingChris Callahan
 
[WebCamp2014] Towards functional web
[WebCamp2014] Towards functional web[WebCamp2014] Towards functional web
[WebCamp2014] Towards functional webBlaž Repas
 
Language Server Protocol - Why the Hype?
Language Server Protocol - Why the Hype?Language Server Protocol - Why the Hype?
Language Server Protocol - Why the Hype?mikaelbarbero
 
PowerShell Zero To Hero Workshop!
PowerShell Zero To Hero Workshop!PowerShell Zero To Hero Workshop!
PowerShell Zero To Hero Workshop!Daisy Stevens
 
Top 100 PHP Questions and Answers
Top 100 PHP Questions and AnswersTop 100 PHP Questions and Answers
Top 100 PHP Questions and Answersiimjobs and hirist
 
Elasticsearch Basics
Elasticsearch BasicsElasticsearch Basics
Elasticsearch BasicsShifa Khan
 
Let's contribute, HTML5Rocks/ko!
Let's contribute, HTML5Rocks/ko!Let's contribute, HTML5Rocks/ko!
Let's contribute, HTML5Rocks/ko!Chang W. Doh
 

Similaire à Practical PowerShell Programming for Professionals (20)

Возможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OSВозможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OS
 
Python programming msc(cs)
Python programming msc(cs)Python programming msc(cs)
Python programming msc(cs)
 
Visual Studio .NET2010
Visual Studio .NET2010Visual Studio .NET2010
Visual Studio .NET2010
 
Pyhton-1a-Basics.pdf
Pyhton-1a-Basics.pdfPyhton-1a-Basics.pdf
Pyhton-1a-Basics.pdf
 
Introducing Language-Oriented Business Applications - Markus Voelter
Introducing Language-Oriented Business Applications - Markus VoelterIntroducing Language-Oriented Business Applications - Markus Voelter
Introducing Language-Oriented Business Applications - Markus Voelter
 
Govind.ppt.pptx
Govind.ppt.pptxGovind.ppt.pptx
Govind.ppt.pptx
 
Vbox7 presentation 2019
Vbox7 presentation 2019Vbox7 presentation 2019
Vbox7 presentation 2019
 
resume-jbarr-linkedin-2016
resume-jbarr-linkedin-2016resume-jbarr-linkedin-2016
resume-jbarr-linkedin-2016
 
Prersentation
PrersentationPrersentation
Prersentation
 
Holy PowerShell, BATman! - dogfood edition
Holy PowerShell, BATman! - dogfood editionHoly PowerShell, BATman! - dogfood edition
Holy PowerShell, BATman! - dogfood edition
 
Entrepreneur’s guide to programming
Entrepreneur’s guide to programmingEntrepreneur’s guide to programming
Entrepreneur’s guide to programming
 
[WebCamp2014] Towards functional web
[WebCamp2014] Towards functional web[WebCamp2014] Towards functional web
[WebCamp2014] Towards functional web
 
vb script
vb scriptvb script
vb script
 
Language Server Protocol - Why the Hype?
Language Server Protocol - Why the Hype?Language Server Protocol - Why the Hype?
Language Server Protocol - Why the Hype?
 
PowerShell Zero To Hero Workshop!
PowerShell Zero To Hero Workshop!PowerShell Zero To Hero Workshop!
PowerShell Zero To Hero Workshop!
 
PHP - Introduction to PHP Fundamentals
PHP -  Introduction to PHP FundamentalsPHP -  Introduction to PHP Fundamentals
PHP - Introduction to PHP Fundamentals
 
Top 100 PHP Questions and Answers
Top 100 PHP Questions and AnswersTop 100 PHP Questions and Answers
Top 100 PHP Questions and Answers
 
Elasticsearch Basics
Elasticsearch BasicsElasticsearch Basics
Elasticsearch Basics
 
Let's contribute, HTML5Rocks/ko!
Let's contribute, HTML5Rocks/ko!Let's contribute, HTML5Rocks/ko!
Let's contribute, HTML5Rocks/ko!
 
Introduction to-php
Introduction to-phpIntroduction to-php
Introduction to-php
 

Dernier

Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 

Dernier (20)

Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
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
  • 3. Practical PowerShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA) About Me
  • 4. SecurityFail Practical PowerShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 5. Practical PowerShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA) About Me
  • 6. Practical PowerShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA) About Me
  • 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)
  • 29. PowerShell 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)
  • 31. HelloWorld.ps1 Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 32. HelloWorld.ps1 Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 33. HelloWorld.ps1 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)
  • 38. HelloWorld.ps1 Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 39. HelloWorld.ps1 Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 40. HelloWorld.ps1 Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 41. HelloWorld.ps1 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”
  • 43. HelloWorld.ps1 Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 44. HelloWorld.ps1 Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 45. HelloWorld.ps1 Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 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)
  • 47. HelloWorld.ps1 Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 48. HelloWorld.ps1 Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 49. HelloWorld.ps1 Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 50. HelloWorld.ps1 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)
  • 52. HelloWorld.ps1 Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 53. HelloWorld.ps1 Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 54. HelloWorld.ps1 Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 55. HelloWorld.ps1 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
  • 65. Conditional Logic Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 66. Conditional Logic Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 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)
  • 69. Conditional Logic Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 70. Conditional Logic Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 71. Conditional Logic Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 72. Conditional Logic 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)
  • 74. Parameters Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 75. Parameters Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA) Get-Help Get-ChildItem Get-ChildItem [[-Path] <String[]>] [[-Filter] <String>] [-Exclude <String[]> [-Name] [-Recurse] [-UseTransaction [<SwitchParameter>]] [<CommonParameters> Get-ChildItem [[-Filter] <String>] [-Exclude <String[]>] [-Force] [-Include -LiteralPath <String[]> [-UseTransaction [<SwitchParameter>]] [<CommonParame Get-ChildItem [-Attributes <FileAttributes]>] [-Directory] [-File] [-Force] [-UseTransaction] [<CommonParameters>]
  • 76. Parameters Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 77. Parameters 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)
  • 99. Practical PowerShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA) Module
  • 100. Practical PowerShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA) Module
  • 101. Practical PowerShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA) Module
  • 102. Practical PowerShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA) Module
  • 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.
  • 104. Import-Module -Force Practical PowerShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 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)
  • 109. ActiveDirectory Practical PowerShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 110. ActiveDirectory 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)
  • 113. ActiveDirectory 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
  • 115. ActiveDirectory Practical PowerShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 116. ActiveDirectory Practical PowerShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA) New-ADUser ●Adds a new user to the domain. ●Disabled by default!
  • 117. ActiveDirectory Practical PowerShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 118. ActiveDirectory Practical PowerShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 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
  • 120. ActiveDirectory Practical PowerShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA)
  • 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)
  • 130. Resources http://www.slideshare.net/BenTen0xA/ practical-powershell-programming-for-professional- Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA) people
  • 131. Resources http://www.slideshare.net/BenTen0xA/ practical-powershell-programming-for-professional- Practical Powe rShell Programming for Professional People BSidesDFW - Ben Ten (@Ben0xA) people
  • 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)