Press "Enter" to skip to content

Category: Powershell

Log Shipping Configuration with Powershell

Lee Markum needs to do log shipping in bulk:

I recently needed to configure log shipping for multiple databases at once as part of a migration project. I turned to PowerShell to do this.

But before we get to that part, this post assumes that you’ve done the upfront work to create shares for the backups to write to and for the backups to be copied to. This will involve providing the right permissions for the SQL Server service accounts involved in Log Shipping. If you are not familiar with this, that’s perfectly fine. Check out this article in MS Docs first.

Click through for the next steps in the process, including a well-timed Get-Help call.

Comments closed

Identify SQL Server Configuration Drift

Garry Bargsley won’t let the cattle become pets:

As you sit and wonder about when the next Star Wars movie is going to come out, do you ever get the thought of “I wonder if all my SQL Servers are configured the same?”

Occasionally, I get a thought like that run through my mind. Or I might see something on Twitter or Blog post about something, and it sparks the question.  Not about Star Wars, but my SQL Server environment.

Today something triggered me to confirm that all of my SQL Servers had the default backup compression setting set to enabled.

Garry mentions dbachecks at the end, and it’s a really good way of performing a fairly large number of such checks easily.

Comments closed

Filtering Unique Objects in Powershell

Jeffrey Hicks solves an interesting problem:

A few weeks ago my friend, Gladys Kravitz, was lamenting about a challenge related to filtering for unique objects. PowerShell has a Get-Unique cmdlet, and Select-Object has a -Unique parameter, but these options are limited. On one hand, I’d say most things we manage with PowerShell are guaranteed to be unique. Objects might have a GUID , ID, or SID, to guarantee uniqueness. But, and this is Gladys’ situation, sometimes the things we are managing come from an external source. Such as importing data from a CSV file. In this situation, it is definitely possible to have duplicate objects.

This turns out to be a bit harder than expected.

Comments closed

Fun with Powershell Strings

Robert Cain shows off some features around strings in Powershell:

PowerShell has some of the best, most flexible string handling of any language I’ve used. In this post we’ll look at some string basics, then some of the features that make it special.

Note in these demos I’m using PowerShell Core 7.1.3 and VSCode. Everything here should also work in the PowerShell 5.1 and the PowerShell IDE.

Read on for types of strings, embedded quotations, string interpolation, and more.

Comments closed

Introducing the Display-Object Cmdlet

Phil Factor has a bit of Powershell for us:

How do you list all the objects and values within a PowerShell object, investigate an object or explore its structure? There must be a simple way. I used to use ConvertTo-JSON. This is fine up to a point but what if you just wish to search for strings or look for objects with a cartain name or value? What if you need their path so you can reference them? It isn’t plain-sailing. In the ISE, the intellisense will help you a lot but I want more and I want to do it in script rather than the IDE

Read on to check it out and get a copy of the cmdlet.

Comments closed

Trimming Strings with Powershell

Patrick Gruenauer shows off the Trim series of methods in Powershell:

In PowerShell it’s easy to manipulate strings, especially to cut off some characters from a string. These three methods are your friends when it comes to trimming: Trim, TrimStart and TrimEnd. In this blog post I will show you how to use PowerShell to manipulate strings and cut off something at the beginning and at the end. I will also try to give you some useful practical examples in hand to convince you that it’s worth to learn something about string manipulation. Let’s hop in.

Click through for examples of these three methods and how they do more than removing whitespace.

Comments closed

Updating dbatools

Chad Callihan shows us how to update the dbatools Powershell module:

The first sentence on the dbatools download page references the belief in releasing early and releasing often. While SQL Server and SQL Server Management Studio may get a handful up dates every year, dbatools averages a few every month. Fortunately, staying up to date with dbatools is easily manageable as we’ll see below.

Read on to see how you can tell which version of the module you have and then how to update it.

Comments closed

ConvertTo-SQLSelect

Shane O’Neill has a new cmdlet for us:

Don’t get me wrong – I’m aware that you don’t need Excel installed on the computer where you’re running these commands from. You still need to save the files somewhere though. The function doesn’t take data from variables.

I can use dbatools and Write-DbaDbTableData. This function is not dependent on the table having to already exist. It will create the table for you if you tell it to. Thank you -AutoCreateTable; even though I recommend pre-sizing your columns if you want to go with this method.

However, I don’t want to have to create the table beforehand.

Click through to check it out and grab a copy for yourself.

Comments closed

Converting from XML in Powershell

Phil Factor has unleashed the full power of XML:

I want to convert reasonably small XML files to hash tables and PowerShell objects. PowerShell never had a ConvertFrom-XML Cmdlet because gulping a large XML file into a PowerShell data object is expensive in resources. It is the sheer time it takes to consume a large XML file. Instead, you have to use the XMLDocument object to navigate to the data you want or use an Xpath query. It is all well and good to handle XML in this way, but it is inconsistent to have no ConvertFrom-XML cmdlet. After all, there is a ConvertFrom cmdlet for CSV, JSON, and a variety of text-based data. It would be good to have one for XML as well. Usually, I just want to consume relatively small XML files and just pick out the data I want. I hoped that one that worked would turn up but somehow it never did. So I wrote my own.

Click through for that script, as well as some considerations about using it.

Comments closed