调用 powershell 函数,通过 splatting 发送参数
Calling a powershell function, sending parameters via splatting
我有一个从 SqlPs 模块调用 "Restore-SqlDatabase" 并通过 splatting 将参数发送到该函数的 Powershell 函数。我试图弄清楚为什么它没有按我期望的方式工作。这是我的代码:
$SqlParams = @{"ServerInstance"="'$ServerName'";
"Database"="'$DatabaseName'";
"BackupFile"="'$BackupFile'";}
if($Credential -ne $null) {
$SqlParams.Add("SqlCredential", $Credential)
}
if($ReplaceDatabase) {
$SqlParams.Add("ReplaceDatabase", $null)
}
try { $PathResult = Test-Path -Path $RestorePath } catch { $PathResult = $False }
if($PathResult) {
Write-Verbose "RestorePath exists, using: $RestorePath"
$RelocateData = New-Object 'Microsoft.SqlServer.Management.Smo.RelocateFile, Microsoft.SqlServer.SmoExtended, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91'
$RelocateData.LogicalFileName = $DatabaseName
$RelocateData.PhysicalFileName = $(Join-Path -Path $RestorePath -ChildPath "$DatabaseName.mdf")
$RelocateLog = New-Object 'Microsoft.SqlServer.Management.Smo.RelocateFile, Microsoft.SqlServer.SmoExtended, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91'
$RelocateLog.LogicalFileName = "$DatabaseName`_log"
$RelocateLog.PhysicalFileName = $(Join-Path -Path $RestorePath -ChildPath "$DatabaseName`_log.ldf")
$RelocateFile = @($RelocateData,$RelocateLog)
$SqlParams.Add("RelocateFile", $RelocateFile)
}
try{
Write-Verbose "Using the following parameters:"
$paramString = $SqlParams | Out-String
Write-Verbose $paramString
Restore-SqlDatabase @SqlParams
} catch {
Write-Error $_
Write-Output "Restore failed..."
return
}
Write-Output "Database successfully restored!"
当我运行这个的时候,它无法连接到数据库。然而,当我 运行 直接在终端中使用 "param" 哈希表和相同参数值的命令时,它完全按预期工作。
有什么建议吗?
这里有一些秘诀。
在 Restore-SqlDatabase
调用之前执行此操作:
SQLCMD -S $ServerName -d master -Q "Alter Database [$DatabaseName] SET SINGLE_USER With ROLLBACK IMMEDIATE"
并在 Restore-SqlDatabase
调用后执行此操作:
SQLCMD -S $ServerName -d master -Q "Alter Database [$DatabaseName] SET MULTI_USER WITH ROLLBACK IMMEDIATE"
现在似乎一切正常。
我有一个从 SqlPs 模块调用 "Restore-SqlDatabase" 并通过 splatting 将参数发送到该函数的 Powershell 函数。我试图弄清楚为什么它没有按我期望的方式工作。这是我的代码:
$SqlParams = @{"ServerInstance"="'$ServerName'";
"Database"="'$DatabaseName'";
"BackupFile"="'$BackupFile'";}
if($Credential -ne $null) {
$SqlParams.Add("SqlCredential", $Credential)
}
if($ReplaceDatabase) {
$SqlParams.Add("ReplaceDatabase", $null)
}
try { $PathResult = Test-Path -Path $RestorePath } catch { $PathResult = $False }
if($PathResult) {
Write-Verbose "RestorePath exists, using: $RestorePath"
$RelocateData = New-Object 'Microsoft.SqlServer.Management.Smo.RelocateFile, Microsoft.SqlServer.SmoExtended, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91'
$RelocateData.LogicalFileName = $DatabaseName
$RelocateData.PhysicalFileName = $(Join-Path -Path $RestorePath -ChildPath "$DatabaseName.mdf")
$RelocateLog = New-Object 'Microsoft.SqlServer.Management.Smo.RelocateFile, Microsoft.SqlServer.SmoExtended, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91'
$RelocateLog.LogicalFileName = "$DatabaseName`_log"
$RelocateLog.PhysicalFileName = $(Join-Path -Path $RestorePath -ChildPath "$DatabaseName`_log.ldf")
$RelocateFile = @($RelocateData,$RelocateLog)
$SqlParams.Add("RelocateFile", $RelocateFile)
}
try{
Write-Verbose "Using the following parameters:"
$paramString = $SqlParams | Out-String
Write-Verbose $paramString
Restore-SqlDatabase @SqlParams
} catch {
Write-Error $_
Write-Output "Restore failed..."
return
}
Write-Output "Database successfully restored!"
当我运行这个的时候,它无法连接到数据库。然而,当我 运行 直接在终端中使用 "param" 哈希表和相同参数值的命令时,它完全按预期工作。
有什么建议吗?
这里有一些秘诀。
在 Restore-SqlDatabase
调用之前执行此操作:
SQLCMD -S $ServerName -d master -Q "Alter Database [$DatabaseName] SET SINGLE_USER With ROLLBACK IMMEDIATE"
并在 Restore-SqlDatabase
调用后执行此操作:
SQLCMD -S $ServerName -d master -Q "Alter Database [$DatabaseName] SET MULTI_USER WITH ROLLBACK IMMEDIATE"
现在似乎一切正常。