Retrieve data and log file names from SQL database

  • 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...

  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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 )....

  • 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

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply