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

Table of Contents (21 chapters)
SQL Server 2014 with PowerShell v5 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Adding files to a FileTable


In this recipe, we will programmatically add files to a FileTable.

Getting ready

Before you proceed with this recipe, you need to have a FileTable in your database. If you do not have one yet, you can follow the Adding a FileTable recipe in Chapter 6, Advanced Administration, to set one up. Alternatively, you can run the prep file B04525 - Ch09 - 03 - Adding Files to FileTable Prep.ps1 from the code bundle.

How to do it...

Let's take a look at the steps required to add multiple files to our FileTable using PowerShell and SMO:

  1. Open PowerShell ISE as an administrator.

  2. Import the SQLPS module as follows:

    #import SQL Server module
    Import-Module SQLPS -DisableNameChecking
  3. Add the following script and run it:

    $instanceName = "localhost"
    $databaseName = "FilestreamDB"
    $tableName = "MyFiles"
    
    $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
    $db = $server.Databases[$databaseName]
    $table = $db.Tables[$tableName]
    
    #what is the filetable...