PowerShell is becoming the de-facto standard in scripting for Windows admins, replacing the ancient (and ageing) VBScript. In this session, you’ll learn the three commands you need to know to start writing your own scripts. This interactive session will:
Introduce you to PowerShell 3.0
Help you write a simple script
Cover working with the Integrated Scripting Environment (ISE) to make scripting a cinch.
This will cover 3 basic commands, and introduce the viewer to the idea of the basic principles of PowerShell.
1. SCRIPTING AS A SECOND
LANGUAGE:
INTRODUCTION TO
POWERSHELL
Rob Dunn, System Administrator Lead
2. “PowerShell is a distributed automation engine
focused on simplifying complex tasks by
composing high level, task-oriented automation
abstractions with an object pipeline and rich
scripting language.”
– Jeffrey Snover
What is PowerShell?
3. Standardization
Cmdlets
Verb/Noun Get, Set, Add, etc
Objects
Object manipulation is the very foundation of how PowerShell works.
Use Get-Member to find out what an object can do
Pipeline (the ‘|’ symbol)
Take the objects created from one cmdlet and sending it to another
Get-Services
Set-Variable
Add-Content
Get-Service –Name BITS | Stop-Service
4. The three commands you must know
Get-Command
Get-Help (RTFM, right?!)
How to read help (there’s some key things you need to look for)
Get-Member
Find out the methods and properties associated with an object
*** Everything in PowerShell is an object ***
5. #SPICEWORLD2016
Use the right tool for the job!
• Not NotePad or other text-based
editors
• Something that is “PowerShell”
aware
• This could be Visual Studio CE,
VSCode, Sapien PowerShell
Studio, or the built-in Microsoft
ISE.
Advantages?
• Easily execute single lines or
sections of code
• Tab completion of parameters,
properties & methods
• Debugging, error discovery, and
syntax highlighting
• Add-on support
6. “An object can be
a variable, function, or data
structure. In the object-oriented
programming paradigm, ‘object’
refers to a particular instance of
a class where the object can be a
combination of variables,
functions, and data structures”
-Wikipedia
Object
Properties
Information about this
object
Methods
Things you can do with
this object
?!
Get-Service BITS
or
$Variable = Get-Service BITS
7. Methods
• $chair.Raise
• $chair.Roll
• $chair.Tilt
• $chair.Break
It’s a “thing” that represents something or many somethings.
Properties
• $chair.ArmWidth = 11.5 cm
• $chair.SeatWidth = 92 cm
• $chair.ArmColor = brown
• $chair.SeatColor = brown
• $chair.WheelCount = 5
$chair
$chair = Get-Chair | Where-Object ArmWidth -eq 11.5cm
$chair.Roll(20ft)
8. File Discovery Script
Get list of files that haven’t changed for 60 days
Filter files by extension (.log)
List name, path, size and creation date
This exercise will demonstrate how to search for commands, how to utilize the help system,
get-member, and the PowerShell ISE.
9. Use PS1 extension
Usually best to run from the PowerShell console, ISE, or other PowerShell-integrated
environment
Execution Policy
Get-ExecutionPolicy
Set-ExecutionPolicy RemoteSigned
10. Use Formatting when possible, when you send your scripts to others to peer-review, it
will be easier for them to read (bonus: you won’t get made fun of, well at least for how it
looks)
Don’t use aliases, it will only confuse future you and/or your co-workers
Make your utility-scripts single purpose. Don’t have a SQL DB integrity checker script
also remove databases; that is a special purpose script.
When in need of a pre-written script, check online resources like the Microsoft Script
Gallery.
#Comment your code so future you won’t curse your/their name.
11. Visit https://goo.gl/Y9RR5z to view a presention by Martin Pugh and myself at
Spiceworld on this topic
At the time, PowerShell 3.0 was current, but the content is still relevant
Skip to 11:50 to view the script writing demo
12. Remember, there are no
stupid questions. Only
stupid people who ask
questions.
- Confucius
…probably
Notas do Editor
Powershell is a tool - it can be a simple tool. What is it to us as IT professionals?