In this recipe, we will programmatically add files to a FileTable.
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.
Let's take a look at the steps required to add multiple files to our FileTable using PowerShell and SMO:
Open PowerShell ISE as an administrator.
Import the
SQLPS
module as follows:#import SQL Server module Import-Module SQLPS -DisableNameChecking
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...