3

I was trying to write a function that to look for pool tags in .sys files. I created an array of all the directories that had .sys files then looped through them using the sysinternals Strings utility.

This is the array:

$paths = Get-ChildItem \\$server\c$ *.sys -Recurse -ErrorAction SilentlyContinue | 
     Select-Object Directory -unique

This was my first attempt at a loop:

foreach ($path in $paths) {

    #convert object IO fileobject to string and strip out extraneous characters
    [string]$path1 = $path
    $path2 = $path1.replace("@{Directory=","")
    $path3 = $path2.replace("}","")
    $path4 = "$path3\*.sys"
    Invoke-Command -ScriptBlock {strings -s $path4 | findstr $string}   
}

I found some references to the error indicating that in foreach loops, all of the information is stored in memory until it completes its processing.

So I tried this:

for ($i = 0; $i -lt $paths.count; $i++){
    [string]$path1 = $paths[$i]
    $path2 = $path1.replace("@{Directory=","")
    $path3 = $path2.replace("}","")
    $path4 = "$path3\*.sys"
    Invoke-Command -ScriptBlock {strings -s $path4 | findstr $string}       
}

But it had the same result. I've read that sending an item at a time across the pipeline will prevent this error/issue, but I'm at a loss on how to proceed. Any thoughts?

1 Answer 1

1

Yeah, it is usually better to approach this problem using streaming so you don't have to buffer up a bunch of objects e.g.:

Get-ChildItem \\server\c$ -r *.sys -ea 0 | Foreach {
    "Processing $_"; strings $_.Fullname | findstr $string}

Also, I'm not sure why you're using Invoke-Command when you can invoke strings and findstr directly. You typically use Invoke-Command to run a command on a remote computer.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. That worked beautifully. As for the invoke-command, I think it was a matter of trying too many things at once. It worked that way and so I left it as is, but yeah, unnecessary. Thanks again...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.