As referenced in my blog article from yesterday, I’ll be presenting a PowerShell 101 session at SQL Saturday Atlanta tomorrow morning (July 15, 2017). While I plan to cover the basics of PowerShell, I also plan to show you what you can do with PowerShell without having to write very much code at all.
I’ll be killing it in my session with live demos including this one that I’ve made a video of as a sneak preview.
If the video embedded in this webpage doesn’t display properly, you can find it on YouTube using this URL. Oh, by the way, the title to this blog article is a little misleading since the entire video is 100 seconds. The actual time to copy the databases is much less.
The video starts out by checking the default instance of SQL Server on a server named SQL17 to see if any user databases exist. Then the names of five different SQL Servers are piped to ForEach-Object. Within the ForEach-Object loop, $_ is a variable for the current object. It’s translated to each individual server name as it iterates through the list of SQL Servers, copying the user databases to SQL17. Only one user database exists on each of the source SQL Servers. The databases are backed up to the specified network share and restored to the destination server. The network share and any sub-folders that are specified must already exist. The account that SQL Server runs as on each of the servers must also have access to the network share. The names of the SQL Servers used in the demo correspond to the version of SQL Server they’re running. The SQL05 server is running Windows Server 2008 (non-R2) and does not have any version of PowerShell installed which means the Copy-SqlDatabase function is extremely versatile.
PowerShell has been around for over 10 years now which means that someone has probably already written the code for whatever it is that you’re trying to accomplish. Using modules such as the DBATools PowerShell module that’s demonstrated in this teaser video, you can accomplish some amazing things without having to reinvent the wheel. More information about the DBATools module can be found at dbatools.io.