Skip to main content

Dumb Things You Need to Know to PowerShell

OK--so I'm not much of a blogger, but as I've started working with PowerShell to write a lot of scripts, I'd like to document some of the pains that it caused.
  • The script parser reads from top to bottom, and if it hasn't read a function before it's called, it won't find the function. Yeah, I know. Just like all of the quirks that I'll list here, I'm sure they had a good reason for making it work like that, but it was painful learning it. So you'll just need to put all of your functions at the top, and the part of the script that runs first is at the end of the file.
  • Passing multiple parameters to a function requires no parentheses, and no commas. Yeah, so when you call a function that has more than one parameter, don't use parentheses, and delimit the parameters with spaces.

    foo param1 param2 parm3

  • Update PowerShell before you start coding. PowerShell is not updated by Windows Update, so you'll want to get the latest before you start hacking away.
  • Passing arrays as parameters doesn't work unless you do it "right."

    [string[]]$recipients = "me@outlook.com", "me@yahoo.com"

    will of course create your array (and there are several other ways to remove the dermal tissue from that feline), and

    function foo([string[]] $TO)

    will create the function's array. Then within your function, just don't use quotes around $TO when you use it. See this.
  • If the contents of a variable need to contain a dollar sign, use the back tick (the single quote on the tilde key).

    $MyDollarSignContainingVariable = WeirdService`$Name

    So this will return

    WeirdService$Name

  • In the PowerShell ISE, you have be on a line that contains code to set a breakpoint. I feel kind of stupid because I can see that it makes sense, but sometimes you just think, "Oh, it's broken," and stop trying because you have another way of doing it (adding write-host messages) that you know works.
  • Functions return all output, not just what you specify after the return statement. So if you have the following lines in a function:

    $t = "whatever"
    $t -match "what"
    return $matches

    The function will actually return an array containing "True" and "what". Yeah, no comment. So within functions, you have to pipe things to out-null if you don't want them to return anything.

    $t = "whatever"
    $t -match "what" | out-null
    return $matches

    This will just return "what" as one might have hoped for in the first place.
  • There is no "pause" statement. Ah, it's one of the most used DOS batch statements, and for some odd reason it hasn't been included as a Powershell statement.

So there you have it--my PowerShell soul laid bare for all to see. Oh, and there is now an extension for Visual Studio for PowerShell, but I haven't done much with it yet. Use NuGet in VS to find it. Hoping it will be nicer than the ISE.

I'm sure that these little quirks, and many others, have caused many to ditch PowerShell and go back to a batch or VBScript. It takes a lot of time to work through all of these little quirks, and many just don't feel like they have the time. If you know of others, post a comment--it would be nice to have a good list of "gotchas" for PowerShell newbies as it would save a generation of scripters a lot of time.

Comments

Popular posts from this blog

Script to move SQL Server System Database Files

One issue that can occur when using VM snapshots to back up a server with large database volumes is that the server gets paused when the snapshots are consolidated. We like to refer to this a "stunning" because it sounds worse, and it can cause really bad things if you are using clusters and/or availability groups. If a cluster node gets stunned for more than a few seconds, the other node will think that it needs to fail over. Additionally, when the stun is over, the node that was stunned comes back to life right where it left off, and if it was the owner when it was stunned, all this has to be sorted out, and honestly, after working with Microsoft Cluster for twenty years, you just don't want to put it in this situation unless there's no way to avoid it. So, your backup strategy changes, and you set your large volumes as independent disks so that they are ignored by the VM snapshots. This lets you backup the OS and any other small volumes without a stun. Your

SQL Diagnostics Manager - Script to Disable Maintenance Mode on All Servers

We use Idera's SQL Diagnostics Manager and there is a defect where monitored servers are sometimes not taken out of maintenance mode as specified by the schedule. As a workaround, I wrote the following script that runs each morning to log which servers are in maintenance mode, and disables maintenance mode on all servers. The use of the -MMRecurring switch on Set-SQLdmMonitoredInstance disables maintenance mode and keeps the maintenance mode schedule enabled. add-pssnapin sqldmsnapin $LogFile = "D:\Jobs\DisableMaintenanceModeAll\MMEnabledLog.txt" # Log any servers that are currently in maintenance mode $MMEnabled = invoke-sqlcmd -serverinstance -Query "USE SQLdmRepository; SELECT InstanceName FROM MonitoredSQLServers WHERE MaintenanceModeEnabled = 1" if( $MMEnabled.ItemArray.Count -gt 0 ) { add-content -Value " " -path $LogFile add-content -Value "================================================" -path $LogFile add-content -Value &qu

Maine Event - Days Eight, Nine, and Ten

Sunday morning brought more sunshine, so we puttered around the house a bit and then made a beeline for Popham Beach. The day went without much excitement other than Leigh finding a sand dollar about the size of a fifty cent piece. Emily once again had a fantastic time and even worked on her swimsuit modelling skills. We also flew my parafoil kite, which requires a good bit of wind to fly, but is extremely stable once it's up. Since we knew the secret of the beach, there was no mystery to the phenomena of the growing beach, but it was still super-duper. We knew this time to take our shoes to the island so we could explore a bit, so we tramped around it for quite a while. That was pretty much it for Day Eight. Day Nine didn't see much action of note until we headed to Portland to pick up Mamaw and Papaw Hinkle. After we loaded them up we stopped at L.L. Bean for a bit. They don't allow you to leave the state until you've made a purchase there. We drove down to the restau