2. Windows powershell- course
-
Why should we care?
What powershell is?
Securing the shell
File and folder management
Command line tips , tricks & gotchs
3. Windows powershell- course
- Cmdlets, snapins, aliases ,help
- Basic cmdlets : processes , services, & event logs
- Retriving mgmt info from Remote computers (WMI)
4. Windows powershell- course
-
using variables ,objects , & Members
Understanding pipeline
Math & comparison operators
Sorting, measuring, selecting & filtering
Working with collection of objects
5. Windows powershell- course
- Formatting subsystem
- Exporting, converting, importing & comparing
- Managing active directory(and local), users, groups
etc,.
- Windows powershell scripts
6. Windows powershell- course
-
The scripting language (with just 14 key words)
Script blocks & functions
Custom blocks in functions
Err or trapping & handling
Script debugging
Regular expressions
10. Windows powershell- intro
• Old way
C.E.C
Snap in
GUI (MMC)
Service
Product
( eg. Ex change )
exe
Bat
WMI
data
COM
VBS
Config
11. Windows powershell- intro
• Ideal way
CLI
Ps1
Script
GUI
PowerShell
Admin
Engine
APP
Product (any)
Any
future
tech
12. Windows powershell- intro
Products build on Phase 2 model:
Exchange 2007
Windows 7
System center VM manager (SCVMM)
System center data protection manager(SCDPM)
System center common object manager (SCOM)
13. Windows powershell- intro
Products built on Phase 1 model:
System center configuration manager (SCCM)
SQL server 2008
VMware EXS
Citrix
F5 Networks
14. Windows powershell- intro
Tap in existing admin products such as:
WMI
ADSI (Active directory services interface)
.NET Framework
COM (common object model)
15. Securing the shell
•
•
•
•
•
•
Scripts : the big concern
Execution policy
script signing
profile (and back doors )
Path required (to prevent Command hijacking )
File extension , double click etc
16. Securing the shell
Scripting disadvantages :
•
•
•
•
No integrity
No identity
Command hijacking (path required to run ps1)
Double click to run
19. Securing the shell
script signing :
CA
Certificate
(PKI)
Class III
Is the script signed
Script
Sign
Get-ExecutionPolicy
Set-ExecutionPolicy
Set-AuthenticodeSignature
Is the script signature intact
Do u trust this CA
20. Using shell for file and folder management
•
•
•
•
Common commands
Slight difference (-recursive, -filter )
Spaces & quotes
PSDrive
-adding and using
- differences
• Redirecting out put (| Out-file )
• Displaying text files
21. Using shell for file and folder management
PSDrive:
Get-psdrive
New-psdrive
www.codeplex/powershellcx
22. Command-lets, Snap-Ins, Aliases, and Help
•
•
•
•
•
•
Quotes ‘ and “
Escaping
Tab completion & cmd history
-path vs –literalpath
Transcripts (Start-Transcript)
Different consoles
Diff consoles
CLI
PrmilSciript
Power
shell
PowerGUI
PowerShell+
23. Command-Line Tips, Tricks, and Gotchas
Get-command (gcm):
Get-command
Get-command -verb –noun
Get-command -noun services
Get-command -verb new
24. Command-Line Tips, Tricks, and Gotchas
Cmdlets:
Verb
Get
Set
New
-
Singular noun
Eventlog
ChildItem
ExecutionPolicy
AuthenticodeSignature
Location
process
space
name
Security
'Program
Files‘
svchost
space
-Paramter /
-parameters
-Recurse
-Filter
-Path
-LiteralPath
-Newest
space
values
25. Command-lets, Snap-Ins, Aliases, and Help
Get-command (gcm):
Get-command
Get-command -verb –noun
Get-command -noun services
Get-command -verb new
26. Command-lets, Snap-Ins, Aliases, and Help
•
•
•
•
•
•
•
•
Aliases & cmdlets
Get-command
Ask for help
Reading the help
Parameter prompting
All about aliases (New-Alias, Export-Alias, import- -Alias)
Pssnapins (Get-PSSnapin –Registered, add-PSSnapin, Export-Console psc1 ,
-PSConsoleFile)
Custom console s (& profiles)
27. Basic Command-lets for Processes, Services, and
Event Logs
• Process (Get-Process,ps , Stop-Process kill)
• Services
• Eventlogs(Common Parameters, -whatif, Get-EventLog security Newest 5 | Format-List *, fl)
• Display tips
• What’s missing
• Local vs remote
28. Basic Command-lets for Processes, Services, and
Event Logs
Services:
Get-Service(gsv)
Stop-Service
Start-Service
Suspend-Service
Resume-Service
Restart-Service
Set-Service
New-Service (Get-Credentila)
29. Basic Command-lets for Processes, Services, and
Event Logs
Cmdlets
Built on .Net framework
Non-remoting
Easy
Built-in help
WMI
it’s own architure
remoting
complicated
no built-in help
30. Retrieving Management Information from
Remote Computers
•
•
•
•
•
Interdiction to WMI
Exploring WMI
Remote WMI
Alternative credentials
Filtering WMI data
31. Retrieving Management Information from
Remote Computers
What is WMI
DMTF(Desktop (or distributed) Management Task Force)
CIM (Common Information Model ) (CIM v2)
WBEM(Web Base Enterprise Model) (with IIS v5)
WMI
32. Retrieving Management Information from
Remote Computers
What is WMI
WMI
CIM
provider
provider
provider
provider
windows
Exchange
IIS
SQL
33. Retrieving Management Information from
Remote Computers
What is WMI
Namespaces (IIS,DNS) (each namespace have a class)
root/CIMv2 for core OS
Classes (CIM) (each class is an manageable object )
-disk
-CPU
-A rec DNS
-web IIS
Instance (each instance is a subset of a class object )
each object will have separate instance
eg: hdd
CPU
34. Retrieving Management Information from
Remote Computers
What is WMI
WMIExplorer : (Methods , instance, properties )
Eg:
Win32_operatingsystem
Win32_diskpartiation
win32_service
35. Retrieving Management Information from
Remote Computers
Back to powershell
Get-WmiObject(gwmi)
win32_service
Get-WmiObject win32_service -Filter "name like 'sh%'“
gwmi win32_service -Filter "name =‘SharedAccess'“
gwmi win32_operatingsystem
gwmi win32_operatingsystem | fl *
gwmi win32_operatingsystem -computername (or -comp) “itdse”,”swpark”
gwmi win32_operatingsystem -comp (type c:abc.txt)
gwmi win32_operatingsystem -comp localhost –cre(Get-Credential)
37. Retrieving Management Information from
Remote Computers
Powershell relational operators for WMI:
For all numeric and date :
=
>
>=
<
<=
Eg: =80
For strings (with an ‘ ’)
Like
Eg: like name =‘svc%’
38. Retrieving Management Information from
Remote Computers
•
•
•
•
•
Interdiction to WMI
Exploring WMI
Remote WMI
Alternative credentials
Filtering WMI data
39. Using Variables, Objects, and Members
•
•
•
•
•
•
•
•
What are variables
Creating, populating, displaying
Interdiction to objects
Simple objects
Viewing object members
Objects in variables
Removing variables
Example credential storage
42. Understanding the Pipeline
•
•
•
•
•
•
•
Why pipeline ? (it is the power in powershell)
Visualizing pipeline
Services in pipeline
Process in pipeline
Piping to file or printer
Write-output
Write-host
43. Understanding the Pipeline
The unix / linux way
Clear txt or some character file
CMD
GREP
PID Name Responding
01 xxy
02 xxz
03 xxx
config
true
true
false
KILL
44. Understanding the Pipeline
Bill gates never shamed to copy
Get-services
|
Where-object
|
Objects after filtering
|
Formit-list
|
objects
Get-services
|
|
out-default
Formatted Object as o/p
46. Understanding the Pipeline
BDL scenario
Get-wmiobjet
|
Where –object
{filetype = mp3}
|
del
Get-wmiobjet
|
Where –object
{versiontype = xx.o}
|
update
Get-wmiobjet
|
Where –object {file
= xxx}
|
copy
Eg: Get-WmiObject Win32_Directory | where-object {filetype = mp3} | del
47. Understanding the Pipeline
Write-Out (-verb out)
Get-Service | Out-File c:sun.txt
Get-Service | Out-Printer hp1005
Write-output “Hello” (Writes objects to the success pipeline)
Write-host “Hello” (Displays objects by using the host user interface)
Write-Output "Hello" | Where-Object {$_.Length -gt 100 } (will display nothing)
Write-Host "Hello" | Where-Object {$_.Length -gt 100 } (will display Hello)
48. Mathematical and Comparison Operators
•
•
•
•
•
•
•
Basic +, -, *, /,%
Comparison operators –eq, -gt, -lt, -ge , -le(for both numiric, and string and concatenate –c
before cmp operator to obtain case sensitive cmp)
Bolin operators –and, -or , -not,
Advance math
Basic comparisons
Case sensitivity
Parsing model
-command
-expression
Eg: Get-WmiObject win32_service -computername localhost,sunil | where {$_.StartAuto -eq "Auto" -and
$_.State -ne "Running"}
49. Sorting, Measuring, Selecting and Filtering
Objects in the Pipeline
•
•
•
•
•
•
Sorting
Measuring
Selecting properties
Selecting subset
Filtering
Powershell work flow
50. Sorting, Measuring, Selecting and Filtering
Objects in the Pipeline
Sorting :
Eg: get-process | Sort-Object CPU
ps | Sort CPU –Descending
Measuring
eg: get-process | Measure-Object
gsv | Measure-Object
get-process | Measure-Object -Property pm -sum -min -max –average
get-service | Measure-Object -Property displayname -line -word -Character
51. Sorting, Measuring, Selecting and Filtering
Objects in the Pipeline
Select object and where object:
Eg: get-service | Select-Object displayname, dependentservices
get-process | select-object ProcessName,Id,VM
Get-Process | Where { $_.Name -Eq "calc" } | Kill
Get-service | Where { $_.status -eq "stopped" } | Start-Service –WhatIf
Get-service | Where { $_.name -eq "browser" } | Start-Service
Get-WmiObject win32_service -comp "sunil" | where {$_.name -match
"browser"}
Get-WmiObject win32_service -comp “itdse“,”swpark” | where ,$_.name -like
"browser"}
52. Working with Collections of Objects in the
Pipeline
•
•
•
•
Object collections
Working with groups
Working with individuals
Foreach-object (%) & wmi
53. Working with Collections of Objects in the
Pipeline
Object collections & foreach-object(%)
Eg: type c:abc.txt| ForEach-Object {gwmi win32_operatingsystem -computername
$_}
type c:abc.txt| % {gwmi win32_operatingsystem -computername $_ | % {
$_.reboot() } }
54. Understanding and Using the Formatting
Subsystem
• How objects become text
• Text in the console window
• Formatting the sub system
-format-wide
-format-list
-format-table
• Custom columns in table
• Hash table
57. Exporting, Importing, Comparing, and
Converting Objects
•
•
•
•
•
Exporting to csv
Importing from csv
CLIxml import/export
Comparing collections
Converting to html