﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Programming / SMO/RMO/DMO  / Detach database, copy to new location, attach database / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 18 May 2013 22:32:54 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Detach database, copy to new location, attach database</title><link>http://www.sqlservercentral.com/Forums/Topic1297937-22-1.aspx</link><description>Well, I will say it is not as obvious as Database.LogFiles hanging off the database object straight-away but when you think about it in terms of how SQL Server organizes data files into groups it makes more sense.I think you'll want code like this:for each $fileGroup in [u][url=http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.database.filegroups(v=sql.105).aspx]Database.FileGroups[/url][/u]---- for each $dataFile in [u][url=http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.filegroup.files(v=sql.105).aspx]$fileGroup.Files[/url][/u]---- ---- do work</description><pubDate>Fri, 11 May 2012 08:52:42 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>RE: Detach database, copy to new location, attach database</title><link>http://www.sqlservercentral.com/Forums/Topic1297937-22-1.aspx</link><description>Well.  Now that you mention it.  :-)What is the corresponding object/property for the data file(s)?Brett</description><pubDate>Fri, 11 May 2012 08:41:46 GMT</pubDate><dc:creator>brett-585458</dc:creator></item><item><title>RE: Detach database, copy to new location, attach database</title><link>http://www.sqlservercentral.com/Forums/Topic1297937-22-1.aspx</link><description>You're welcome :-) Post back if you get stuck going beyond what I gave you.</description><pubDate>Fri, 11 May 2012 08:38:54 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>RE: Detach database, copy to new location, attach database</title><link>http://www.sqlservercentral.com/Forums/Topic1297937-22-1.aspx</link><description>Thanks opc.three,That did it!  I used your code snippet and installed the package you suggested and did the trick.Thanks again,Brett</description><pubDate>Fri, 11 May 2012 08:34:56 GMT</pubDate><dc:creator>brett-585458</dc:creator></item><item><title>RE: Detach database, copy to new location, attach database</title><link>http://www.sqlservercentral.com/Forums/Topic1297937-22-1.aspx</link><description>Relevant?[u][url=http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/861b57bc-aa21-4b03-bd01-5c164e2bac4e]http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/861b57bc-aa21-4b03-bd01-5c164e2bac4e[/url][/u]</description><pubDate>Thu, 10 May 2012 21:20:08 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>RE: Detach database, copy to new location, attach database</title><link>http://www.sqlservercentral.com/Forums/Topic1297937-22-1.aspx</link><description>I ran this with my info but am still having a problem.  Here is the exact script I'm running (copy of yours with names changed):Add-Type -AssemblyName “Microsoft.SqlServer.Smo”######################################################################$instanceName = 'BRETTW7'$databaseName = 'test1'######################################################################$server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName$database = $server.Databases[$databaseName]	Write-Host ("$database has " + $database.LogFiles.Count + " log files.")foreach($logFile in $database.LogFiles){    Write-Host ("`t" + $logFile.Name + " -&amp;gt; " + $logFile.FileName)}# $server.DetachDatabase(string databaseName,bool updateStatistics) &amp;gt; http://msdn.microsoft.com/en-us/library/ms210177.aspxAnd here is the output and error:[test1] has  log files.The following exception was thrown when trying to enumerate the collection: "Could not load file or assembly 'Microsoft.SqlServer.BatchParser, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.".At C:\Users\Brett\AppData\Local\Temp\bdd09e9f-9e90-425a-bfc0-3810a9fb614c.ps1:15 char:8+ foreach &amp;lt;&amp;lt;&amp;lt;&amp;lt; ($logFile in $database.LogFiles)    + CategoryInfo          : NotSpecified: (:) [], ExtendedTypeSystemException    + FullyQualifiedErrorId : ExceptionInGetEnumerator Thanks for you help,Brett</description><pubDate>Thu, 10 May 2012 19:34:43 GMT</pubDate><dc:creator>brett-585458</dc:creator></item><item><title>RE: Detach database, copy to new location, attach database</title><link>http://www.sqlservercentral.com/Forums/Topic1297937-22-1.aspx</link><description>Here is a start for you. Post back if you have more questions:[code="other"]Add-Type -AssemblyName “Microsoft.SqlServer.Smo”######################################################################$instanceName = '.\STD2008R2'$databaseName = 'test_multi_files'######################################################################$server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName$database = $server.Databases[$databaseName];Write-Host ("$database has " + $database.LogFiles.Count + " log files.")foreach($logFile in $database.LogFiles){    Write-Host ("`t" + $logFile.Name + " -&amp;gt; " + $logFile.FileName)}# $server.DetachDatabase(string databaseName,bool updateStatistics) &amp;gt; http://msdn.microsoft.com/en-us/library/ms210177.aspx[/code]</description><pubDate>Thu, 10 May 2012 12:44:54 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>Detach database, copy to new location, attach database</title><link>http://www.sqlservercentral.com/Forums/Topic1297937-22-1.aspx</link><description>Hello,PowerShell newbie here. I'm trying to write a PS script that will detach a database, copy the data and log files to new location, and attach database using new file location. I'm stuck on getting the current physical file names. Here is the code I have so far:[void] [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | out-null[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlEnum") | out-null$server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList 'BRETTW7'$database = $server.Databases['Test1'];$dblogfiles = $database.LogFiles.Item(0).Name;Write-Host 1: $dblogfiles	;$fileToRename = $database.FileGroups["PRIMARY"].Files[$dblogfiles];write-host 2: $database.FileGroups.get_Item(0).FilesWrite-Host 3: $fileToRename	;Write-Host 4: $initialpath;So -- how do I get the physical file name of the data and log files?Also, does anyone have any scripts or snippets that would help me?Thank you in advance.Brett </description><pubDate>Thu, 10 May 2012 08:48:49 GMT</pubDate><dc:creator>brett-585458</dc:creator></item></channel></rss>