CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+
-
Upload
jasper-gardner -
Category
Documents
-
view
219 -
download
1
Transcript of CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+
![Page 1: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/1.jpg)
CN1266 Network Scripting V1.2
Kemtis KunanuraksapongMSIS with Distinction
MCTS, MCDST, MCP, A+
![Page 2: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/2.jpg)
Agenda
Chapter 4: Shelling Out Commands and ScriptsChapter 5: When Dollars Turn into VariablesChapter 6: A Bit of Logic to Save the DayFlowchart and Pseudo codeChapter 26: The Ten Most Important Cmdlets (1,7)Chapter 27: Ten Common PowerShell Mistakes (5,6,7)
![Page 3: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/3.jpg)
CMDLets
To see the list of the commandsGet-CommandGet-Command | MorePipe (|) is used to pass the output of any command to another. See Chapter 7
![Page 4: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/4.jpg)
CMDLets (Cont.)
Get-HelpGet-Help Get-AliasGet-Help Get-Alias –detailed
More information
Get-Help Get-Alias -fullMore technical information
TryGet-help get-service
![Page 5: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/5.jpg)
CMDLets (Cont.)
Try these:Get-service –name eventlog, spoolerGet-service eventlog, spooler
![Page 6: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/6.jpg)
One shell to rule them all
Command Shell – dir, md, rdWindows script host
VBScript (.vbs) or JavaScript (.js)Use CScript.exe or WScript.exe
![Page 7: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/7.jpg)
Variables
What is variable?Can contain character(s), symbol(s), and special character(s)$MyVar$A6${Var with Space}${V@r}
Camel case notation - $ThisIsAnExampleGet-Help Set-Variable
![Page 8: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/8.jpg)
Data Types
Kinds of valuesVariants – can take on any data typePrimitive values – basic data types such as
BooleanCharDateIntegerSee more on Page 63
![Page 9: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/9.jpg)
Working with data types
Try these code:$a = 2$b = 3$c =$a+$bWrite-output $c
Try put “2” in stead of 2$a = “2”$b=“3”
![Page 10: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/10.jpg)
Working with data types (2)
By adding double quotation mark, it explicitly defines the variable as string or textPlus sign becomes to concatenate the string instead of addition
![Page 11: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/11.jpg)
Why should we explicitly define
Unexpected valuesTo reduce the chance of type mismatch
ClarityImproved performance
PSH knows right away without checking the type
![Page 12: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/12.jpg)
Why should we explicitly define (2)
Try this:[int]$IntOnly = 100$Sum = 2 + $IntOnlyWrite-output $sum$IntOnly = “PowerShell Rules!”Write-output $IntOnly +” Yes, it does”
$IntOnly.GetType().Name
![Page 13: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/13.jpg)
Why should we explicitly define (3)
Table 5-1 on Page 67 shows the shortcut of data type for explicitly defined variableCasting – a process or way to convert data type
![Page 14: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/14.jpg)
Why should we explicitly define (4)
Try these:$MyString = “ Windows PowerShell “$MyDouble = 2.0$OutString = $MyString + $MyDoubleWrite-output $OutString
Try these:$OutString = $MyDouble + $MyString
![Page 15: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/15.jpg)
Why should we explicitly define (5)
Try these:$OutString = [string]$MyDouble + $MyString
OR$OutString = ($MyDouble –as [string]) + $MyString
![Page 16: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/16.jpg)
Constant and Read-Only Var.
You have to useSet-Variable [-Option {None | ReadOnly | Constant | Private | AllScope}]Remove-Variable
Try:Set-Variable PI 3.141 –option constantSet-Variable School “Remington” –option ReadonlyWrite-output “Pi’s value is ” + $PI +
“ This code is run at “ + $School
![Page 17: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/17.jpg)
Automatic Variables
What is automatic variables?Pre-assigned by PSHSee Table 5-2 on Page 72 - 73
![Page 18: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/18.jpg)
Working with Objects
Properties?Method?Try code on Page 75 and 76
![Page 19: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/19.jpg)
Star Trek Quiz Game
See HandoutChallenge: Modify the StarTrek Quiz so that it displays the correct answer for any question that the player misses
![Page 20: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/20.jpg)
A Logic Primer
Automatic variables : $true, $falseAndOrExclusive (XOR)NotTry code on page 79
![Page 21: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/21.jpg)
Order of Precedence
( )Negate (Not)AndOrXOR
![Page 22: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/22.jpg)
Logical Operator
See table 6-5 on Page 79 - 80Try the code on page80
![Page 23: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/23.jpg)
IF / Else
Remember the flow chart?$Name = “Kemtis”If ($name –eq “Kemtis”){
Write-host “Hello Kemtis!”} else {
Write-host “Hello sir”}
ORIf ($name –eq “Kemtis”){Write-host “Hello Kemtis!” }
![Page 24: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/24.jpg)
IF / ElseIf/Else
If/Elseif/Else$grade = 60If ($grade –ge 90){ Write-host “A”}Else{ If ($grade –ge 80) { Write-host “B”} Else{If ($grade -lt 80){Write-host “F”}}}
![Page 25: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/25.jpg)
IF / ElseIf/Else (3)
If/Elseif/Else#Grade V2.0$grade = 60If ($grade –ge 90){ Write-host “A”}ElseIf ($grade –ge 80) { Write-host “B”}ElseIf ($grade -lt 80){Write-host “F”}
![Page 26: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/26.jpg)
Switch Statement
$size = “M”Switch($size){ “S” {Write-host “Small”} “M” {Write-host “Medium”} “L” {Write-host “Large”} default {Write-host “Unknown”}}
![Page 27: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/27.jpg)
Looping
For loopForEach loopWhile loopDo While loopDo Until loopInfinite loop
![Page 28: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/28.jpg)
For Loop
For ($i =1; $i –le 5; $i++){ Write-Host $i}
![Page 29: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/29.jpg)
ForEach Loop
Foreach ($i in Get-Alias){ Write-Host $i.name}
![Page 30: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/30.jpg)
While Loop
$objRandom = New-Object Random$val = $objRandom.Next(1,10)While ($val –ne 7){ Write-host (“You got a “ + $val + “…”) $val = $objRandom.Next(1,10)}Write-host “Congrat! You got a 7”
![Page 31: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/31.jpg)
Do While Loop
$objRandom = New-Object Randomdo { $val = $objRandom.Next(1,10) Write-host (“You got a “ + $val + “…”)} While ($val –ne 7)Write-host “Congrat! You got a 7”
![Page 32: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/32.jpg)
Do Until Loop
$objRandom = New-Object Randomdo { $val = $objRandom.Next(1,10) Write-host (“You got a “ + $val + “…”)} Until ($val –eq 7)Write-host “Congrat! You got a 7”
![Page 33: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/33.jpg)
Infinite Loop
$objRandom = New-Object Random$val = $objRandom.Next(1,10)While ($val –ne 10){ Write-host (“You got a “ + $val + “…”) $val = $objRandom.Next(1,10)}Write-host “Congrat! You got a 11”
![Page 34: CN1266 Network Scripting V1.2 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+](https://reader030.fdocuments.net/reader030/viewer/2022032415/56649f0b5503460f94c1e95e/html5/thumbnails/34.jpg)
Guess My Number Game
See handoutHomework:
Make the game more intuitive by adding additional instructions and guidance. (IE. You are getting very close)Modify the game to allow the player to quit at any time, instead of just at the end of the current round of play.