Some Powershell commands (or cmdlets) – Part 3


Note: As you are going to see in some examples here, Posh allows you to use the pipeline operator ” | ” to pass an object. This is essential when creating Posh scripts

So, when you “pipe” an object from one part of a command to another, you’re simply passing that object – unchanged – from one part of the command to another (from the Posh Manual)

Get-Content: Gets the content of the item at the specified location; 3 aliases: cat, gc and type

Examples:

cmdlet example:
get-content c:\abc.txt

Aliases examples:
cat c:\abc.txt
gc c:\abc.txt
type c:\abc.txt

Getting online help: http://technet.microsoft.com/library/hh849787.aspx
==============

Script – Baby steps

This is a two step process to list all processes for 2 computers:

1. Create a text file called cn.txt in the c:\temp folder
Add two computer names into it
Save and close the cn.txt file
2. now type:
get-process -cn (cat c:\temp\cn.txt)
This will list all processes for the two computers listed in cn.txt
==============
Get-History – Gets a list of the commands entered during the current session.
Aliases: ghy, h, history

Examples:
Get-History
Get-History | Where-Object {$_.CommandLine -like “*Service*”}
h

==============

Invoke-History – Runs commands from the session history.

Aliases: ihy, r

Examples:

Invoke-History

Invoke-History -Id 132

r 132

Note: This command runs the command in the session history with ID 132. Because the name of the Id parameter is optional, you can abbreviate this command as “Invoke-History 132", "ihy 132", or "r 132“.

Get-History -Id 255 -Count 7 | Invoke-History

This command runs the 7 commands in the history that end with command 255 (typically 249 through 255). It uses the Get-History cmdlet to retrieve the commands.  The pipeline operator (|) passes the commands to Invoke-History, which executes them

90..92 |foreach-object {Invoke-history -id $_}

This command runs commands 90 through 92. Because you can list only one ID value, the command uses the ForEach-Object cmdlet to run the Invoke-History command once for each ID value

online help

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: