0

我正在尝试使用以下脚本获取数据和日志文件信息,并根据以下 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

我能够得到输出...但是如果一个数据库有多个数据文件...那么我只得到一个数据文件...请帮助我...

提前致谢...

4

1 回答 1

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 =真

您应该能够在您的 ps cmdline 中执行此操作(并且它不会失败) set-Location Sqlserver:\ 或 Get-PSProvider 应该返回包含 sqlserver 的行

这意味着您可以以 rest/directory 样式询问 sqlserver

第 2 步: cd SQLSERVER:\SQL\YOURSQLSERVERNAME\default\databases\YOURDATABASENAME\logfiles 替换 YOURDATABASENAME 和 YOURSQLSERVERNAME(通常是您的计算机名)

现在你可以运行 dir 这个“目录”或 dir | 选择文件名(目录)。文件名

于 2013-05-17T11:27:26.350 回答