Book Image

SQL Server 2014 with Powershell v5 Cookbook

By : Donabel Santos
Book Image

SQL Server 2014 with Powershell v5 Cookbook

By: Donabel Santos

Overview of this book

PowerShell can be leveraged when automating and streamlining SQL Server tasks. PowerShell comes with a rich set of cmdlets, and integrates tightly with the .NET framework. Its scripting capabilities are robust and flexible, allowing you to simplify automation and integration across different Microsoft applications and components. The book starts with an introduction to the new features in SQL Server 2014 and PowerShell v5 and the installation of SQL Server. You will learn about basic SQL Server administration tasks and then get to know about some security-related topics such as the authentication mode and assigning permissions. Moving on, you will explore different methods to back up and restore your databases and perform advanced administration tasks such as working with Policies, Filetables, and SQL audits. The next part of the book covers more advanced HADR tasks such as log shipping and data mirroring, and then shows you how to develop your server to work with BLOB, XML, and JSON. Following on from that, you will learn about SQL Server's BI stack, which includes SSRS reports, the SSIS package, and the SSAS cmdlet and database. Snippets not specific to SQL Server will help you perform tasks quickly on SQL servers. Towards the end of the book, you will find some useful information, which includes a PowerShell tutorial for novice users, some commonly-used PowerShell and SQL Server syntax, and a few online resources. Finally, you will create your own SQL Server Sandbox VMs. All these concepts will help you to efficiently manage your administration tasks.
Table of Contents (15 chapters)
14
Index

Extracting binary data from SQL Server

In this recipe, we will extract the binary content from SQL Server and save it back to individual files in the filesystem.

Getting ready

For this recipe, we will use the table that we created in the previous recipe, Storing binary data in SQL Server, to extract files. Feel free to use your own tables that have the VARBINARY(MAX) columns; just ensure that you change the table name in the script.

In addition to our SampleBLOB table, we will create an empty table with a single VARBINARY(MAX) table. We will use this to facilitate the creation of a format file we need to export binary data out of SQL Server using bcp:

USE SampleDB
GO
IF OBJECT_ID('EmptyBLOB') IS NOT NULL
    DROP TABLE EmptyBLOB
GO
CREATE TABLE EmptyBLOB
(
    BLOBStuff VARBINARY(MAX)
)

How to do it...

These are the steps required to extract binary data from SQL Server:

  1. Open PowerShell ISE as an administrator.
  2. Import the SQLPS module as follows:
    #import SQL Server module
    Import-Module...