CYBR 3210 (Bryant) - Chapter 10: Scripting in Windows 10 and CentOS 7

Ace your homework & exams now with Quizwiz!

•ISE key features:

-Brace matching - locates the closing brace if you have an opening brace selected -IntelliSense - Displays clickable menus of cmdlets, parameters, parameter values, files, or folders that match characters you type -Drag and drop text editing - select text and drag it to another location in the editor -Parse error display - errors indicated with red underlines -Context-sensitive help - press F1 to get help

•Rules for using PowerShell and avoiding errors:

-Cmdlet names are always a combination of a verb and noun, separated by a dash -Cmdlets and parameters are always separated by a space -When there are multiple parameters in a command, each parameter is separated by a space -Every parameter is preceded by a hyphen (-)

Header Line

-First line in a script -Indicates the type of shell environment under which the script is run •The following line shows a header line for a bash script: -#! /bin/bash

•Formatting Data

-Format-Table cmdlet allows you to control which property values are presented -With the -Property parameter, you can select which properties you want to display and change their order of display -Review the Help for Format-Table for examples -Notice PM numbers are large •To scale these numbers type, Expression=$_.PM/1MB to divide each PM by 1 MB

Selecting

-Selecting items to remain in a table after removing unwanted items using the Select-Object cmdlet

Filtering Data

-The report will be more meaningful if you eliminate the powershell _ise process -Use filtering capabilities with the Where-Object cmdlet -Type Help Where-Object -Examples to see examples for how to use Where-Object -To use filtering, you need to know the comparison operators defined in Table 10-1 To eliminate powershell_ise process, select all processes that aren't equal to powershell_ise process

•Selecting Data

-To select specific data (example: the top 10 PM values in your report) •Use Select-Object (use Help to review examples) -Last option is documented in the Help file For Select-Object, Powershell also has an undocumented -First option - selects specified number of lines from the front of the cmdlet

•if Command

-Two formats used: •if [decision placed here] ; then •[command list here] •fi -Or •if [decision] ; then •[command list here] •else •[command list here] •fi With both if command constructs, the decision is made by testing a variable This variables may change as the script is run

•while Command

-Use to enable multiple executions of a set of instructions while a certain condition exists -General syntax used: •While [loop decision] do •done Limit for the loop comes from testing a changing variable

•for in Command

-Use to execute a number of instructions based on a maximum count you specify -General syntax: •for [somevalue] in [limitation variable] do •done

Script

A program that consists of a set of commands to control some function of an OS Can be set to execute in a scripting shell - PowerShell in W10 - Bash in CentOS7

PowerShell

A task automation FW from Microsoft that consists of a command-line shell and an associated scripting language

Script Pane

Above the Console pane Editor pan in the ISE Can create text files, cut, copy, paste

Bash

An acronym for the CentOS 7 Bourne-again shell

Console Pane

Bottom left Similar to PowerShell command prompt Outputs of scripts appear in this pane as you run them in the ISE

Formatting

Making the output look more pleasing with the Format-Table cmdlet

Cmdlet

PowerShell introduces cmdlet Single-function command-line tools built into PowerShell Use cmdlets separately Power is when you chain them in sequence to perform complex tasks

Character $_.

Represent a dynamic variable that stores the current information being piped to it

Filtering

Selecting items to remain in a table after filtering out unwanted items using the Where-Object cmdlet

•Sorting Data

Sort-Object Know params of an object to sort the table -Descending = sort starts with highest values Ending in a pupe character make cmdlets continue on next line

Command Add-on Pane

To the right of the Console pane You can search for cmdlets Click arrow beside All to display all commands available in PowerShell - filter these commands

Filtering, Sorting, Selecting, and Formatting Scripts

You only want top 10 Processor Memory in your report Report is more meaningful if memory used by PowerShell is excluded Useful operations below

Sorting

arranging a table in a particular order with the Sort-Object cmdlet

PM (MB)

indicated that the values are scaled by dividing by 1 MB

Label

provides a way to customize the column heading

Tracing the Script

