Windows Vista Forums

powershell "optimization" challenge
  1. #1


    =?Utf-8?B?S2FybCBQcm9zc2Vy?= Guest

    powershell "optimization" challenge

    I wrote some powershell code to do a specific task, and am interesting if
    people can make it more elegant, or more "powershell"ish..

    the context is i want to search through recursive directories on a webserver
    look for files of a certian types (i.e .cfm .htm etc), then give a list of
    files that contain a certian text in it.. giving a table with 3 columns, one
    with the filename, the next with the line number , and the third the line
    that has that text..

    this is how i did it:

    ls "\\somepath\wwwroot\" -re |
    where { ".cfm",".htm",".asp",".html" -eq [system.IO.Path]::GetExtension($_
    ) } |
    % { $linenum = 0; $temp = $_ ; $_} | get-Content |
    Where-Object {$_ -match "www.whatever.org" } |
    % {$res = 1 |Select-Object "name", "contentline", "linenum"; $res.Name =
    $temp.name;$res.contentline = $_ ; $res.linenum = $linenum++ ; $res}




    How would you do it?

      My System SpecsSystem Spec

  2. #2


    Jouko Kynsijärvi Guest

    Re: powershell "optimization" challenge

    Karl Prosser wrote:
    > the context is i want to search through recursive directories on a
    > webserver look for files of a certian types (i.e .cfm .htm etc), then
    > give a list of files that contain a certian text in it.. giving a
    > table with 3 columns, one with the filename, the next with the line
    > number , and the third the line that has that text..


    > ls "\\somepath\wwwroot\" -re |
    > where { ".cfm",".htm",".asp",".html" -eq
    > [system.IO.Path]::GetExtension($_ ) } |
    > % { $linenum = 0; $temp = $_ ; $_} | get-Content |
    > Where-Object {$_ -match "www.whatever.org" } |
    > % {$res = 1 |Select-Object "name", "contentline", "linenum";
    > $res.Name = $temp.name;$res.contentline = $_ ; $res.linenum =
    > $linenum++ ; $res}


    gci -r -i *.cfm,*.htm,*.asp,*.html | select-string www.whatever.org | ft
    filename,linenumber,line



      My System SpecsSystem Spec

  3. #3


    =?Utf-8?B?S2FybCBQcm9zc2Vy?= Guest

    Re: powershell "optimization" challenge

    thats so nice and elegant..
    i think that select-string is just want i needed, i mean they made it just
    for me for this thing, where it persists the filename that was passed through
    the input file, and also persists the line number and contents, in a way i
    was just trying to make select-string..

    lessons learned:

    look at the input parameters for commandlets carefully, don't write them off
    too early (i hadn't assumed that select-string could take in fileobjects, nor
    that it would even pump out an object that has a filename parameter..



      My System SpecsSystem Spec

  4. #4


    Jeffrey Snover [MSFT] Guest

    Re: powershell "optimization" challenge

    > look at the input parameters for commandlets carefully, don't write them
    > off
    > too early (i hadn't assumed that select-string could take in fileobjects,
    > nor
    > that it would even pump out an object that has a filename parameter..


    We thought people would like this.
    It is actually a little bit better.
    You can pipe the output of one select-string into another select-string and
    the second will only search the lines that from the first select-string.
    Try it out. It rocks!

    --
    Jeffrey Snover [MSFT]
    Windows PowerShell/Aspen Architect
    Microsoft Corporation
    This posting is provided "AS IS" with no warranties, no confers rights.
    Visit the Windows PowerShell Team blog at:
    http://blogs.msdn.com/PowerShell
    Visit the Windows PowerShell ScriptCenter at:
    http://www.microsoft.com/technet/scr.../hubs/msh.mspx




      My System SpecsSystem Spec

powershell "optimization" challenge

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: "andy t" not up to challenge about failure of WSR scan doc feature andy t Vista General 0 10 Jan 2010
Unwanted Multiple contacts in "To","CC","BCC" of email send catago xsailer Vista mail 2 12 Oct 2008
Vista not wotking with "My Computer" or "Control Panel", "Screen Saver" Platebanger Vista General 6 05 Feb 2008
"Desktop Optimization Pack" as Winternals upgrade... how does a user get it? Modern Meta Vista General 7 01 Mar 2007
WM5 Sync with Vista "Windows Calender", "Contacts", and "Mail" Tony Vista General 1 16 Feb 2007