Sunday, May 30, 2010

Making Windows PowerShell your own

Previously we looked at some of the different ways one can extend Windows PowerShell. Now we'll learn some basic for actually using these methods.

You may notice that I've skipped creating snap-ins here, as that requires developer skills. Also, we will cover modules and advanced functions in detail in a future article.

Functions

Functions are one of the elementary building blocks of Windows PowerShell, and they can vary in complexity from a single line to full-blown modules. Still, the basic format and components remain the same.

The most basic syntax is:
function <> { <> }

The standard format contains four basic constructs, which are Param and three script-blocks or clauses -- Begin, Process and End.

  • Param -- Defines parameters for the function. If not provided, the function will use groups the parameters passed to the function in a variable called $args.
  • Begin (script-block) -- Begin is used for function setup. This code is only called the first time a function is initialized.
  • Process (script-block) -- Process is used in a pipeline and is processed for every item in the pipeline. You reference the current item using "$_".
  • End (script-block) -- End is ... well, the end. Just like Begin, it is only processed once, but instead of during initialization it is processed right before the code is completed. It is also the default block, which means if you do not specify any code blocks it implies that all the code is the end block.

Example of syntax:
Function <>
{
Param()

# Script Blocks
Begin{}
Process{}
End{}
}

    Note: Notice the {} on the end of each script block. The code is placed in between. For example: Process { Write-Host "Processing element $_" }

For more details try Get-Help about_functions.

Filter

I almost didn't want to mention these, but I decided it would be a disservice to the reader not to. Filters are not used very often in Windows PowerShell and are really not needed. This is just like a function but all the code is run as "Process".

Again, for more details try Get-Help about_functions.

Scripts

A script in its simplest form is one or more Windows PowerShell commands saved in a .ps1 file. There is the famous "Hello World" example in PowerShell, which is very much like a batch file except it can support the same constructs as a function. In actuality, you can simply remove the function {} from a function and save it in a file to have a script:

Param($Name)
Begin{"Initializing Script with Parameter $Name"}
Process{" You just passed $_ to me"}
End{" Jobs done… lets cleanup"}

For more details, try Get-Help about_scripts.

Profile

A profile is basically just a Windows PowerShell script that is processed every time PowerShell is started. This is similar to autoexec.bat from back in the old days. We've covered profiles in Windows PowerShell before, so I won't rehash it again here. I will let you in on a little known fact though: PowerShell has a built-in variable with all the profile locations wrapped up nicely for you. This can be used to create and edit your profile:

  • $Profile -- This is the built-in variable that contains information about profile location. It contains four useful properties discussed below.
  • $Profile.AllUsersAllHosts -- Contains the location of the All Users profile (Global) for all PowerShell hosts.
  • $Profile.AllUsersCurrentHost -- Contains the location of the All Users profile (Global) for the current PowerShell host.
  • $Profile.CurrentUserAllHosts -- Contains the location of the Current User profile for all PowerShell hosts.
  • $Profile.CurrentUserCurrentHost -- Contains the location of the Current User profile for the current PowerShell host.

To wrap up, these are just a few ways you can customize and extend Windows PowerShell for your own use. By using functions, scripts, and creating a custom profile, you can start down the road to nirvana when it comes to an automation environment.

No comments:

Post a Comment