•A simple way to debug a script is to use script-level tracing -Each command that is executed is displayed in the Windows PowerShell console •To enable script tracing -Use the Set-PSDebug cmdlet and specify one of the three levels for the -trace parameter: •0-Turns off script tracing •1-Traces each line of the script as it is executed •2-Traces each line of the script and displays variable assignments ProcessName was powershell_ise Where-Object {$_.ProcesName -ne -'powershell_ise' >>>>

Using the ISE Toolbar

•Eight of the first nine buttons on the toolbar are similar to those found in Microsoft Office products -To clear output pane, click the Clear Console Pane button, which looks like a blue squeegee •Next five buttons control execution of scripts -Launch a script by pressing F5 or click the icon that displays a green triangle -If you need to stop execution, press Ctrl+Break or click the Stop Operation button To run a PowerShell script on a remote computer - click New Remote PowerShell - enter a computer name, username, and password - script runs on remote computer To run PowerShell script in a new session on same computer - select Start PowerShell.exe Next three buttons control display and placement of three panes in ISE - for more info, highlight cmdlet - select Show Command Window

Using Get-Command and Help

•Get-Command is one of the more useful cmdlets -You can use it to find a cmdlet you need for the script you are writing •To find a cmdlet that creates a new file -Type Get-Command New* -Type cmdlet -The parameter -Type is an alias for -CommandType -If you do not see the cmdlet that you assume is the right one (example:New-File), then you can use Help to learn what each cmdlet is used for •Example: type Help New-Item Get-Command leads to New-Item cmdlet - start search with New-Fle Get-Command is an alternative to Help when you need to locate a cmdlet Clicking the New File icon creates a new script file, not a file to be used within the script

Identifying ISE Panes

•ISE has three panes -Script, Console, and Command Add-on

Interpreting Errors

•In PowerShell -You will see red text in the parse error display to indicate errors -Error messages almost always include the line and char (character) number where PowerShell was unable to interpret your cmdlet

Working with IntelliSense

•Microsoft's IntelliSense -Is an "automatic completion" feature that helps you develop scripts more quickly -Displays clickable menus of cmdlets, parameters, and syntax when you use the ISE to create scripts Easier to discover cmdlets and syntax when you use the ISE to create scripts - Double-click a suggestion from menu -Example: •Typing Get-C tells IntelliSense to provide a list of cmdlets that start with Get-C

Using the Windows PowerShell Integrated Scripting Environment

•PowerShell ISE -Provides a rich scripting experience by enabling you to write, run, and test scripts in a graphical and intuitive environment -Makes PowerShell easier to use True power: helps you enter, update cmdlets easily Integrated development editor for PowerShell scripts

Creating a PowerShell Script for a Directory Tree

•PowerShell cmdlets equivalent to MD and CD -MKDIR and Set-Location

Using Profiles

•PowerShell profile -A script file that runs automatically each time the ISE starts -Can be used to configure your ISE environment -Can be set to load the common modules you use every time you open the shell •Windows PowerShell execution policies let you determine the conditions under which PowerShell loads configuration files and runs scripts -The RemoteSigned policy requires a digital signature for scripts received from an Internet site

Why Learn PowerShell and Scripting?

•Reasons you need to learn PowerShell scripting: -PowerShell scripting is here to stay -Virtually all current Microsoft products can be managed through PowerShell -Microsoft considers it important -It can make your job easier -Many GUIs are PowerShell front ends -Microsoft certification exams contain PowerShell questions -If you do not learn it, someone else will

Script Parameters

•Script parameters can contain any information you need to pass to a batch program when you execute it •Parameters $0 - $9 are separated by spaces, commas, or semicolons

Conditional Execution

•Script routines should have two important properties: -Command lines that are executed multiple times should be written only once to avoid errors in consistency -Structures must provide a clear selection between command lines when conditions in a script are tested

Clear Typing

•Suggestions to reduce the chance that PowerShell will misinterpret your cmdlets: -Capitalize the first letter of each word and leave other lowercase -Be sure to place spaces between the cmdlet and the first property and between the property and value -Lines can be terminated between cmdlets by a | pipe -You can break cmdlets between parameters using the backtick key (`) -Clear typing looks professionals

Exit Return Code

•The statement exit 0 is needed to end the script if all goes well -A return code of zero indicates success

Preparing a Report with a PowerShell Script

•This section covers how to build a script to report memory usage by the various processes running on your Windows 10 virtual machine •Section also covers how to construct scripts using incremental development -A method of script development in which the script is designed, implemented, and tested incrementally PowerShell Get-Command cmdlet is a good way to search for cmdlets PowerShell Help features are essential in clarifying the proper use of cmdlets Good rule of thumb: add cmdlets to the script only if they have a high probability in a PowerShell by reducing the time needed to discover and correct mistakes

Creating Script Files in CentOS 7

•This section covers how to build script files and enhance them by adding decision-making capabilities and repetition •You write scripts in CentOS 7 using a text editor such as Vim

Troubleshooting PowerShell Scripts

•This section presents some tools you can use to solve any errors or problems that may arise as you build scripts in PowerShell

Obtaining Process Data

•To find a cmdlet that obtains process values -Use the Get-Command -noun Process command •Use the Get-Proces -full command to provide definitions for each column Get-Process produces tabl with one row for each process and its statistics - table can be piped to additional cmdlets for further processing

Using PowerShell Help to Build Scripts

•To obtain help for the Get-ChildItem cmdlet -Type Get-Help Get-ChildItem | More in the Console pane and press Enter -With each Get-Help cmdlet, the NAME, SYNOPSIS, and SYNTAX are displayed •Summary of the syntax characters for the Get-ChildItem cmdlet: -A hyphen (-) indicates a parameter name -Angle brackets (< >) indicate placeholder text -Brackets ([ ]) indicate optional items •To find information for cmdlets: -Type Help 'Create File' To list cmdlets that create files •PowerShell provides a -WhatIf parameter that shows what would happen if you chose to run a cmdlet Help cmdlet is designed to search intuitively for needed info

File Extensions

•Using an extension on script files is optional •To avoid confusion with other file types -Some system administrators prefer to use an .sh extension on their scripts

Executable Permission

•You must change the permissions on the file before running it -The default permissions do not include execution permissions •The chmod command modifies the permission of the file •The following command sets myScript as executable: -chmod 755 myScript


Related study sets

Insurance Licensing - Chapter 4: Dwelling Policy

View Set

NUTR 223 (UNR) NC Connect: Mediterranean diet

View Set

chapter 3 and 4- Consumer Preferences and the Concept of Utility

View Set

Muscle contractions ch 7 kin 130

View Set

Ignatavicius Chapter 34: Care of Patients With Dysrhythmias

View Set

Comprehensive physical assessment of a child

View Set