Skip to main content

using powershell to replace “find” or “findstr”

this is one of those things i’m blogging to remind myself instead of bugging hal rottenberg. :)

in order to find something inside a list of files, you can use find or findstr. what’s the difference between those? findstr is a bit more robust, accepting pattern matches with regex, for example. in most cases though, i’m just looking for a string inside of a list of text files. so here we go with find and the general output we can expect…

C:\temp>find /i "wscript.echo" *.*

---------- DATE2INTEGER8.VBS
' wscript.Echo CurrentDate(Now)
' WScript.Echo CurrentDate(dDateThreshold)
      WScript.Echo oRecordSet.Fields("cn") & ":" & oRecordSet.Fields("displayname") & ":" & Integer8Date(oRecordSet.Fields("pwdlastset").Value,lBias)

---------- DLNAMES.TXT

---------- DNS_DEBUG.LOG

---------- TEMP_SCRIPT.SM
 WScript.Echo
 WScript.Echo "=========================================="
 WScript.Echo "Computer: " & strComputer
 WScript.Echo "=========================================="
    WScript.Echo "ArpAlwaysSourceRoute: " & objItem.ArpAlwaysSourceRoute
    WScript.Echo "ArpUseEtherSNAP: " & objItem.ArpUseEtherSNAP

i was kind of perturbed about having to switch back and forth from cmd shell to powershell so i asked hal about it one day… and he told me to use select-string. it turns out if acts differently if you don’t pipe anything to select-string. as you can see, the output is much nicer too …

[12] » Select-String -SimpleMatch -Pattern "wscript.echo" -Path *.* | Format-Table filename, linenumber, line -autosize

Filename          LineNumber Line
--------          ---------- ----
date2integer8.vbs         11 ' wscript.Echo CurrentDate(Now)
date2integer8.vbs         12 ' WScript.Echo CurrentDate(dDateThreshold)
date2integer8.vbs         33     WScript.Echo oRecordSet.Fields("cn") & ":" &
temp_script.sm             8    WScript.Echo
temp_script.sm            10    WScript.Echo "Computer: " & strComputer
temp_script.sm            18       WScript.Echo "ArpAlwaysSourceRoute: " &
temp_script.sm            19       WScript.Echo "ArpUseEtherSNAP: " &
temp_script.sm            20       WScript.Echo "Caption: " & objItem.Caption

i purposely wrote the command verbosely for clarity. to be succinct, in this case, you’ll get the same result with:

ss "wscript.echo" *.* | ft f*, l* -auto

Comments

Popular posts from this blog

using preloadpkgonsite.exe to stage compressed copies to child site distribution points

UPDATE: john marcum sent me a kind email to let me know about a problem he ran into with preloadpkgonsite.exe in the new SCCM Toolkit V2 where under certain conditions, packages will not uncompress.  if you are using the v2 toolkit, PLEASE read this blog post before proceeding.   here’s a scenario that came up on the mssms@lists.myitforum.com mailing list. when confronted with a situation of large packages and wan links, it’s generally best to get the data to the other location without going over the wire. in this case, 75gb. :/ the “how” you get the files there is really not the most important thing to worry about. once they’re there and moved to the appropriate location, preloadpkgonsite.exe is required to install the compressed source files. once done, a status message goes back to the parent server which should stop the upstream server from copying the package source files over the wan to the child site. anyway, if it’s a relatively small amount of packages, you can

How to Identify Applications Using Your Domain Controller

Problem Everyone has been through it. We've all had to retire or replace a domain controller at some point in our checkered collective experiences. While AD provides very intelligent high availability, some applications are just plain dumb. They do not observe site awareness or participate in locating a domain controller. All they want is the name or IP of one domain controller which gets hardcoded in a configuration file somewhere, deeply embedded in some file folder or setting that you are never going to find. How do you look at a DC and decide which applications might be doing it? Packet trace? Logs? Shut it down and wait for screaming? It seems very tedious and nearly impossible. Potential Solution Obviously I wouldn't even bother posting this if I hadn't run across something interesting. :) I ran across something in draftcalled Domain Controller Isolation. Since it's in draft, I don't know that it's published yet. HOWEVER, the concept is based off

sccm: content hash fails to match

back in 2008, I wrote up a little thing about how distribution manager fails to send a package to a distribution point . even though a lot of what I wrote that for was the failure of packages to get delivered to child sites, the result was pretty much the same. when the client tries to run the advertisement with an old package, the result was a failure because of content mismatch. I went through an ordeal recently capturing these exact kinds of failures and corrected quite a number of problems with these packages. the resulting blog post is my effort to capture how these problems were resolved. if nothing else, it's a basic checklist of things you can use.   DETECTION status messages take a look at your status messages. this has to be the easiest way to determine where these problems exist. unfortunately, it requires that a client is already experiencing problems. there are client logs you can examine as well such as cas, but I wasn't even sure I was going to have enough m