basic. ps is a command line interpreter/scripting environment designed for.net similar to c# ...
Post on 22-Dec-2015
217 views
TRANSCRIPT
![Page 1: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/1.jpg)
Introduction to Powershell
Basic
![Page 2: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/2.jpg)
Basics
PS is a command line interpreter/scripting environment
Designed for .Net Similar to C# Easy to instantiate .Net classes
Standardized syntax PS is object based – not text based
This includes pipeline processing Reduces need for reading process to parse data
V1.0 does not naturally support remote execution.
![Page 3: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/3.jpg)
Start Powershell
Start->Programs->Windows Powershell->Windows Powershell
![Page 4: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/4.jpg)
At the command line
Expressions Cmdlet PowerShell Functions PowerShell Scripts Native Windows Commands
- Also - Supports direct interpolation
![Page 5: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/5.jpg)
PS Drives
Let’s start by looking at the environment
PS Drives give you access to resources in a consistent manner – similar to Unix’s approach to files and devices
PS Drives provide access to folders/files, the registry, existing functions and variables etc
Enter the following at the command line:
![Page 6: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/6.jpg)
Get-PSDrive
![Page 7: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/7.jpg)
Experience exercise
Enter the following lines at the command prompt and observe the behavior:#Key in this line and the rest below
54
(54 * 3 ) – 2
"Patrick is " + 54
Get-Date
![Page 8: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/8.jpg)
Operators
() {} - Grouping++ , -- Increment, decrement, respectively+ , - Unary operator* or *= Multiplication. / or /= Division. % or %= Modulus+ or += Addition. - or -= Subtraction. = Assigns a value to a variable
Standard rules of precedence apply.
![Page 9: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/9.jpg)
.Net CLR Types
In .Net everything is an object.
![Page 10: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/10.jpg)
Powershell Data types
Type Description
[int] 32-bit signed integer
[long] 64-bit signed integer
[string] Fixed-length string of Unicode characters
[char] A Unicode 16-bit character
[byte] An 8-bit unsigned character
[bool] Boolean True/False value
[decimal] An 128-bit decimal value
[single] Single-precision 32-bit floating point number
[double] Double-precision 64-bit floating point number
[xml] Xml object
[array] An array of values
[hashtable] Hashtable object
![Page 11: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/11.jpg)
Exploring on your own
129 + "1""1" + 129[int]"0129" + 1[datetime]"10/20/2009""We are meeting on " + get-date"We are meeting on " + (get-date)
![Page 12: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/12.jpg)
Expressions are objects
Let’s explore this by entering the following
(120).GetType()"Patrick Bailey".GetType()"Patrick Bailey".ToUpper()“Pat has " + "Pat".Length + " letters."(Get-Date).AddYears(-5)
![Page 13: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/13.jpg)
Variables while we’re at it
$a = 123.45$a.GetType()$b = "Patrick"$b.GetType()
![Page 14: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/14.jpg)
Convenient here-string
![Page 15: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/15.jpg)
Arrays
$a = 1,2,3,4,7,"How do you do", 9,(Get-Date)$a$a[2]$a[3..6]$a[-1]
![Page 16: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/16.jpg)
Hashtables$list = @{"St Paul" = [datetime]"10/20/2009"; ` "Minneapolis" = [datetime]"11/17/2009"}$list$list["St Paul"]
Line continuation
![Page 17: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/17.jpg)
Cmdlets
Cmdlets are instances of .NET Framework classes; they are not stand-alone executables.
Cmdlets do not generally do their own parsing, error presentation, or output formatting. Parsing, error presentation, and output formatting are handled by the Windows PowerShell runtime.
Cmdlets process input objects from the pipeline rather than from streams of text, and cmdlets typically deliver objects as output to the pipeline.
Cmdlets are record-oriented because they process a single object at a time.http://msdn.microsoft.com/en-us/library/ms714395%28VS.85%29.as
px
![Page 18: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/18.jpg)
Cmdlet(A compiled application for the PS environment)
131 Cmdlets in v1.0, 237+ in V2.0 verb-noun naming (e.g. get-
childitem) Elements of a command :
command -parameter1 -parameter2 arg1 arg2Command
Name
SwitchParameter
Parameter withargument
PositionalArgument
Ref: http://technet.microsoft.com/en-us/library/dd315315.aspx
![Page 19: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/19.jpg)
Exploring datetime object
![Page 20: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/20.jpg)
Information about the system
Enter each of these and observe
Get-CommandGet-Command -Verb SetnotepadGet-ProcessGet-Process notepadGet-Service -Name *sql*
Ref: http://technet.microsoft.com/en-us/library/dd315315.aspx
![Page 21: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/21.jpg)
Some standard named params -ErrorAction
Behavior when an error occurs. Values include SilentlyContinue, Continue (defualt), Stop
-Confirm Before execution the command will ask user for
continuation option. Mostly available with commands that alter resources.
-ErrorVariable Variable to assign error object to.
-WhatIfDescribes the action that would be taken – does
not execute command
![Page 22: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/22.jpg)
Try these one at a time…
Set-Content demofile.txt "Hello`nThis is the second line." –Confirm
Get-Content demofile.txt -OutVariable txtContent
$txtContent
Remove-Item demofile.txt –WhatIf
Remove-Item demofile.txt
Remove-Item demofile.txt #yes this repeats it
Remove-Item demofile.txt -ErrorAction SilentlyContinue
![Page 23: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/23.jpg)
Filter cmdlets and the pipeline
Most cmdlets, as you have seen, produce objects for their output. Literal values do the same.
Get-Member provides a listing of the public members of those objects
By default it displays public instance members
![Page 24: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/24.jpg)
Taking it one step further
![Page 25: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/25.jpg)
The pipeline
Similar to traditional scripting languages
BUT it “pipes” objects – specifically .Net objects
Access to data is through methods and properties – not string parsing*
*Of course, any property of type string has the expected set of methods for returningmodifications of the string.
![Page 26: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/26.jpg)
Examples of Get-Member
"Pat winks" | Get-Member
Get-Process | Get-Member
[Int32] | Get-Member
[Int32] | Get-Member –Static
[Int32]::MaxValue
![Page 27: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/27.jpg)
A closer look
“It’s not rude to point”
notepad;notepad
$plist = Get-Process notepad$p1 = $plist[0]$p1 | Get-Member
$p1.HasExited
Stop-Process -Id $p1.Id
$p1.HasExited
![Page 28: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/28.jpg)
Filtering in action
notepad;notepad;notepad
Get-Process | Where-Object { $_.Name -eq "notepad" }
Get-Process | Select-Object Handles, Id, ProcessName
Get-Process -Name notepad | ForEach-Object { Stop-Process $_.Id }
![Page 29: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/29.jpg)
What was that?
Script blocks – think of them as anonymous methods or short inline scripts
$_ is similar to Perl’s use as the current value in process
![Page 30: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/30.jpg)
Group-Object
![Page 31: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/31.jpg)
Grouping Objects get-childitem C:\temp | group-object extension
![Page 32: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/32.jpg)
Service Cmdlets
get-service new-service restart-service set-service stop-service start-service
![Page 33: Basic. PS is a command line interpreter/scripting environment Designed for.Net Similar to C# Easy to instantiate.Net classes Standardized syntax](https://reader030.vdocuments.site/reader030/viewer/2022032523/56649d785503460f94a5b9e9/html5/thumbnails/33.jpg)
Starting and Stopping a Service