Option 1: Use the GUI
Run Ssms.exe > login as sa > Right-click SQL Server instance > Properties > Database Settings > modify the field next to ‘Backup’ > OK > restart SQLSERVER when it’s safe to do so
Option 2: PowerShell
# Localhost version
$newBackupDirectory='Z:\Backup'
function changeSqlBackupDirectory($newBackupDirectory){
$ErrorActionPreference='stop'
function confirmation($content,$testValue="I confirm",$maxAttempts=3){
$confirmed=$false;
$attempts=0;
$content|write-host
write-host "Please review this content for accuracy.`r`n"
while ($attempts -le $maxAttempts){
if($attempts++ -ge $maxAttempts){
write-host "A maximum number of attempts have reached. No confirmations received!`r`n"
break;
}
$userInput = Read-Host -Prompt "Please type in this value => $testValue <= to confirm. Input CANCEL to skip this item";
if ($userInput.ToLower() -eq $testValue.ToLower()){
$confirmed=$true;
write-host "Confirmed!`r`n";
break;
}elseif($userInput -like 'cancel'){
write-host 'Cancel command received.'
$confirmed=$false
break
}else{
Clear-Host;
write-host $($content|out-string).Trim()
write-host "Attempt number $attempts of $maxAttempts`: $userInput does not match $testValue. Try again or Input CANCEL to skip this item`r`n"
}
}
return $confirmed;
}
try{
$sqlFolder=(Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL').MSSQLSERVER
$registrySqlDefaults="REGISTRY::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\$sqlFolder\MSSQLServer"
$sqlDefaults=get-itemproperty $registrySqlDefaults
$backupDirectory=$sqlDefaults.BackupDirectory
write-host "Default backup directory prior to changes: '$backupDirectory'"
$confirmed=confirmation "Change default backup from $backupDirectory to $newBackupDirectory"
if($confirmed){
Set-Itemproperty -path $registrySqlDefaults -Name 'BackupDirectory' -value $newBackupDirectory
$newSqlDefaults=get-itemproperty $registrySqlDefaults
$changedBackupDirectory=$newSqlDefaults.BackupDirectory
write-host "Default backup directory has been changed to: '$changedBackupDirectory'"
write-warning 'Refreshing the SQLSERVER service for changes to become effective.'
import-module sqlps
$sqlServer=(get-item "SQLSERVER:\SQL\$env:computername").ManagedComputer
$defaultInstance=$sqlServer.Services['MSSQLSERVER']
#$defaultInstance.stop()
$defaultInstance.refresh() # Refresh seems to be sufficient in lieu of a restart
#$defaultInstance.start()
return $true
}else{
write-host "No changes have been made as process has been cancelled by $env:username"
return $false
}
}catch{
write-warning $_
return $false
}
}
changeSqlBackupDirectory $newBackupDirectory
$setBackupDirectory='Z:\Backup'
$sqlServers='sql01','sql02','sql03'
$adminCredential=get-credential
function setSqlBackupDirectory($sqlServers,$setBackupDirectory,$adminCredential){
function changeSqlBackupDirectory($newBackupDirectory){
$ErrorActionPreference='stop'
function confirmation($content,$testValue="I confirm",$maxAttempts=3){
$confirmed=$false;
$attempts=0;
$content|write-host
write-host "Please review this content for accuracy.`r`n"
while ($attempts -le $maxAttempts){
if($attempts++ -ge $maxAttempts){
write-host "A maximum number of attempts have reached. No confirmations received!`r`n"
break;
}
$userInput = Read-Host -Prompt "Please type in this value => $testValue <= to confirm. Input CANCEL to skip this item";
if ($userInput.ToLower() -eq $testValue.ToLower()){
$confirmed=$true;
write-host "Confirmed!`r`n";
break;
}elseif($userInput -like 'cancel'){
write-host 'Cancel command received.'
$confirmed=$false
break
}else{
Clear-Host;
write-host $($content|out-string).Trim()
write-host "Attempt number $attempts of $maxAttempts`: $userInput does not match $testValue. Try again or Input CANCEL to skip this item`r`n"
}
}
return $confirmed;
}
try{
$sqlFolder=(Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL').MSSQLSERVER
$registrySqlDefaults="REGISTRY::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\$sqlFolder\MSSQLServer"
$sqlDefaults=get-itemproperty $registrySqlDefaults
$backupDirectory=$sqlDefaults.BackupDirectory
write-host "Default backup directory prior to changes: '$backupDirectory'"
$confirmed=confirmation "Change default backup from $backupDirectory to $newBackupDirectory"
if($confirmed){
Set-Itemproperty -path $registrySqlDefaults -Name 'BackupDirectory' -value $newBackupDirectory
$newSqlDefaults=get-itemproperty $registrySqlDefaults
$changedBackupDirectory=$newSqlDefaults.BackupDirectory
write-host "Default backup directory has been changed to: '$changedBackupDirectory'"
write-warning 'Refreshing the SQLSERVER service for changes to become effective.'
import-module sqlps
$sqlServer=(get-item "SQLSERVER:\SQL\$env:computername").ManagedComputer
$defaultInstance=$sqlServer.Services['MSSQLSERVER']
#$defaultInstance.stop()
$defaultInstance.refresh() # Refresh seems to be sufficient in lieu of a restart
#$defaultInstance.start()
return $true
}else{
write-host "No changes have been made as process has been cancelled by $env:username"
return $false
}
}catch{
write-warning $_
return $false
}
}
$results=@{}
foreach ($sqlServer in $sqlServers){
$result=invoke-command -computername $sqlServer -Credential $adminCredential -scriptblock{
param($changeSqlBackupDirectory,$newBackupDirectory)
[scriptblock]::create($changeSqlBackupDirectory).invoke($newBackupDirectory)
} -Args ${function:changeSqlBackupDirectory},$setBackupDirectory
write-host "$sqlServer`: $result"
$results+=@{$sqlServer=$result}
}
return $results
}
setSqlBackupDirectory $sqlServers $setBackupDirectory $adminCredential
Categories: