You are invited to Log in or Register a free Frihost Account!

Powershell rant 1

Little over 2 years ago I started out with PowerShell and in the beginning I didn't like it at all!!!

But now I've learned to appreciate it a lot more because when you need to perform a lot of tasks on windows machines or even specific server components it does have it's perks.

Every mayor component we use at the office ( Active Directory, Exchange ( on-prem and o365), Configuration Manager and what not ) has a lot of CmdLets that are extremely useful when you need to automate tasks.

One time I wrote a script that migrated an entire SCCM 2007 site over to an SCCM 2012 primary site.
this functionality was not built into the 2007 version so I had to interact with it's WMI interface and used the SCCM 2012 CmdLets to import the packages, collections and deployments.

Having a C# background helped me a lot in the early stages and by now I'm even compiling my own CmdLets.

If you look at it's essence it's a combination of bash, all the way from variables, the pipeline and dot sourcing, and the .NET framework ( it's actually built on it....). the latter is actually the part that makes it so powerful, it allows you to interact directly with the .net framework and all it's classes.
moreover it allows you to implement your own .Net code

There's a saying in dutch ( possibly in other languages as well) which goes as 'beter goed gestolen dan slecht bedacht' in other words it's always better to steal something good then come up with something bad. which microsoft is doing a lot more then I would like.

Very simple example:
$TestSource = @"
public class SomeClass
    public static string Test(string test){
        return System.String.Format("you entered: {0}", test);

Add-Type -TypeDefinition $TestSource
[SomeClass]::Test("This is a test")

The same technique can also be used to interact with the Windows API, see :

Rant starts here
Enter the Powershell HELL: Most admins may be good admins but are poor coders and I see this all the time.
Powershell is as loosely typed as it can get but it's being abused all over the place.

back in the VBScript days I would shrug at most code that was written by my co-workers and when I asked why they didn't use strict mode or why they didn't declare their variables they would say 'hey, the code works', sure it does, in a perfect world it would. but as soon as an unexpected value was presented to their 'script' it would hit then fan and I would be the guy to straighten things out again.

Not to brag or anything but I put a lot of pride and effort into my code and possibly overdoing it a little, taking a little more time then others to get the code in production stage but in the end I've never had any issues with my production code.

for instance, I declare ALL my variables with it's type, I don't care if Powershell can juggle types around and figures out that 'this' + 3 is a string. heck I don't even want to have it make that assumption. another reason why I declare my variables with their type is that ISE will list all members of the object whenever I use it. the only time I will not properly declare my variables is when the type of the returned object is waaaay too long, a good example would be an MSFT_Disk CIM Instance object:

PS C:\Users\marco> get-disk | select -First 1 | gm
   TypeName: Microsoft.Management.Infrastructure.CimInstance#ROOT/Microsoft/Windows/Storage/MSFT_Disk

function declarations really get me going, a lot of times I will see something like this:

function test($name, $value){

    Write-host "your name is $name and the value is $value"


test "marco" "bla"

sure, this will work but it's a serious pain in the a**.
- both $name and $value can have any types assigned to it.
- Parameters are not named
- and formatting is left for powershell to figure out.

this example is an extremely simple example but imagine a source file with 700+ lines of code with more then a dozen of functions like this. it becomes an ugly mess that needs to be turned inside out every time you need to update the code or add functionality to it.

If I were to write the same code it would look like this;


function Test-Something
        [Parameter(Mandatory = $true, HelpMessage = 'Enter the name to process',  Position = 0)]
        [string] $Name,

        [Parameter(Mandatory = $true, HelpMessage = 'Enter the value to process', Position = 1)]
        [string] $Value

    # every time you Write-Host... a puppy dies
    Write-Verbose -Message ( 'Processing name: "{0}" with value: "{1}".' -f $Name, $Value)

    # Do stuff with $Name and $Value here...


Test-Something -Name 'Marco' -Value 35 -Verbose

Yes it's a lot more code, yes it does exactly the same thing but the next guy that picks up the code doesn't have to figure out what the hell is going on.

Sorry that this post turned into a rant, I got carried away Laughing

2 blog comments below

Still a great posting, though Smile
Nice combination of describing Powershell and a rant. Which is not only a rant, but a constructive proposal how to improve code in Powershell.
I hate & love it. Microsoft's attempt to clone bash to me looks a bit funny. Powershell code doesn't look nice. But it IS powerfull !
amagard on Wed Aug 24, 2016 10:41 am
Obviously Microsoft wants to get more people into powershell and I get that. I really do.
Most ( if not all ) of their enterprise software had a bunch of powershell CmdLets to interact with the software. and with good reason.

The issue is that there are a lot of people using ( mostly oneliners ) powershell to manage their environement ( O365, Azure, Configuration Manager, etc... ) that dont know ( and don't care ) about programming and the only thing that matters to them is getting the job done.

don't get me wrong, in the end this is all that matters, but from time to time they need to run a migration or automate another project that requires more coding then a few one liners in the terminal and instead of passing the change order to me they will try i themselves Rolling Eyes

In 90% of the cases they dragged me into a confcall or asked me to pass by their desk for a 'simple' powershell question. Very Happy

I've even tried to teach most of these kids a thing or 2 about Powershell and a few have actually picked up the language very well, so there's light at the end of the tunnel.

I guess I need to keep reminding myself that powershell is not a programming language but a shell that supports scripting
Marcuzzo on Fri Jun 02, 2017 10:17 pm

© 2005-2011 Frihost, forums powered by phpBB.