Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Retrieve data and log file names from SQL database Expand / Collapse
Author
Message
Posted Wednesday, May 1, 2013 2:43 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: 2 days ago @ 9:23 AM
Points: 92, Visits: 738
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...
Post #1448615
Posted Wednesday, May 1, 2013 5:43 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:58 PM
Points: 36,794, Visits: 31,253
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1448650
Posted Wednesday, May 1, 2013 7:27 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: 2 days ago @ 9:23 AM
Points: 92, Visits: 738
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 )....
Post #1448659
Posted Thursday, May 2, 2013 5:25 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 11:45 PM
Points: 7,094, Visits: 12,582
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
Post #1449014
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse