Powershell 脚本目标是将数据库从一台服务器复制到另一台服务器,然后在目标服务器上执行简单的查询。当前用户对数据库服务器没有权限,因此在两个服务器中都创建了一个登录名,并将密码作为安全字符串存储在文件中:
$sourceServer = "ServerA"
$destServer = "ServerB"
$restoreUser = "replicationuser"
$encPass = Get-Content -Path ".SecuredPass.spf"
$secString= $encPass | ConvertTo-SecureString -Key (1..16)
$restoreCredential=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $restoreUser, $secString
Copy-DbaDatabase -Source $sourceServer -Destination $destServer -Database "MyDatabase" -SourceSqlCredential $restoreCredential -DestinationSqlCredential $restoreCredential -BackupRestore -NetworkShare "\MyShareDatabases" -WithReplace -EnableException
$sqlQuery = 'select 1+1'
Invoke-DbaSqlQuery -Query $sqlQuery -SqlInstance $destServer -SqlCredential $restoreCredential
脚本运行时,将复制数据库,但查询执行会给出以下消息:
WARNING: [15:53:29][Invoke-DbaQuery] Failure | Property LoginSecure cannot be changed or read after a connection string has been set.
运行查询时应执行哪些操作才能使用凭据?
我们在 Invoke-DbaQuery
中处理凭据的方式存在一些问题。您可以尝试最新版本,看看此问题是否仍然存在。