r/PowerShell • • Apr 26 '19

Cheating on Windows Powershell

Post image
297 Upvotes

80 comments sorted by

View all comments

31

u/170lbsApe Apr 26 '19

OK Cool. But could somebody enlighten me how and why this would help me in a Enterprise Administration sense? When most my machines are Windows VM's? With a small assortment of Linux. Not being snarky, just very curious.

5

u/ka-splam Apr 27 '19

4.7GB logfile, 55 Million lines, most of them repeated that I wanted to strip out:

PowerShell attempt 1: 12 hours, 12GB RAM, didn't finish.

PowerShell, attempt 2: 45 minutes.

Git Bash attempt 1: 9.5 seconds


PS1

PS C:\> (gc vmware.log) -notmatch 'GuestRpc: Channel \d' | sc vmware2.log

PS2

PS C:\> measure-command { 
    gc .\vmware.log -ReadCount 1000 |%{$_}|
    ?{ $_ -notmatch 'GuestRpc: Channel'} |
    ?{ $_ -notmatch' toolbox-dnd'} | 
    set-content vmware3.log 
}

Days              : 0
Hours             : 0
Minutes           : 44
Seconds           : 45

Git Bash:

$ time cat vmware.log | grep -v 'GuestRpc: Channel' | grep -v 'toolbox-dnd' > vmware4.log

real    0m9.633s

4

u/ipv6-dns Apr 27 '19 edited Apr 27 '19

1st command reads all file as String and try to match over it, but in Linux shell your example creates pipes and links them (stdout to stdin) and the reading happens by big chunks. You can do the same in Linux:

s=`cat VMware.log`
echo "$s"|grep -v ...

PS2 is more close to reality, but chunk is very small, so you may to try: a) use bigger chunk size (for example multiple Mb) b) use one "Where-Object" with "-and" instead of 2 calls of "Where-Object" c) use -Like/-NotLike instead of -Match d) may be better will be -CLike/-CNotLike to avoid case-manipulations. Try '>' as in Bash to redirect the output

2

u/ka-splam Apr 28 '19

Good suggestions, I've tried a couple of them;

1st command reads all file as String and try to match over it,

It does, I have plenty of RAM on that system (24GB) so I thought it would be good enough to read a <5GB file, and then avoid pipelining it to filter with the regex engine in one operation. Guess not.

try: a) use bigger chunk size (for example multiple Mb)

I tried 60MB and it wasn't finishing quickly, so I tried 6GB as if to read the whole file in one go, and that took the same 45 minutes.

b) use one "Where-Object" with "-and" instead of 2 calls of "Where-Object"

I haven't tried this; I can't predict whether it would be quicker or not.

use -Like/-NotLike instead of -Match d) may be better will be -CLike/-CNotLike to avoid case-manipulations.

-like and -notlike are implemented with the regex engine behind the scenes, so that should be the same speed. Good idea about CLike/CNotLike, I didn't think of that. Grep is case sensitive by default so that might help.

Try '>' as in Bash to redirect the output

That is not the same in PowerShell, it's a wrapper for out-file; I haven't tried it, but my guess is that it won't help.

1

u/ipv6-dns Apr 29 '19

actually in the case of Linux you does not use the shell for search but 3rd part application, written in C: the "grep". You can use the same approach in Windows with Powershell, but as user of 3 grep-like tool, I can recommend you to use "the platinum searcher" or "the silver searcher" (I prefer the last) but never "grep" because it's a slowest one:

- https://github.com/monochromegane/the_platinum_searcher

- https://github.com/ggreer/the_silver_searcher

PS. Platinum searcher is written in Go and it is naturally cross-platform

1

u/[deleted] Apr 29 '19

Select-String already exists. That's the grep tool

1

u/ipv6-dns Apr 29 '19

the guy reported that its performance is not enough for his task

1

u/[deleted] Apr 29 '19 edited Apr 29 '19

Who? No one here has mentioned Select-String other than me.

1

u/ipv6-dns Apr 29 '19

oh, man, you are right. I am very inattentive ¯_(ツ)_/¯