Top Banner
MS WINDOWS POWER SHELL CSE 535 Operating Systems
26

MS WINDOWS POWER SHELL CSE 535 Operating Systems.

Dec 26, 2015

Download

Documents

Kory Morris
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

MS WINDOWS POWER SHELL

CSE 535 Operating Systems

Page 2: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

What is Power Shell

PowerShell is Microsoft Windows’ object-oriented programming language interactive command line shell

PowerShell designed to ◦ automate system tasks: such as batch processing

◦ create systems management tools ◦ for commonly implemented processes

Page 3: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

What is it for?

The PowerShell language is similar to PerlPerl. PowerShell includes

◦more than 130 standard command line tools ◦ for functions that formerly required users

to create scripts in VBVB, VBScriptVBScript or C#C#.

Page 5: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

Windows PowerShell CmdletCmdlet

CmdletCmdlet (or Command-let) ◦which .NET objects can be accessed

from the command line. ◦A Cmdlet name consists of two elements:

a verb and a noun

Example: ◦one of the most useful Cmdlets:

Get-help

Page 6: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

Automating Tasks PowerShell offers ways

◦ to automate tasks:

Cmdlets:◦ very small .NET classes ◦ appear as system commands

Scripts:◦ combinations of cmdlets and associated logic.

Executables:◦ standalone tools

Instantiation of standard .NET classes.

Page 7: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

Integrating with .NET

PowerShell integrates with the .NET environment Can be embedded within other applications.

Over a hundred cmdlets are included to be used separately or combined with others

to automate more complex tasks Users can

also create and share cmdlets.

Page 8: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

Abbreviations for Cmdlets

there are abbreviations ◦ for some of the Cmdlets, for example:

Get-ProcessGet-Process

Can also be typed as:

psps

Page 9: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

Linux-similar Cmdlets

“ps” ◦ is command familiar to Linux users ◦ as the command

to view details about currently running processes.

there are a number of ◦ other Linux-PowerShell parallels:

“man” is the same as “Get-Help –detailed”“ls” is the same as “Get-ChildItem”“pwd” is the same as “Get-Location”

Page 10: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

Understanding AliasesPowerShell commands are usually long.

◦example: Get-ChildItem. However, PowerShell has aliases.

◦Example:◦ “dir” is the same as “Get-ChildItem”

get-alias get-alias

get-alias dir get-alias dir

Page 11: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

Obtaining PowerShell

PowerShell is installed in Windows Server 2008 is also part of Windows 7,

it does not come ready loaded with XP or Vista!

Page 12: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

Running PowerShellOnce PowerShell has been installed

◦ then it can be run in two ways:

Click on “Start”, “Run” ◦and then type in “powershell”

Start a new command prompt ◦and then type in “powershell”

the user can start using ◦ the PowerShell commands.

Page 13: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

Cmdl: psOne very useful Cmdlet is psps

◦or get-processlists the currently running processes

◦ the number of memory page (frame)s◦ the percentage of processor usage

Page 14: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

Pipe and Sort

The list is sorted alphabetically ◦according to the process name.

You can sort WS field ◦According to the memory usage

To do this the user ◦ “pipes” the output of ps ◦ to a second Cmdlet “sort-object”

ps | sort-object WS –descendingps | sort-object WS –descending

Page 15: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

Sorting wrt Memory Usage

Page 16: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

Filtering & Splitting the Commands

User can filter that◦ the “powershell” not to be seen in output

Add a filterpiped Cmdlets can be

◦split over multiple lines:

ps |ps | where-object –FilterScriptwhere-object –FilterScript{$_.processname -ne "powershell"} {$_.processname -ne "powershell"} || sort-object WS –descendingsort-object WS –descending

Page 17: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

Filtered

Page 18: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

To concatenation symbolThe concatenation symbol (>)

◦ will send the result output file ◦ after the information has been converted to HTML ◦ so that it can then be viewed in a web browser:

ps |ps |where-object -FilterScript where-object -FilterScript {$_.processname -ne "powershell"} |{$_.processname -ne "powershell"} |sort-object WS –descending |sort-object WS –descending |convertto-html -property Name,convertto-html -property Name,WS > ps.htmlWS > ps.html

Page 19: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

Displaying in Browser

Page 20: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

Find all the processes on a computer that started today

Get-Process | Where {$_.starttime -ge Get-Process | Where {$_.starttime -ge [datetime]::today}[datetime]::today}

Page 21: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

Find the processes that use more than 1000 MB of memory and kill them

get-process | where-object { $_.WS -gt get-process | where-object { $_.WS -gt 1000MB } | stop-process -whatif 1000MB } | stop-process -whatif

Page 22: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

Calculate the number of bytes in the files in a directory

get-childitem | measure-object -property get-childitem | measure-object -property length -sum length -sum

Page 23: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

Defining Variables

PS C:\> $a = 5 $a = 5

PS C:\> $a $a

5

PS C:\>

Page 24: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

Determine Variable

PS C:\> $a.GetType() $a.GetType()

Page 25: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

Piped Commands

dir | sort LastWriteTime | moredir | sort LastWriteTime | more

Page 26: MS WINDOWS POWER SHELL CSE 535 Operating Systems.

PS C:\WINDOWS> $a = dir | sort $a = dir | sort LastWriteTimeLastWriteTime

PS C:\WINDOWS> $a[0] $a[0]

PS C:\WINDOWS> $a[1] $a[1]