630 likes | 719 Views
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.
E N D
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 • 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!
What is PowerShell? • PowerShell is • a scripting language • a command line (cmdlets) • automation tool • Object-oriented
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
Object, Method, and Properties • Exercise 20 minutes: • Name 3 objects • Name at least 3 methods of each object • Name 3 properties of each object
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
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
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!!!”
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
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
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
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
$Host VS $psISE • Both are the same variable, one is for command and the other is for ISE • $Host • $psISE • Try: • $psISE.options
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
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
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
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”
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
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
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
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
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)
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\’
Color-Coded Directory Listing (2) • Mode property refer to the folder/file attributes such as directory (d), archive (a), read-only (r),
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
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
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
Knock Knock Joke (4) • Clear-Host • Write-Output "The Knock Knock Joke" • Write-Output "" • Write-Output “CN1266 - 2010" • Start-Sleep -Seconds 3 • Clear-Host
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
Script Template • #*********************************# • # Script Name: • # Version: • # Author: • # Date: • # • # Description: • # • #*********************************#
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
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.
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.
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 • #
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
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
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
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"
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
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
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
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 " • }
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 " • }
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 " • }
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 " • }
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"
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"
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"
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