1

I'm trying to backup a SQL database to a remote location, with the Backup-SqlDatabase cmdlet, to a PSDrive (filesystem) with no success.

I am creating a PSDrive for example \\server\sharedFolder with a name TARGET.

New-PSDrive –Name TARGET -PSProvider FileSystem -Root \\server\sharedfolder -Credential (Get-Credentials)

but when I run the backup like:

Backup-SqlDatabase -ServerInstance $server -Database $dbName -BackupFile "TARGET:bckfile.bak" -Credential $sqlCredentials

I get a completely different location and error:

Backup-SqlDatabase : System.Data.SqlClient.SqlError: Cannot open backup device 'E:\SQL_BCK\TARGET\TARGET:bckfile.bak'. Operating system error 3 (The system cannot find the path specified.).

But if I run the backup like:

Backup-SqlDatabase -ServerInstance $server -Database $dbName -BackupFile "\\server\sharedfolder\bckfile.bak" -Credential $sqlCredentials

I have no problems.

The issue is, I need to access the shared folder with different credentials, so the PSDrive was the perfect solution, because during configration I ask the user for credentials.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Nuno
  • 126
  • 2
  • 14
  • How do you create the psdrive `TARGET`? If you create it on your local workstation, it isn't visible on the server that is actually writing the data. – vonPryz Feb 01 '17 at 17:30
  • New-PSDrive –Name TARGET -PSProvider FileSystem -Root \\server\sharedfolder -Credential (Get-Credentials)... the target is correct... but doesn't work when used in the backup-sqldatabase – Nuno Feb 01 '17 at 17:34
  • Try `New-PSDrive ... -Persist`. – Ansgar Wiechers Feb 02 '17 at 11:14

1 Answers1

0

Try to add -Scope "Script" into New-PSDrive

Check Scope section in the doc