Words: 400
Time to read: ~ 2 minutes
Continue reading “Getting Details from a Maintenance Plan using PowerShell”
You must know your enemy before you can replace your enemy…
Words: 400
Time to read: ~ 2 minutes
Continue reading “Getting Details from a Maintenance Plan using PowerShell”
Trying to be smart getting me learned!
Words: 432
Time to read: ~ 3 minutes
…since I’ve written a blog post and there’s no better way to get back into something than to just start doing. Even if it’s just a throwaway, little post.
So here’s mine. Hope you enjoy it.
… but recently I was trying in PowerShell to split a string up into its individual characters. So, as an example, ‘SQL Server’ would become the ('S', 'Q', 'L', ' ', 'S', 'e', 'r', 'v', 'e', 'r')
collection of characters.
I also wanted the character before it and after it as well…
This Char | Prev Char | Next Char |
S | r | Q |
Q | S | L |
L | Q | |
L | S | |
S | e | |
e | S | r |
r | e | v |
v | r | e |
e | v | r |
r | e | S |
…that I found was to simply ask for the character a certain position (or index) in the string.
$String = 'SQL Server' | |
for ($i = 0; $i -lt $String.Length; $i++ ) { | |
[PSCustomObject]@{ | |
'This Char' = $string[$i] | |
'Prev Char' = $string[$i -1] | |
'Next Char' = $string[($i +1) % $String.Length] | |
} | |
} |
As you can see we had to add a little got’cha to our code: ($i +1) % $String.Length
This is because, without the modulo operator (what remains when we divide the numbers), PowerShell looks for the next index (10) and returns nothing since there is essentially nothing in index 10.
So we ask PowerShell what 10 modulo the length of the string is ( 10 % 10) and the remainder is 0. This way we can wrap back around to the start again!
…this may seem like a pretty simple exercise, especially since we know that PowerShell has a Substring method.
$String = 'SQL Server' | |
0..($String.Length – 1) | ForEach-Object –Process { | |
$String.Substring($_, 1) | |
} |
However, what happens when we try and go backwards i.e. $String.Substring(-1, 1)
?
Nope!
Exception calling “Substring” with “2” argument(s): “StartIndex cannot be less than zero.
…I couldn’t get any way that used “.whatever()” to work. Substring; nope, Chars; nada.
The moment that I passed in a -1 I just saw a sea of red.
Thankfully I’ve been frequenting the PowerShell slack channel lately and they were able to let me know why.
sifb [Nov 12th at 10:01 PM]
@Shane O’Neill I think the $array[-1] loop-around is a powershell convenience, and doesn’t exist in C# / the lower level .Net libraries
…even though we may be used to SubString, it doesn’t mean that it is the best way for us to go. This is technically a new language and there are going to be tips and tricks for doing things that we don’t yet know about!
Half the fun is rooting them out, finding them, and slowly, slowly watching your code improve and knowing why.
I would have written a shorter [post], but I did not have the time.
If it walks like a ‘duck’, talks like a ‘duck’, and looks like a ‘duck’…careful! It could be a N’duck’
Words: 270
Time to read: around 2 minutes.
Continue reading “Another Difference between N” and ” in SQL Server.”
Words: 884
Time to read: ~ 4 minutes
Update: 29-Aug-2018 – Thanks to the good folks in reddit (u/TheIncorrigible1 & u/Ta11ow) who pointed out that Pause is a function, not an alias & a really cool addition to the confirm attribute. Also added the second step I left out of the 1st draft (oops).
All in all it’s just another…
Words: 579
Time to read: ~ 3 minutes
Words: 857
Time to read: ~ 4 minutes
Next step is wondering if we can avoid SQL/PowerShell injection…
Words: 569
Time to read: ~ 3 minutes
Continue reading “Figuring out a PowerShell version of Dynamic SQL”
I was so disappointed we don’t have a webinar on May the 4th 🙁
Words: 1075
Time to read: ~ 5 minutes
I learned a thing myself at the end!
Words: 532
Time to read: ~ 2.5 minutes
Update: 20180417 – Added explanation of SQL Script rather than random statements.
Continue reading “When you NEED to Declare a Table Using the OUTPUT Clause”
Words: 584
Time to read: ~ 3 minutes
Continue reading “T-SQL Tuesday #100 – Looking Forward 100 Months”