Download presentation
Presentation is loading. Please wait.
Published byCori Doyle Modified over 8 years ago
1
CN1266 Network Scripting Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+
2
Agenda Chapter 14: Using Functions to Divide and Conquer Quiz Exercise
3
First function function GetOsInfo{ ▫$OsInfo = Get-WmiObject Win32_operatingsystem ▫write-host ($osinfo.caption + " " + $osinfo.version) } Getosinfo
4
Defining parameters function getosinfo ($computername){ ▫$osinfo = get-wmiobject Win32_operatingsystem -computer $computername ▫write-host ($osinfo.caption + " " + $osinfo.version) } $computer = get-content env:computername getosinfo $computer
5
Defining more than one parameters function greeting ($firstname, $lastname){ ▫write-host ("Hello " + $firstname + " " + $lastname) } greeting "Kemtis" "K."
6
Defining more than one parameters (2) function greeting{ ($firstname, $lastname) ▫write-host ("Hello " + $firstname + " " + $lastname) } greeting "Kemtis" "K."
7
Defining more than one parameters (3) function sum([int]$a, [int]$b){ ▫write-host ($a + $b) } sum 1 2
8
Return value from function function greeting($firstname, $lastname){ ▫return ($firstname + " " + $lastname) } $greet = greeting "Kemtis" "K." $greet += ", how are you?" Write-Host $greet
9
Return value from function (2) function dirwin{ return Get-ChildItem $env:windir } dirwin
10
Return value from function (3) To return a set of values ▫function DirWinNames{ $dir = Get-ChildItem $env:windir foreach ($item in $dir){ Write-Output $item.name } ▫} ▫$contents = DirWinNames ▫foreach ($item in $contents){ Write-Host $item ▫}
11
Scope Three scopes: ▫Global scope ▫Script scope ▫Private scope ▫See figure 14-1 on Page 194 ▫Test the script on Page 194 *Define $gs1 in the console then type the rest in ISE*
12
Watching out for name overlap function scopetest{ $myvar = "Jeremy" Write-Host $myvar } $myvar = "Royski" Write-Host $myvar scopetest
13
Watching out for name overlap (2) function changename{ $name = "Bradley" } $name = "Jason" Write-Host $name changename Write-Host $name
14
Watching out for name overlap (3) function changename{ $script:name = "Bradley" } $name = "Jason" Write-Host $name changename Write-Host $name
15
Create your own Cmdlets See Listing 14-1 on Page 198 – 199 Defining attributes ▫SupportShouldProcess Enable –confirm and –whatif, which prompts the user before the script makes any changes to the system ▫DefaultParameterSet Use it to define the dafault paramter to use
16
Create your own Cmdlets (2) ▫ConfirmImpact Used to determine when the action should be confirmed by a call to the ShouldProcess method. It will run only if the value is equal to or greater than the value of the shell’s $ConfirmPreference ▫Snapin To define the name of the snap-in used to register the cmdlet ▫See Table 14-1 on parameters
17
Three methods of the function Begin{} ▫It will be run once when the function is called Process{} End{} It will be run once when the function terminates
18
Assignment Grade Checker ▫Write a script to take two inputs from user. (Name and score) ▫Write a function that will determine the grade letter base on the score that user type ▫The function should return the grade letter with a compliment follow by the user name. Then display to the screen ▫If user put 90, the compliment should be “You got an A, Good job! Kemtis”
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.