Welcome to Sign in | Join | Help
in
Home Blog Forums

The Lazy Admin

PowerShell DIY commands (using functions)

Sponsor


As you might have guessed I'm a lazy admin :) Now being a lazy admin doesn't mean I sit around pondering whether captain Kirk is better than Captain Picard, and all the while, hope somebody is taking care of what I'm supposed to be doing. It means I work smarter not harder. Yes I know 2 clichés in the same paragraph. And everyone knows Picard is totally the best Captain.

So the point of all this is PowerShell has this awesome feature for creating functions. What is a function? Well for those who don't have a programming/developers background, a function is a chunk of code that performs an easily reusable set of instructions.

To draw an analogy, a car has many parts. Each part performs a specific function. If we were to lump every function of every part of a car into one giant part it would be much harder to design. It would also be harder to fix, and much more expensive to replace.

When programmers code applications they use the same idea. If they write up some code that does one thing very well. For instance format a string of text; they will often put this generic code into a separate function. This way every time they want to format text in the same way then need only call that function.

So why is this interesting in PowerShell? Because we can build up functions that do complex operations and then call them using one command instead of 10 or 15. This makes me happy because then I can be lazy, yet still look good because I can get so much done so fast.

For this example I made a function that searches txt files in a given folder for a string.

Function Search-Folder {

param([string] $searchstring,[string] $searchFolder)

    $folder = get-childItem($searchFolder)

    foreach ($item in $folder)

    {

        if (($item).name -like "*.txt")

        {

        $lines = @(Get-Content ($item.FullName))

            for ($i = 0; $i -lt $lines.count; $i++)

            {    

                if ($lines[$i].Contains($searchstring))

                {

                    Write-Host "File " ($item).name "Matches Search"    

                }

            }

        }

    }

}

You can use this example by copy and pasting the above it into an empty text file.

Save the file as SearchFolder.ps1

Next you need to dot source the file into Powershell. To do this you may have to lower your Execution policy level.

Type> Set-ExecutionPolicy unrestricted

* note if you are running Windows Vista, you will have to elevate the privileges of the PowerShell session in order to affect this change. This allows you to run scripts for testing. You should only run PowerShell with this option in a secured and non production environment.

Next you need to dot –source the function. This lets you add functions and other pre-written code to your current PowerShell Environment.

Type> . ./SearchFolder.ps1

For clarity. That is [period][space][period] [forward slash]

Finally you can use the function to search text files in a folder by calling your new built in function.

Search-Folder "test string","c:\test_folder_of_TXT_Files"

Go ahead and play around with the script. You can make functions out of any list of commands you may encounter in PowerShell. Another cool function you can use is the on the Sapien Scripting blog maintained by Don Jones. it allows you to execute a Live search, then store the results in PowerShell objects.





Published Wednesday, May 09, 2007 8:30 AM by daniel.nerenberg
Filed under:

Comments

No Comments
Anonymous comments are disabled

About daniel.nerenberg

I am an MCT, Consultant based out of Montreal Quebec Canada. As the "new" Lazy Admin on the block I am working to make TheLazyAdmin.com the best website for MS Software tips and tricks out there!


All postings are provided "AS IS" with no warranties, and confer no rights.
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.