Thanks Jonathan for your help…
it worked well…but i have hard coded the $dev value here…
[string]$dev = <device>|CAForms|#12!testCDC|#12!CAForms|#12!D:\MSSQL\DATA|#12!D:\MSSQL\DATA\CAForms.mdf</device>
<device>|CAForms|#12!testCDC|#12!CAForms_log|#12!L:\MSSQL\LOG|#12!D:\MSSQL\DATA\CAForms.ldf</device>
In reality…no one knows how many and what nodes $dev contains. We need to retrieve nodes from $dev dynamically. i have mentioned the nodes just to make you understand better.
Actual code is :
param
(
$ClientName,
$Sourcedatabase,
$DestServer,
$DESTINATIONDB
)
$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 = $DestServer; Database = ‘$DESTINATIONDB’; Integrated Security = True”
$SqlCmd.CommandText = "DECLARE @defaultDataLocation nvarchar(400)
DECLARE @defaultLogLocation nvarchar(400)
EXEC master.dbo.xp_instance_regread
N’HKEY_LOCAL_MACHINE’,
N’Software\Microsoft\MSSQLServer\MSSQLServer’,
N’DefaultData’,
@defaultDataLocation OUTPUT
EXEC master.dbo.xp_instance_regread
N’HKEY_LOCAL_MACHINE’,
N’Software\Microsoft\MSSQLServer\MSSQLServer’,
N’DefaultLog’,
@defaultLogLocation OUTPUT
SELECT @defaultDataLocation AS ‘Default Data Location’,@defaultLogLocation AS ‘Default Log Location’ "
$SqlCmd.Connection = $SqlConnection
$SqlAdapter.SelectCommand = $SqlCmd
$SqlAdapter.Fill($DataSet)|out-null
foreach ($Row in $Dataset.Tables[0].Rows)
{
$dev1 = $($Row[0])
$dev2 = $($Row[1])
$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 = $Sourcedatabase; Integrated Security = True”
$SqlCmd.CommandText = “select f.name ,f.physical_name from sys.master_files f, sys.databases d where f.database_id = d.database_id and
d.name = ‘$Sourcedatabase’”
$SqlCmd.Connection = $SqlConnection
$SqlAdapter.SelectCommand = $SqlCmd
$SqlAdapter.Fill($DataSet)|out-null
foreach ($Row in $Dataset.Tables[0].Rows)
{
if($($Row[1]).EndsWith(“.ldf”))
{
$dev = “<device>|” + $Sourcedatabase + “|#12!” + $DESTINATIONDB
$dev = $dev + “|#12!” + $($Row[0]) +“|#12!”
$dev = $dev + $dev2 +“|#12!”
$dev = $dev + $($Row[1])
$dev = $dev + “</device>”
$dev
}
else
{
$dev = “<device>|” + $Sourcedatabase + “|#12!” + $DESTINATIONDB
$dev = $dev + “|#12!” + $($Row[0]) +“|#12!”
$dev = $dev + $dev1 +“|#12!”
$dev = $dev + $($Row[1])
$dev = $dev + “</device>”
$dev
}
}
}
[xml]$temp = Get-Content \susq\prod\BAL\sqlserver\scripts\template_restore_old.xml
#($temp.xml.TMMsg_CreateTaskReq.taskinfo.subTasks.options.restoreOptions.sqlServerRstOption.device).Appendchild([string]$dev);
<#$device = $temp.CreateElement(“deviceâ€)
$device.InnerXml = “|CAForms|#12!testCDC|#12!CAForms|#12!D:\MSSQL\DATA|#12!D:\MSSQL\DATA\CAForms.mdfâ€
$temp.xml.TMMsg_CreateTaskReq.taskinfo.subTasks.options.restoreOptions.sqlServerRstOption.device.Appendchild($device)
$device = $temp.CreateElement(“Deviceâ€)
$device.InnerXml = “|CAForms|#12!testCDC|#12!CAForms_log|#12!L:\MSSQL\LOG|#12!D:\MSSQL\DATA\CAForms.ldfâ€
$temp.xml.TMMsg_CreateTaskReq.taskinfo.subTasks.options.restoreOptions.sqlServerRstOption.device.Appendchild($device
$temp.Save(“\susq\prod\BAL\sqlserver\scripts\template_restore_old.xml”)#>
If you can look at the above code…specifically last few lines of the code, you can see $dev has xml node information which we are going to append to xml file.
So, i’m trying to retrieve the $dev values dynamically and append it to xml file.
please take a look at the code and let me know your thoughts…
thank you in advance…