|
Wordle 378 4/6
⬛🟨⬛⬛🟨
🟨🟨⬛⬛⬛
⬛⬛⬛⬛🟨
🟩🟩🟩🟩🟩
|
|
|
|
|
still can't get wordle to post my results to here. cut and paste did not work either.
"A little time, a little trouble, your better day"
Badfinger
|
|
|
|
|
I am starting to learn PowerShell. I have avoided it for years, because whatever I needed to do beyond a simple BAT file, I could do in C#.
However, I am in a situation where I have to use PowerShell, so I am learning it.
One of the things I learned is that within a PowerShell script, you can call .cs files to run C# code. So this question came to mind, and I thought you all might have some opinions. I wrote a PS script that calls a .cs file and does exactly what I described. I have not mastered calling non-GAC .NET library DLLs yet, but I am working on it.
Why not use PowerShell as a management script, and write C# code to execute the "business logic"? I am not convinced that it is better to use PowerShell cmdlets to hack up some brittle script to do what can be more easily done in C#, which is far more capable and wider-ranging than PowerShell script.
This is a snippet from my script:
$Source = Get-Content "SomeCSClass.cs" -Raw
Add-Type -TypeDefinition $Source
$TestDLL = New-Object PSTest.SomeCSClass
$TestFilePath = $TestDLL.GetCurrentDirectory() + "\PowerShell Example Files\"
## Get a list of files using C# code from a static method
$Files = [PSTest.SomeCSClass]::GetListOfFiles($TestFilePath, "*.txt", 0)
Your thoughts?
|
|
|
|
|
I also avoid PowerShell, it's of no use to me either.
|
|
|
|
|
This is very welcome news. I might just want to learn PowerShell after all just for this capability.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Powershell+C#=cryptic^2
sorry no interest
"A little time, a little trouble, your better day"
Badfinger
|
|
|
|
|
Cryptic only if you are not familiar with C#. For someone who is used to the 40+ year old approach of scripting shells, they might find it easier to stay with scripting.
And if you don’t have to use scripting shells - don’t. It is a backwards way to get work done. But, in my case, I have no choice. Trying to convince a DevOps team they do not need scripting is a lost cause.
|
|
|
|
|
I tried PowerShell a couple of times, but I agree that for a C# programmer the syntax is really awful.
modified 11-Nov-22 12:51pm.
|
|
|
|
|
Years ago I wrote a C# DLL that allows my programs to take in raw C# cs files and run them as scripts.
Why PowerShell was created without the ability to natively use C# for scripting instead of that inane verb-noun syntax with the cryptic parameters is beyond me.
|
|
|
|
|
If all your business logic is C#, and you just read it in PS, you can't step through that code to debug it. Of course if that's already been thoroughly tested and you're dropping it in as-is, then it's not much of an issue.
PS can create/use the same .NET objects you're familiar with natively. Yes, the syntax can be non-intuitive at first, but once you get the hang on it, the benefit of being able to bang out a short script to automate some menial task beats having to fire up VS and build/compile an EXE every time you want to make a tiny change.
|
|
|
|
|
PS syntax is not intuitive. It is awful to work with.
What I do is create a C# library DLL project, and write unit tests for the class(es). Then I include my ps1 file, which I can run from within VS by using the PS ISE, which loads the cs file.
That gives me good source control, good testing, and minimal “script kiddie” coding.
C# is far more versatile and elegant with which to code. Since our team standardized on PS long before I got there (DevOps stuff), it then is compatible with what others are doing by staying within the PS world.
|
|
|
|
|
MSBassSinger wrote: C# is far more versatile and elegant with which to code
Well obviously. PS and C# serve completely different purposes.
|
|
|
|
|
You could also call .NET from PS. That way, you don't mix 2 languages. .NET methods don't look perfectly not-out-of-place in PS but the result will still be way more maintainable than calling C# files from PS.
|
|
|
|
|
I find it easily maintainable.
|
|
|
|
|
Even though PowerShell has a not so great syntax, it's a god-tier shell.
You can call bash from PS whenever you need UNIX tools.
You can call .NET from PS whenever you need to configure anything system wise. Anything.
You can call the Windows Store when you need to install anything vaguely supported by Microsoft.
You can use those god-awful Node-esque scripts when you need to transpile/polyfill something front-end related.
You can patch system level .dll's to enable advanced remoting features.
You can remote from anything into anything.
Only downside is the security. If you listen to Microsoft, they will tell you they have experts that understand and patch common vulnerabilities quickly.
If you actually use it for a couple of years, you'll quickly notice their understanding of security resembles covering their ears and yelling "lalala I can't hear you" very loudly.
|
|
|
|
|
is it possible for you to write up a post on this topic?
diligent hands rule....
|
|
|
|
|
Thanks for the suggestion. I’ll see if I can find the time while on vacation in a couple of weeks.
|
|
|
|
|
Yes.
I've been wanting to write something, might as well be this.
|
|
|
|
|
|
*shudder*
PS is for scripting and for automation. It to me was just another language that could enable me to do a few more things than a bat file did and it was going to be "more" supported by MS going forward.
C# is a programming language that you make things(objects) out of. you can call .net objects from PS and that is what PS is for. Keep C# where it belongs and PS where it belongs.
I really think too often we all(myself included) get stuck in our ways and avoid learning something new just because at fist glance we don't like it. PowerShell is just another technology. Once learned you will see some benefits. Granted C# is more powerful. But each has their purpose in the MS exosphere.
To err is human to really elephant it up you need a computer
|
|
|
|
|
PS is built on .NET and uses objects. Think of the approach I use the same principle as separating business logic from UI logic. I separate the business logic (much better done in C#) from the scripting logic (simpler work better done in PS).
|
|
|
|
|
IIRC, Powershell is written/implemented in C#.
|
|
|
|
|
Written in C#, uses .NET, but the syntax is nothing like C#.
IMHO, they would have been better off creating a C# library for static commands that are now verb-noun syntax and keeping the familiar C# syntax.
|
|
|
|
|
If you think about the C# to open a process and pass it arguments, the Powershell syntax is less verbose than that would be.
Part of it is they started kicking around together as kids and since neither was an adult it's a lord of the flies sort of deal. (Much respect to both, but not giving up the metaphor.)
C# only not terribly long ago (C# 3/4, respectively) added var/dynamic that would enable some of what powershell is doing with sticking command results into objects.
The typical problem space (system administration/networking/security?) also has all these concerns that you simply do not have with the majority of what C# code out there is doing.
I'm not saying I wouldn't love it if it PowerShell were more C#-y and less whatever it is.
But I do see challenges and reasons not to simply say "let's make it a C# console that sends input (script file or otherwise) straight to IL and onwards to a shell-specific runtime".
Nowadays? Even if the guy tasked with and enabled to undertake a massive re-write of PowerShell looked at the state of things and decided that approach was a really good way to go? Well you've got all this baggage that plane simply wouldn't carry. There are probably many many folks less C# dev and more really smart scripter-admins (or wanting nothing to do with writing any of it) who depend on what's there and if you pulled the rug in the name of "code sanity" they would be very unhappy.
Maybe it would be possible for the interpreter to detect "old-style" vs "new" and translate the old stuff on the fly...
|
|
|
|
|
I am not advocating to change it. It is too late. They missed the boat on making PS something good for non-developer DevOps folks and for C# developers.
But it would be great if they would simply streamline the way a .NET DLL, not in the GAC, is loaded for use.
My reason for this is be able to apply a separation of concerns - administrative (PS script) vs business rules (C#).
|
|
|
|