当我们在选中“记住密码”的情况下登录 SQL Management Studio(使用服务器名称、登录名和密码)时。我需要知道,它保存在 PC 中的位置。
我需要格式化我的电脑。当我们安装 SQL Management Studio 时,我将丢失我保存的所有凭据。这就是为什么我需要将这些文件备份到保存位置的原因。
小智 12
答案是一个基于本文先前答案的简单版本,并进行了一些必要的修复。
假设您运行的是 SQL 2008 或更高版本
转到 Microsoft SQL Server Management Studio,右键单击任何已连接的服务器,单击“注册”并选择服务器,如果您有该服务器保存的密码,则密码应该已填充。然后单击“保存”现在转到主菜单 -> 查看 -> 注册服务器,您将看到刚刚注册的服务器,现在右键单击它并单击任务 -> 导出,指定文件名并取消选中“不包含用户”导出文件中的名称和密码”,导出的服务器将具有如下扩展名:“.regsrvr”,现在通过使用以下脚本,您将看到解密的连接字符串:
param(
[Parameter(Mandatory=$true)]
[string] $FileName
)
Add-Type -AssemblyName System.Security
$ErrorActionPreference = 'Stop'
function Unprotect-String([string] $base64String)
{
return [System.Text.Encoding]::Unicode.GetString([System.Security.Cryptography.ProtectedData]::Unprotect([System.Convert]::FromBase64String($base64String), $null, [System.Security.Cryptography.DataProtectionScope]::CurrentUser))
}
$document = [xml] (Get-Content $FileName)
$nsm = New-Object 'System.Xml.XmlNamespaceManager' ($document.NameTable)
$nsm.AddNamespace('rs', 'http://schemas.microsoft.com/sqlserver/RegisteredServers/2007/08')
$attr = $document.DocumentElement.GetAttribute('plainText')
if ($attr -ne '' -and $Operation -ieq 'Decrypt')
{
throw "The file does not contain encrypted passwords."
}
$servers = $document.SelectNodes("//rs:RegisteredServer", $nsm)
foreach ($server in $servers)
{
$connString = $server.ConnectionStringWithEncryptedPassword.InnerText
echo ""
echo "Encrypted Connection String:"
echo $connString
echo ""
if ($connString -inotmatch 'password="?([^";]+)"?') {continue}
$password = $Matches[1]
$password = Unprotect-String $password
echo ""
echo "Decrypted Connection String:"
$connString = $connString -ireplace 'password="?([^";]+)"?', "password=`"$password`""
echo $connString
echo ""
}
Run Code Online (Sandbox Code Playgroud)
如何使用脚本:
小智 8
首先,您需要在 SSMS 中注册服务器。在对象资源管理器中右键单击服务器并选择注册或右键单击本地服务器组,选择新服务器注册并选择服务器名称。如果之前记住了服务器密码,则会填写服务器密码。然后根据@mrdenny 的回答导出服务器。
现在是棘手的部分。您需要重新加密目标计算机上用户配置文件下的密码。我准备了一个可以做到这一点的 PowerShell 脚本。
param(
[Parameter(Mandatory=$true)]
[string] $FileName,
[Parameter(Mandatory=$true)][ValidateSet('Decrypt', 'Encrypt')]
[string] $Operation
)
$ErrorActionPreference = 'Stop'
function Protect-String([string] $clearText)
{
return [System.Convert]::ToBase64String([System.Security.Cryptography.ProtectedData]::Protect([System.Text.Encoding]::Unicode.GetBytes($clearText), $null, [System.Security.Cryptography.DataProtectionScope]::CurrentUser))
}
function Unprotect-String([string] $base64String)
{
return [System.Text.Encoding]::Unicode.GetString([System.Security.Cryptography.ProtectedData]::Unprotect([System.Convert]::FromBase64String($base64String), $null, [System.Security.Cryptography.DataProtectionScope]::CurrentUser))
}
$document = [xml] (Get-Content $FileName)
$nsm = New-Object 'System.Xml.XmlNamespaceManager' ($document.NameTable)
$nsm.AddNamespace('rs', 'http://schemas.microsoft.com/sqlserver/RegisteredServers/2007/08')
$attr = $document.DocumentElement.GetAttribute('plainText')
if ($attr -eq '' -and $Operation -ieq 'Encrypt')
{
throw "The file does not contain plaintext passwords."
}
if ($attr -ne '' -and $Operation -ieq 'Decrypt')
{
throw "The file does not contain encrypted passwords."
}
$servers = $document.SelectNodes("//rs:RegisteredServer", $nsm)
foreach ($server in $servers)
{
$connString = $server.ConnectionStringWithEncryptedPassword.InnerText
if ($connString -inotmatch 'password="([^"]+)"') {continue}
$password = $Matches[1]
if ($Operation -ieq 'Decrypt')
{
$password = Unprotect-String $password
}
if ($Operation -ieq 'Encrypt')
{
$password = Protect-String $password
}
$connString = $connString -ireplace 'password="([^"]+)"', "password=`"$password`""
$server.ConnectionStringWithEncryptedPassword.InnerText = $connString
}
if ($Operation -ieq 'Decrypt')
{
$document.DocumentElement.SetAttribute('plainText', 'true')
}
else
{
$document.DocumentElement.RemoveAttribute('plainText')
}
$document.Save($FileName)
Run Code Online (Sandbox Code Playgroud)
在源机器上运行.\Move-SqlRegisteredServers.ps1 -FileName 'Your.regsrvr' -Operation Decrypt. 这将用纯文本替换加密的密码。
在目标机器上运行.\Move-SqlRegisteredServers.ps1 -FileName 'Your.regsrvr' -Operation Encrypt. 这将使用新密钥再次加密密码。
现在您可以将Your.regsrvr文件导入 SSMS,并将您的服务器与保存的凭据放在一起。
假设您运行的是 SQL 2008 或更高版本,请在“注册服务器”窗口中右键单击“数据库引擎”下的文件夹,然后选择“任务”,然后选择“导出”。指定一个文件并取消选中“在导出文件中不包含用户名和密码”复选框。单击确定。安全这个文件。当您重建您的机器时,导入该文件,您将拥有该列表中保存的所有内容。
了解您运行 SSMS 的 SQL Server 版本和操作系统会很有帮助。话虽如此,对于 SQL Server 2008,它存储在 SqlStudio.bin 文件中:
%appdata%\Microsoft\Microsoft SQL Server\100\Tools\Shell\SqlStudio.bin
Run Code Online (Sandbox Code Playgroud)
据我了解,这里存储了很多其他设置,只需将该文件移动到某个地方,可能会或可能不会适合您。
| 归档时间: |
|
| 查看次数: |
38026 次 |
| 最近记录: |