1 / 63

CN1266 Network Scripting V1.1

CN1266 Network Scripting V1.1. Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+. Agenda. Introduction Chapter 1: The Windows PowerShell Rap Sheet Chapter 2: Customizing and Shortcutting the Environment Chapter 3: A Pinch of Shell, a Pound of Power. Icon Used in the book.

coy
Télécharger la présentation

CN1266 Network Scripting V1.1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CN1266 Network Scripting V1.1 Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+

  2. Agenda • Introduction • Chapter 1: The Windows PowerShell Rap Sheet • Chapter 2: Customizing and Shortcutting the Environment • Chapter 3: A Pinch of Shell, a Pound of Power

  3. Icon Used in the book • Tips highlight a point that you should know • Things to prevent something bad from happening • Can skip but you should spend sometime with it • Remember!

  4. What is PowerShell? • PowerShell is • a scripting language • a command line (cmdlets) • automation tool • Object-oriented

  5. Object, Method, and Properties • What is an Object? • Object has method and properties • IE. Box is an object • Box’s methods are put it in, taken out, and re-arrange • Box’s properties are height, width, water resistance, etc

  6. Object, Method, and Properties • Exercise 20 minutes: • Name 3 objects • Name at least 3 methods of each object • Name 3 properties of each object

  7. How to install PowerShell • If you are not using Windows 7 • Uninstall PowerShell 1 via control panel • Download and install • Microsoft .NET Framework 2.0 • Microsoft .NET Framework 3.5.1 • WinRM 2.0 CTP3 • Windows PowerShell 2

  8. How to work with PowerShell • Command lines? • Start -> All programs -> Windows PowerShell V2 -> Windows PowerShell v2 • GUI? • Windows PowerShell Integrated Shell Environment (ISE) • Script/Editor pane • Output pane • Command pane

  9. Personalizing the command shell • To identify each windows open easily • Change the color, background and foreground • $host.UI.RawUI.BackgroundColor=“magenta” • $host.UI.RawUI.ForegroundColor=“blue” • Change the window size • $host.UI.RawUI.BufferSize • $host.UI.RawUI.WindowSize • Change the window title • $host.UI.RawUI.WindowTitle=“I rule!!!”

  10. Personalizing the command shell (2) • In order to change window size, you have to assign a new variable • $size = $Host.UI.RawUI.WindowSize • $size.Width = 100 • $size.Height = 25 • $Host.UI.RawUI.WindowSize = $size

  11. Personalizing the command shell (3) • To keep the change permanent, we can utilize the profile • $profile – to see the profile location • Microsoft.PowerShell_profile.ps1 • Microsoft.PowerShellISE_profile.ps1 • Try this code in your profile. • Clear-Host or CLS – is to clear the screen • See Page 25

  12. Execution Policy • For better security, PSH does not allowed to run unless you set execution policy • 3 Level of policy are • Allsigned - Only permits scripts that have a trusted signature to execute on your computer • Remotesigned - Permits PowerShell scripts downloaded from the web to run only if they are from a trusted source • Unresricted - Allows any PowerShell script to run on your computer

  13. Execution Policy (2) • By default, PSH’s execution policy is Restricted • You have to set the level to one of the three before you run your first script • Set-ExecutionPolicy Unrestricted • Set-ExecutionPolicyRemoteSigned • Set-ExecutionPolicyAllSigned

  14. $Host VS $psISE • Both are the same variable, one is for command and the other is for ISE • $Host • $psISE • Try: • $psISE.options

  15. Adding function to the ISE menu Function My-Function{ Write-Host “Running my own function” } $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(“Run it”,{My-Function},”Shift+Ctrl+f”) • Replace ”Shift+Ctrl+f” to $null if you don’t want any shortcut

  16. Aliases • All command are in verb-noun format • A second name to whatever command you designate to it • New-Item -path alias:cl –value c:\windows\system32\calc.exe • new-item -path alias:np -value c:\windows\notepad.exe • set-item -path alias:np -value c:\windows\system32\calc.exe

  17. Aliases (2) • Scope options • None • Constant:Constant – can’t be deleted or change value • ReadOnly:ReadOnly – can be deleted. To change value, you have to supply Force option • Private:Private – can be seen only with the current scope • AllScope:AllScope

  18. Aliases (3) • New-Item –path alias:cl –value c:\windows\system32\calc.exe –options “AllScope, Constant” • New-Item –path alias:np–value c:\windows\system32\notepad.exe –options “Readonly”

  19. Aliases (4) • To rename alias • Rename-Item alias:np –newname note • Alternative way to create alias • New-Alias npcl c:\windows\system32\notepad.exe • Set-Alias npcl c:\windows\system32\calc.exe

  20. Aliases (5) • To delete alias • Remove-Item alias:npcl • Remove-Item alias:myalias* • To remove all aliases that has specific pattern • Remove-Item alias:se-force

  21. Aliases (6) • Alias Drive • Set-location alias: OR CD alias: • Get-ChildItem * OR Dir * • Creating Persistent Aliases • Export-Alias C:\myaliases.txt • Option: • -noclobber – returns error if the file already exists • -append • Import-Alias C:\myaliases.txt

  22. Tab Expansion • All you need is the first few letters of the command, file, or folder then press the Tab key • Try: • Cd win -> Tab • C:\windows\sys -> Tab -> Tab again • $size = $Host.UI.RawUI.WindowSize • $size.w -> Tab

  23. Time for your first script! • Try these on either notepad or ISE • Save file with .ps1 file extension • #REQUIRES Version 2 – if you want your code to be V2 only • Write-Output “Hello World!” • Echo “Hello World!” • $name = Read-Host “What’s your name?” • Write-Host (“Hello “ + $name)

  24. Color-Coded Directory Listing • See Listing 3-1 on Page 40 • Set-ExecutionPolicy? • If your script is in the same directory, • ./script.ps1 • If you want to refer to the script path • &’c:\documents and settings\admin\’

  25. Color-Coded Directory Listing (2) • Mode property refer to the folder/file attributes such as directory (d), archive (a), read-only (r),

  26. Knock Knock Joke • Clear-Host • $userReply = "" • while ($userReply -ne "Who is there?"){ • $userReply = read-host "Knock Knock!" • } • Clear-Host • while ($userReply -ne "Orange who?"){ • $userReply = read-host "Orange." • } • Clear-Host • Write-Output "Orange you glad you created this PowerShell script?“ • Start-Sleep -Seconds 5

  27. Knock Knock Joke (2) • Clear-Host • while ($userReply -ne "Who is there?"){ • $userReply = read-host "Knock Knock!" • } • Clear-Host • while ($userReply -ne "Orange who?"){ • $userReply = read-host "Orange." • } • Clear-Host • Write-Output "Oranges are oranges but this is PowerShell scripting!" • Start-Sleep -Seconds 5

  28. Knock Knock Joke (3) • Clear-Host • while ($userReply -ne "Who is there?"){ • $userReply = read-host "Knock Knock!" • } • Clear-Host • while ($userReply -ne "Banana who?"){ • $userReply = read-host "Banana." • } • Clear-Host • Write-Output "Orange you glad I didn't say orange?" • Start-Sleep -Seconds 5

  29. Knock Knock Joke (4) • Clear-Host • Write-Output "The Knock Knock Joke" • Write-Output "" • Write-Output “CN1266 - 2010" • Start-Sleep -Seconds 3 • Clear-Host

  30. Exercise (2) • Modify the knock knock jokes to your own jokes. • Take credit for you work by modifying the developer information that is displayed at the end of the script

  31. Script Template • #*********************************# • # Script Name: • # Version: • # Author: • # Date: • # • # Description: • # • #*********************************#

  32. Exercise 2 • Use a script template and write a script to: • Adding a new function to ISE • Add new alias • Capture the output screen and result

  33. The story of three amigos • Create a new script file using the PowerShell template and add opening statements. • Declare variables used throughout the script file. • Display the introduction screen. • Display game instructions.

  34. The story of three amigos (2) • Collect first player input. • Collect additional player input. • Display the opening portion of the story. • Display the rest of the story.

  35. The story of three amigos (3) • # ******************************* • # • # Script Name: ThreeAmigos.ps1 (The Story of the Three Amigos) • # Version: 2.0 • # Author: Your Name • # Date: November 3, 2010 • #

  36. The story of three amigos (4) • # Description: This PowerShell script is a mad-lib styled game that tells • # a humorous story using input provided by the player. • # • # ******************************* • #Clear the Windows command console screen • Clear-Host

  37. The story of three amigos (5) • #Define the variables used in this script to collect player input • $animal = "" #Stores the name of an animal supplied by the player • $vehicle = "" #Stores the name of a vehicle supplied by the player

  38. The story of three amigos (6) • $store = "" #Stores the name of a store supplied by the player • $dessert = "" #Stores the name of a dessert supplied by the player

  39. The story of three amigos (7) • #Display the game's opening screen • Write-Host (x4 times) • Write-Host " T H E S T O R Y“ • Write-Host (x3 times) • Write-Host " O F T H E T H R E E A M I G O S"

  40. The story of three amigos (8) • Write-Host (x3 times) • Write-Host " By YourName." • Write-Host (x10 times) • Write-Host " Press Enter to continue.“ • #Pause script execution and wait for the player to press the Enter key • Read-Host

  41. The story of three amigos (9) • #Clear the Windows command console screen • Clear-Host • #Provide the player with instructions • Write-Host (x7 times) • Write-Host " This is an interactive mad-lib styled story. Before it can be" • Write-Host

  42. The story of three amigos (10) • Write-Host " told, you must answer a few questions." • Write-Host (x13 times) • Write-Host " Press Enter to continue.“ • #Pause script execution and wait for the player to press the Enter key • Read-Host

  43. The story of three amigos (11) • #Ask the player the first question • while ($animal -eq ""){ • Clear-Host • #Clear the Windows command console screen • Write-Host (x6 times) • $animal = read-host " Enter the name of a scary animal " • }

  44. The story of three amigos (12) • #Ask the player the second question • while ($vehicle -eq ""){ • Clear-Host • #Clear the Windows command console screen • Write-Host (x6 times) • $vehicle = read-host " Enter the name of a transportation vehicle " • }

  45. The story of three amigos (13) • #Ask the player the third question • while ($store -eq ""){ • Clear-Host #Clear the Windows command console screen • Write-Host (x6 times) • $store = read-host " Enter the name of your favorite store " • }

  46. The story of three amigos (14) • #Ask the player the fourth question • while ($dessert -eq ""){ • Clear-Host #Clear the Windows command console screen • Write-Host (x6 times) • $dessert = read-host " Enter the name of your favorite dessert " • }

  47. The story of three amigos (15) • #Clear the Windows command console screen • Clear-Host • #Provide the player with instructions • Write-Host (x5 times) • Write-Host " Once upon a time there were three very special children“ • Write-Host " named Alexander, William, and Molly. Alexander was the oldest"

  48. The story of three amigos (16) • Write-Host " and was known to be brave and strong. Molly, the youngest," • Write-Host " was just seven years old, yet she possessed an extraordinary" • Write-Host " sense of awareness that even the wisest sage would" • Write-Host " admire and respect. William, the middle child, was both brave"

  49. The story of three amigos (17) • Write-Host " and wise many times beyond his years. They lived together at" • Write-Host " the top of a hill, just outside the outskirts of town, where" • Write-Host " they faithfully watched over the townsfolk. Always together" • Write-Host " and always looking out for each other and the people in the"

  50. The story of three amigos (18) • Write-Host " town, they were known by everyone as The Three Amigos." • Write-Host (x7 times) • Write-Host " Press Enter to continue.“ • #Pause script execution and wait for the player to press the Enter key • Read-Host

More Related