Powershell Quick Tips: Valid Folder name string when working with SMO .AddDevices()

When building a Powershell script with SMO to Backup/Restore databases I found an error when using folder names in the string.  The following foldername sample will show you what’s correct (or not) when using the .AddDevice() method in either SQL Backup and Restore:

$Device1 = "C:\Temp\Sample.bak"

AddDevice($Device1)   #procedure will run

$Device2 = "C:\Program Files\Microsoft SQL Server\MSSQL\Backup\Nw.bak"

AddDevice($Device2)  #procedure will fail

$Device3 = "C:\’Program Files’\’Microsoft SQL Server’\MSSQL\Backup\Nw.bak"

AddDevice($Device3)   #procedure will run

If the folder name includes spaces then your SMO procedure will not work.  You need to use the single quote in both beginning and end of the foldername string.  Keep in mind, You don’t need to use the single quote in the string if you’re using the DOS Copy, CD, and MD commands.  Always test you scripts!


About maxt2posh

I’m from Puerto Rico, have been working with computers since 1979. Throughout many years, I have worked with SQL Server Technologies, provided support to Windows Servers/Client machines, Microsoft Virtualization Technologies, and build some Visual Studio solutions. I’m a Microsoft PowerShell MVP since 2009 and MVP SAPIEN Technologies since 2010. I speak in most of the SQLSaturday, IT Pro and .NET camps events around the Florida’s State. Also founder of the Florida PowerShell User Group which meeting every 3rd Tuesday evening of the month.
This entry was posted in PowerShell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s