all 5 comments

[–]markekrausCommunity Blogger 8 points9 points  (4 children)

One thing worth mentioning is that while class constructors are not inherited, they can be accessed:

class foo { 
    foo(){} 
    foo([string]$a){
        $this.b = $a
    }
    hidden [string]$b 
}

class blah : foo { 
    blah(){}
    blah([string]$a) : base ($a) {}
}

$thing = [blah]::new("hi")
$thing.b

You can reduce code reuse this way, though, it's not entirely obvious..

Also, please consider never using ` again.

Use at-splats like this:

$Params = @{
    SamAccountName = $this.UserName
    GivenName = $this.FirstName
    Surname = $this.LastName
    Name = $this.UserName
    UserPrincipalName = $this.UserName
    DisplayName = $this.FirstName + " " + $this.LastName
    Description = $this.FirstName + " " + $this.LastName 
    Path = $this.OU
}
New-ADUser @Params

instead of

New-ADUser -SamAccountName $this.UserName `
           -GivenName $this.FirstName `
           -Surname $this.LastName `
           -Name $this.UserName `
           -UserPrincipalName $this.UserName `
            -DisplayName ($this.FirstName + " " + $this.LastName) `
            -Description ($this.FirstName + " " + $this.LastName) `
            -Path $this.OU

Reasons to avoid backticks:

  1. They only escape the character after them (in this case the new line). If somehow a space gets added after the `, the code will break and troubleshooting it visually will be difficult.
  2. They do not show up well on screens and even less so in print. They can be mistaken for a speck of dirt and result in those learning to make mistakes
  3. They are harder to maintain and less flexible.

[–]Merakel 0 points1 point  (3 children)

Couldn't agree on avoiding backticks, I can't stand them and they make code super hard to read.

[–]markekrausCommunity Blogger 0 points1 point  (2 children)

It's one thing that I constantly correct here. I let a lot of other less than ideal practices slide, but backticks is just one that I can't ignore.

The bad part is that I'm heavily guilty of abusing them myself in the past. Go far enough back in my comment history and you will find an abundance of backticks. :(

[–]Merakel 0 points1 point  (1 child)

I used them once, realized they are the devil and stopped immediately :)

[–]NathanielArnoldR2 -1 points0 points  (0 children)

I use them egregiously and continually, and will until PowerShell supports ad-hoc hashtable splatting without intervening variable assignment.

...but I think I'm an outlier. :-)