powershell - Execute CMD command and Stream CMD results to StreamWriter -


i trying execute cmd command , stream results of commands stream writer. reason log files trying sift through 1gb each , storing results variable can use several gigs of memory. pipe out streamwriter because of beautiful performance.

here excerpt script.

$streamwriter = [system.io.streamwriter] "test.txt" $searchstring = "2016_06_*" $searchlogs = "2016_*.log"  "cmd /c gunzip.exe -c $searchlogs |grep -i `"$searchstring`"" | $streamwriter.writeline($_)  $streamwriter.flush() $streamwriter.close() 

  1. do not mix cmd , powershell. use 1 or other. told last time.
  2. do not use powershell v1 anymore. still supported windows system can run @ least powershell v2. upgrade now.
  3. you don't need cmd running executables powershell. had told well.
  4. if put commandline in quotes powershell echo it, not execute it.
  5. for filtering wildcard matches use -like operator.
  6. stream writers don't read pipeline. need wrap them in foreach-object statement.
$streamwriter = [io.streamwriter]'test.txt' $searchstring = "2016_06_*" $searchlogs   = "2016_*.log"  gunzip.exe -c $searchlogs |   where-object { $_ -like $searchstring } |   foreach-object { $streamwriter.writeline($_) }  $streamwriter.flush() $streamwriter.close() 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -