As a DBA, we’re often forced to use PowerShell scripts to automate database management.
A script language on Windows is a great idea. Use the .Net framework is a great idea. But why Powershell makes me want to do this each time I try to debug it ?
Before starting to explain why I think PowerShell is a stupid language, I’d like to thank Remi Larger, my teammate, who provided almost exemples.
Guess my type
Let’s start with a function which return an Array :
function GetABeautifulArray() { $mybeautifularray = @("FirstElement", "SecondElement") return $mybeautifularray } $array = GetABeautifulArray $array.GetType()
Yes, I get an Array
I’m updating the function to add in the array only 1 element :
function GetABeautifulArray() { $mybeautifularray = @("FirstElement") return $mybeautifularray } $array = GetABeautifulArray $array.GetType()
Now guess my type :
If your collection contains only one element, the function will return the element itself instead of the collection…
And you know what ? If you want to get a collection, you can use this obvious syntax by adding a comma :
return , $mybeautifularray
Don’t type your variables
When you learned development, you’ve got used to declare your variables in your code ? Muhahahaha, you’re dead 🙂
$test1 = $null $test1 -eq $null
Hopefully, this code returns true. What about this one ?
[string]$test2 = $null $test2 -eq $null
Yes you’re right, I’d better test my strings like that :
[string]::IsNullOrEmpty($test1) [string]::IsNullOrEmpty($test2)
But what about other object ?
[int]$test3 = $null $test3 -eq $null
I have lot of exemples to explain why I don’t like Powershell and I’m always surprised about behavior differences between C# and PowerShell even if they both use .net Framework…
Almost Powershell scripts I encountered in different companies aren’t unit tested, the code isn’t build, do you really want to manage your production with this kind of tool ?
Great post, this is completely true !
And you’re right, Remy is a legend.