简体   繁体   English

Powershell:使用“ 3”参数调用“ AttachDatabase”的异常

[英]Powershell: Exception calling “AttachDatabase” with “3” argument(s)

I 'm trying to backup the mdf file using the following approach but the script fails when attaching the database back to the server. 我正在尝试使用以下方法备份mdf文件,但是将数据库附加到服务器时脚本失败。 I have started facing this issue in our new server (Windows Server 2008 R2). 我已经开始在新服务器(Windows Server 2008 R2)中解决此问题。 The following is the scriplet. 以下是片段。

#load assemblies
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null

write-host "Initializing..."
#Initialization section
$serverName = "sqlserver\SQL2005"
$databaseName = "myDatabase"
$sourceLocation = "\\sqlserver\dbPath\myDatabase"
$attachDBSourceLocation = "D:\Sql 2005 Databases\dbPath\myDatabase"
$mdfFileName = $sourceLocation + "\" + $databaseName + ".mdf"
$attachMDFFileName = $attachDBSourceLocation + "\" + $databaseName + ".mdf"
$ldfFileName = $sourceLocation + "\" + $databaseName + "_log.ldf"
$destLocation = read-host -prompt "Enter the destination location"
#End of Initialization section
write-host "Initialization completed"

#create a new server object
$serverConn = New-Object ("Microsoft.SqlServer.Management.Common.ServerConnection")
$serverName, "psuser", "psuser"
$server = New-Object ("Microsoft.SqlServer.Management.Smo.Server") $serverConn 
write-host "Server object created"

$server.Databases["myDatabase"].ExecuteNonQuery("ALTER DATABASE myDatabase SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE", [Microsoft.SqlServer.Management.Common.ExecutionTypes]::Default)

#detach the database to be copied. Drop connections if any by changing the connection mode to single user mode
$server.DetachDatabase($databaseName, $true)
write-host "Database $databaseName detached"

#copy the database (backup)
write-host "Started copying database '$mdfFileName' to '$destLocation'. Please wait..."
copy-item $mdfFileName -Destination $destLocation
write-host "Database '$databaseName' copied to '$destLocation'"

#delete the log file
remove-item -path $ldfFileName
write-host "Database LDF file deleted" #attach the mdf file without ldf so that a new ldf is automatically created
write-host "Attaching Database '$databaseName'. Please wait..."
$strColl = New-Object ("System.Collections.Specialized.StringCollection")
[void]$strColl.Add($attachMDFFileName)
$server.AttachDatabase($databaseName, $strColl, [Microsoft.SqlServer.Management.Smo.AttachOptions]::NewBroker)
write-host "Database attached successfully"

write-host "Shrinking Database '$databaseName'. Please wait..."
$server.Databases[$databaseName].ExecuteNonQuery("DBCC SHRINKDATABASE ($databaseName, 5)")

write-host "Database shrinked successfully"
write-host "Database backup successful"

Are you sure you need that attachmentoptions overload? 您确定您需要附件选项重载吗? From the overload list for that method: 从该方法的重载列表中:

http://msdn.microsoft.com/en-US/library/microsoft.sqlserver.management.smo.server.attachdatabase(v=SQL.90).aspx http://msdn.microsoft.com/zh-CN/library/microsoft.sqlserver.management.smo.server.attachdatabase(v=SQL.90).aspx

it doesn't appear that you need to specify that if you're re-attaching an existing database, just the database name and the file collection. 似乎不需要指定是否要重新连接现有数据库,只需指定数据库名称和文件集合即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PowerShell:使用“7”参数调用“GetListItems”异常 - PowerShell: Exception calling “GetListItems” with “7” argument(s) 使用“1”参数调用“SetAccessRule”的异常::: Powershell 错误 - Exception calling "SetAccessRule" with "1" argument(s) ::: Powershell Error 创建Powershell字典时,异常调用带有“ 2”参数的“添加” - Exception calling “Add” with “2” argument(s) when creating Powershell dictionary 如何调试使用“5”参数调用“InvokeMember”的powershell异常:类型不匹配 - How to debug a powershell exception calling "InvokeMember" with "5" argument(s): Type mismatch Powershell 错误:使用“2”参数调用“批准”的异常:值不能是 null - Powershell Error: Exception Calling “Approve” with “2” argument(s): Value cannot be null powershell:使用“1”参数调用“填充”的异常:“'/'附近的语法不正确。” ' - powershell: Exception calling “Fill” with “1” argument(s): “Incorrect syntax near '/'.” ' 使用“1”参数调用“发送”的 PowerShell 异常:“发送邮件失败”。 - PowerShell Exception calling “Send” with “1” argument(s): “Failure sending mail.” TcpClient 抛出带有“2”参数的异常调用“.ctor”:在 powershell 中“没有这样的主机是已知的” - TcpClient throws Exception calling ".ctor" with "2" argument(s): "No such host is known" in powershell Powershell Invoke-SSHCommand:使用“1”参数调用“EndExecute”的异常 - Powershell Invoke-SSHCommand: Exception calling “EndExecute” with “1” argument(s) PowerShell 异常从另一个函数中使用“0”参数调用“Start” - PowerShell Exception calling “Start” with “0” argument(s) from within another function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM