Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If Powershell is built on top of .Net, what is the reason to write scripts instead of C# code? [closed]

As a person who is new to Powershell I am not 100% clear at one point:

If

"In PowerShell, administrative tasks are generally performed by cmdlets (pronounced command-lets), specialized .NET classes implementing a particular operation."

What are the reasons to write scripts instead of writing .Net executables with those classes and compile them as console applications?

like image 985
pencilCake Avatar asked Dec 03 '12 22:12

pencilCake


People also ask

Is PowerShell written in C#?

Although PowerShell Cmdlets are usually written in PowerShell, there are occasions when the level of integration with existing C# or VB libraries is awkward to achieve with PowerShell.

What is the difference between PowerShell and PowerShell scripting?

The principal difference between the two is convenience. PowerShell is a simpler and more straightforward scripting and execution environment, while the ISE provides more flexible and forgiving editing and execution features. PowerShell can be a good platform for simple tasks where actions are clear.

Is PowerShell based on .NET framework?

Unless you have jumped straight into this chapter, you already know that Windows PowerShell is founded on the . NET Framework and that cmdlets and the objects passed along a Windows PowerShell pipeline are .

Why is PowerShell an ideal platform for script development?

PowerShell Is Powerful and Versatile NET, which means it can call . NET methods natively to accomplish tasks — even if the built-in PowerShell cmdlet doesn't support it. On top of that, PowerShell can also run anything that you can already run from the command prompt.


2 Answers

You could just as easily ask the question, what are the reasons to write applications in .NET when you can just as easily create fully fledged desktop and server software in PowerShell?

There are reasons to use both in different circumstances. PoSH scripts themselves are simply small text files, and are very easy to write and maintain. As someone who dabbles in both, I'd say in a lot of circumstances system maintenance tasks are much easier to accomplish with a quickly whipped up PoSH script than with a .NET application.

That said there are some things which are much easier to accomplish in .NET (I use C# myself), but the process of developing these is much more cumbersome, and requires an additional skillset not common among SysAdmins.

Put simply, PoSH is targeted at SysAdmins and .NET is targeted at developers, but that's not to say there isn't some room for crossover.

like image 73
Matt G Avatar answered Nov 01 '22 16:11

Matt G


I full agree with @Matt G answer.

I just want to add one thing which is arround culture. Windows SysAdmins culture is more "click" driven (admin by the Graphic User Interface) than programming driven.

I use to work in Unix world, there, SysAdmins where power scripters (sh, csh, ksh ...) and when needed they where able to write a small 'C' program on the corner of the table (for perfomance, or to act as a complex file filter etc).

Since the begining of .NET (2001) Windows SysAdmins can use the C# compiler (available from the .NET directories as CSC.EXE) as Linux or Unix were able to use 'cc' compiler, but they don't. The best ones were using WSH (Window Script Shell), but VBScript was not so easy to use and need to discover different COM objects for each case.

Microsoft, multiply the ways to urge Windows SysAdmins to use command lines and scripting (WSH, netsh, wmic etc.). For a few years now (2006) Microsoft decided to take the Sysadmins by the hand, and gave them a Power tool, removing the power from the user interface (Exchange 2007). So now SysAdmin can't do anything else than use PowerShell. But as they are not devolppers, the language is interpreted and the 'object culture' is somehow hidden for the beginers.

Now good Windows SysAdmins can develop their own C# class and insert them inline into their PowerShell scripts, SysAdmins are not developpers, they just need to automate their process.

like image 23
JPBlanc Avatar answered Nov 01 '22 16:11

JPBlanc