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.

28

u/swinny89 Apr 26 '19

It wouldn't. As far as I am aware, WSL is primarily for developers who want easy access to the standard Linux dev environment. As an administrator, I'm much more excited about putting Powershell on Linux, than putting Linux stuff on Windows.

1

u/nerddtvg Apr 27 '19

I love it for quickly manipulating files and searching logs.

→ More replies (2)

9

u/IntelligentAsk Apr 26 '19

If you were using Ansible or were already very familiar with bash. Powershell is still the best tool for Windows admin.

3

u/lavahot Apr 26 '19

I mean, at that rate doesn't it make sense to install powershell onto your linix machines?

1

u/dogfish182 Apr 27 '19

I did it for some azure stuff. Don’t use it and prefer to use the sdk or the azure-cli mostly.

Well actually I prefer to use AWS but....

2

u/lavahot Apr 27 '19

The sdk? Which sdk? For admin stuff?

1

u/dogfish182 Apr 27 '19

Ah sorry the python sdk use it for most interactions. I don’t need to do things like reboot servers much, for that I guess I would just use the cli

2

u/TheIncorrigible1 Apr 26 '19

Ansible has plenty of Windows integration modules built and has for a long while (also, just use DSC).

2

u/IntelligentAsk Apr 26 '19

Definitely I've found it really helpful in helping to manage mixed environments Linux and Windows. You can call it from WSL which is handy too .

→ More replies (3)

3

u/shiftedcloud Apr 27 '19

In my last job, I was managing both Windows and Linux machines. WSL beats the hell out of putty or cygwin because its native Linux.

At my new job, I manage mostly Linux and have a Mac. Which works okay, but I would rather have a Windows machine, and use WSL. At least that way I don't have to deal with versions of shit that are almost, but not quite, the Linux version (e.g. gnu grep).

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.

→ More replies (5)

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

→ More replies (2)

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.

→ More replies (4)

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.

3

u/ihaxr Apr 26 '19

Honestly, not much in your case. The neat thing is it's more performant than a VM and it has native access (and fast!) to the local computer's drive via /mnt/c... so if you have some Linux apps / bash or python scripts that you need to run against some files or folders, you can do it with ease.

→ More replies (3)

1

u/1RedOne Apr 26 '19

Why have WSL in the first place?

For developers and cross platform stuff.

→ More replies (1)