SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Retrieve data and log file names from SQL database


Retrieve data and log file names from SQL database

Author
Message
Robin35
Robin35
SSChasing Mays
SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)

Group: General Forum Members
Points: 600 Visits: 1224
Hi,

I'm trying to pull the logical and physical file names from sql server database using powershell and add those values in exisiting xml file...
This whole process is for database restore...we don't do direct sql db restore..we use third party tool called Commvault...that is the reason for restore automation...Here's my code....its just a part of it....

param
(

[Parameter(Mandatory="True")][String]$ClientName,
[Parameter(Mandatory="True")][String]$Sourcedatabase
#[Parameter(Mandatory="True")][String]$Destdatabase

)

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
#$DataSet = New-Object System.Data.DataSet

$SqlConnection.ConnectionString = "Server = $ClientName; Database = $RestoreSource; Integrated Security = True"

$SqlCmd.CommandText = "select d.name dbname,f.name logicalfilename,f.physical_name from sys.master_files f, sys.databases d where f.database_id = d.database_id and d.name = '$Sourcedatabase' and f.type = 0 "

$SqlCmd.Connection = $SqlConnection
$SqlAdapter.SelectCommand = $SqlCmd
$SqlAdapter.Fill($DataSet)|out-null
$dbs = $DataSet.Tables[0]
$dbs
$SqlConnection.Close()


In the above sql server i'm just trying to pull data file with type = 0..its working fine...if i put type =1 i can get log file info....please let me know how to get data and log file information, such that i can path those information into xml file ...like below

<device>|SourceDB|#12!DestinationDB|#12!SourceLogicaldatafilename|#12!Destinationdatafilepath|#12!Sourcedatafilepath</device>
<device>|SourceDB|#12!DestinationDB|#12!SourceLogicallogfilename|#12!Destinationlogfilepath|#12!Sourcelogfilepath.ld </device>

all fields needs to filled accordingly...if we have more than one data file...then it hsould populate n number of device tags....

Please help me on this...thanks....let me know if you have any questions...
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)

Group: General Forum Members
Points: 85384 Visits: 41078
I guess I'd have to ask why the 3rd party product doesn't already do this "auto-magically".

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Robin35
Robin35
SSChasing Mays
SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)

Group: General Forum Members
Points: 600 Visits: 1224
That is the problem here....we have that limitation....so trying to automate....
and also forgot to mention....we need to get source and destination database names, source logical data and log file names , Source and destination data and log file names ( physical )....
Orlando Colamatteo
Orlando Colamatteo
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14877 Visits: 14396
Try something like this instead:

Add-PSSnapin SqlServerCmdletSnapin100 -ErrorAction SilentlyContinue
Add-PSSnapin SqlServerProviderSnapin100 -ErrorAction SilentlyContinue

$searchTerm = "something"
# note the second entry is for a default instance so you would only change the word "server" and leave the "\default"
$databases = @{"server\instance"="db_name";"server\default"="db_name"}

foreach($database in $databases.Keys)
{
Set-Location ("SQLSERVER:\SQL\" + $database + "\Databases\" + $databases.Get_Item($database) + "\FileGroups")

foreach($FileGroup in Get-ChildItem){
foreach($File in $FileGroup.Files){
# in here you can build up any string you like using the logical file name and physical
# path, or whatever else, and push it into an XML file
"FileGroup=" + $FileGroup.DisplayName + ". FileName=" + $File.FileName + ". FileID=" + $File.ID
}
}
}


__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search