我正在尝试使用以下脚本获取数据和日志文件信息,并根据以下 xml 节点排列输出。
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 = "select f.physical_name from sys.master_files f, sys.databases d where f.database_id = d.database_id and
d.name = 'master'"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter.SelectCommand = $SqlCmd
$SqlAdapter.Fill($DataSet)|out-null
foreach ($Row in $Dataset.Tables[0].Rows)
{
$dev1 = $($Row[0])
$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($dev1.EndsWith(".mdf") -and $($Row[1]).EndsWith(".mdf"))
{
$dev = "<device>|" + $Sourcedatabase + "|#12!" + $DESTINATIONDB
$dev = $dev + "|#12!" + $($Row[0]) +"|#12!"
$dev = $dev + $dev1 +"|#12!"
$dev = $dev + $($Row[1])
$dev = $dev + "</device>"
$dev
}
if($dev1.EndsWith(".ldf") -and $($Row[1]).EndsWith(".ldf"))
{
$dev = "<device>|" + $Sourcedatabase + "|#12!" + $DESTINATIONDB
$dev = $dev + "|#12!" + $($Row[0]) +"|#12!"
$dev = $dev + $dev1 +"|#12!"
$dev = $dev + $($Row[1])
$dev = $dev + "</device>"
$dev
}
}
}
输出应该像
|test_restore|#12!testCDC|#12!test_replication|#12!D:\MSSQL\DATA\MSSQL10.MSSQLSERVER\MSSQL\DATA\master.mdf|#12!D:\MSSQL\DATA\Test _Restore.mdf | test_restore|#12!testCDC|#12!test_replication_log|#12!D:\MSSQL\DATA\MSSQL10.MSSQLSERVER\MSSQL\DATA\mastlog.ldf|#12!L:\MSSQL\LOG\ Test_Restore_log.ldf
我能够得到输出...但是如果一个数据库有多个数据文件...那么我只得到一个数据文件...请帮助我...
提前致谢...