• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 546
  • Last Modified:

PowerShell to Backup SQL Database

I have the following code to back up a SQL Server database that works like a charm. The only problem is, it has been running all along with integrated security. The script is being pushed to a new server the database connection will be with SQL Server authorization (UserID and password).

I have two PoSh variables named $UserID and $Password. How would I integrate that into the following script to pass those credentials along?
function BackupDatabase($step, $server, $database)
	$s = new-object ("Microsoft.SqlServer.Management.Smo.Server") $server

	$bkdir = $s.Settings.BackupDirectory
	$dbname = $database
	$backupFileName = $bkdir + "\" + $dbname + "_" + $step + "_" + $now + ".bak"
	Write-Host "$step Conversion backup."
	Write-Host "Backing up the [$dbname] database to $backupFileName"
	$dbBackup = new-object ("Microsoft.SqlServer.Management.Smo.Backup")
	$dbBackup.Action = "Database"
	$dbBackup.Database = $dbname
	$dbBackup.CompressionOption = 1
	$dbBackup.CopyOnly = $TRUE
	$dbBackup.Devices.AddDevice($backupFileName, "File")
	return $?

Open in new window

1 Solution
dbbishopAuthor Commented:
Got it to work. Thx.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now