我正在尝试使用以下脚本获取数据和日志文件信息,并按照以下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 \测试 _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
我能够获得输出......但如果数据库有多个数据文件......那么我只得到一个数据文件...... 请帮帮我...
提前感谢...
答案 0 :(得分:0)
我认为你这样做很难......你应该尝试正确使用powershell
步骤1:如果您使用的是sqlserver 2008
确保您已注册并添加了sql snappins: https://blogs.msdn.com/b/dtjones/archive/2011/03/31/loading-sql-server-snapins-into-your-powershell-session.aspx?Redirected=true
你应该可以在ps cmdline中做到这一点(并且它不会失败) set-Location Sqlserver:\ 要么 Get-PSProvider应该在其中返回一行包含sqlserver
这意味着您可以在休息/目录样式中查询sqlserver
第2步: cd SQLSERVER:\ SQL \ YOURSQLSERVERNAME \ default \ databases \ YOURDATABASENAME \ logfiles 替换YOURDATABASENAME和YOURSQLSERVERNAME(通常是您的computerName)
现在你可以运行dir这个“目录”了 要么 dir |选择FileName (DIR).FileName