CN1266 Network Scripting Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+
Agenda Chapter 14: Using Functions to Divide and Conquer Quiz Exercise
First function function GetOsInfo{ ▫$OsInfo = Get-WmiObject Win32_operatingsystem ▫write-host ($osinfo.caption + " " + $osinfo.version) } Getosinfo
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
Defining more than one parameters function greeting ($firstname, $lastname){ ▫write-host ("Hello " + $firstname + " " + $lastname) } greeting "Kemtis" "K."
Defining more than one parameters (2) function greeting{ ($firstname, $lastname) ▫write-host ("Hello " + $firstname + " " + $lastname) } greeting "Kemtis" "K."
Defining more than one parameters (3) function sum([int]$a, [int]$b){ ▫write-host ($a + $b) } sum 1 2
Return value from function function greeting($firstname, $lastname){ ▫return ($firstname + " " + $lastname) } $greet = greeting "Kemtis" "K." $greet += ", how are you?" Write-Host $greet
Return value from function (2) function dirwin{ return Get-ChildItem $env:windir } dirwin
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 ▫}
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*
Watching out for name overlap function scopetest{ $myvar = "Jeremy" Write-Host $myvar } $myvar = "Royski" Write-Host $myvar scopetest
Watching out for name overlap (2) function changename{ $name = "Bradley" } $name = "Jason" Write-Host $name changename Write-Host $name
Watching out for name overlap (3) function changename{ $script:name = "Bradley" } $name = "Jason" Write-Host $name changename Write-Host $name
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
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
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
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”