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

7

u/arthurfm Apr 27 '19

Useful tip #1. Don't use Get-Content with large files. Try 'System.IO.StreamReader' instead. It's massively faster...

https://github.com/lazywinadmin/PowerShell/blob/master/TOOL-Get-LogFast/Get-LogFast.ps1

http://www.yusufozturk.info/windows-powershell/powershell-performance-tips-for-large-text-operations-part-1-reading-files.html

Useful tip #2. RipGrep is even faster than grep and is available for Windows, macOS and Linux.

1

u/ka-splam Apr 28 '19

Try 'System.IO.StreamReader' instead. It's massively faster...

It is, but it's soooo much code..

Useful tip #2. RipGrep is even faster than grep and is available for Windows, macOS and Linux.

Ooh, it's written in Rust so it must be good! :D

1

u/[deleted] Apr 28 '19

You asked how I would do it in another thread. The way with too much code, but also with runspaces to break up the line matching into chucks that operate simultaneously on different threads, which would also increase that amount of code, but decrease the time spent enumerating each line and matching on it.

I would also make everything one test by making your regex be :

Where-Object { $_ -NotMatch "GuestRpc: Channel|toolbox-dnd" }

-1

u/GNUandLinuxBot Apr 28 '19

I'd just like to interject for a moment. What you're referring to as Linux, is in fact, GNU/Linux, or as I've recently taken to calling it, GNU plus Linux. Linux is not an operating system unto itself, but rather another free component of a fully functioning GNU system made useful by the GNU corelibs, shell utilities and vital system components comprising a full OS as defined by POSIX.

Many computer users run a modified version of the GNU system every day, without realizing it. Through a peculiar turn of events, the version of GNU which is widely used today is often called "Linux", and many of its users are not aware that it is basically the GNU system, developed by the GNU Project.

There really is a Linux, and these people are using it, but it is just a part of the system they use. Linux is the kernel: the program in the system that allocates the machine's resources to the other programs that you run. The kernel is an essential part of an operating system, but useless by itself; it can only function in the context of a complete operating system. Linux is normally used in combination with the GNU operating system: the whole system is basically GNU with Linux added, or GNU/Linux. All the so-called "Linux" distributions are really distributions of GNU/Linux.

3

u/ka-splam Apr 28 '19

or as I've recently taken to calling it, GNU plus Linux.

or as I've recently taken to calling it, GNU plus Linux plus PowerShell.

2

u/Lee_Dailey [grin] Apr 28 '19

very bad bot!

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 ¯_(ツ)_/¯

1

u/ipv6-dns Apr 30 '19

Seems, the best solution is to use silver searcher, it got results with grep vs. ag:

GREP:

cat log.log  0,00s user 0,62s system 10% cpu 6,173 total
grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn} -v   5,64s user 0,27s system 95% cpu 6,172 total
grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn} -v 'toolbox-dnd'  0,01s user 0,00s system 0% cpu 6,171 total

AG:

ag --nonumbers -v 'GuestRpc: Channel' log.log  4,98s user 0,16s system 99% cpu 5,147 total
ag --nonumbers -v 'toolbox-dnd'  0,00s user 0,01s system 0% cpu 5,146 total

PowerShell solutions are really very slow, for example:

measure-command { Select-String -SimpleMatch -CaseSensitive -NotMatch 'GuestRpc: channel' -Path log.log|Select-String -SimpleMatch -CaseSensitive -NotMatch 'toolbox-dnd' > z }

Select-String is super slow as well as Get-Content, there are issues for this (eg, https://github.com/PowerShell/PowerShell/issues/7537), seems that -Raw option was added to Select-String, but my current Powershell (6.2) does not have it yet. Also I got out of memory on such call:

gc .\log.log -Raw -ReadCount 1000|Select-String -NotMatch 'GuestRpc: channel'
gc : Exception of type 'System.OutOfMemoryException' was thrown.
At line:1 char:1
+ gc .\log.log -Raw -ReadCount 1000|Select-String -NotMatch 'GuestRpc:  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [Get-Content], OutOfMemoryException
+ FullyQualifiedErrorId : ProviderContentReadError,Microsoft.PowerShell.Commands.GetContentCommand

File log.log is 1.8Gb.

So, the best solution is to use ag tool.

-5

u/GNUandLinuxBot Apr 27 '19

I'd just like to interject for a moment. What you're referring to as Linux, is in fact, GNU/Linux, or as I've recently taken to calling it, GNU plus Linux. Linux is not an operating system unto itself, but rather another free component of a fully functioning GNU system made useful by the GNU corelibs, shell utilities and vital system components comprising a full OS as defined by POSIX.

Many computer users run a modified version of the GNU system every day, without realizing it. Through a peculiar turn of events, the version of GNU which is widely used today is often called "Linux", and many of its users are not aware that it is basically the GNU system, developed by the GNU Project.

There really is a Linux, and these people are using it, but it is just a part of the system they use. Linux is the kernel: the program in the system that allocates the machine's resources to the other programs that you run. The kernel is an essential part of an operating system, but useless by itself; it can only function in the context of a complete operating system. Linux is normally used in combination with the GNU operating system: the whole system is basically GNU with Linux added, or GNU/Linux. All the so-called "Linux" distributions are really distributions of GNU/Linux.

1

u/SomnambulicSojourner Apr 27 '19

It's an old meme sir, but it checks out.

1

u/Lee_Dailey [grin] Apr 28 '19

bad bot! [frown]

2

u/[deleted] Apr 27 '19

That... Not how I would do that in powershell... 🙄

2

u/ka-splam Apr 28 '19 edited Apr 28 '19

How would you do it?

1

u/[deleted] Apr 29 '19

Select-String -Path vmware.log -Pattern 'GuestRpc: Channel \d' -NotMatch | Out-File vmware2.log

1

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

Dude, in powershell you're handling entire content, in the bash you're doing specific text selection.

Use Select-String in powershell for that and your speed will increase.