假设我有两个数据库服务器。
我有以下脚本
Function Query1($Query1) {
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=db1.com;Initial
Catalog=Report Server;Integrated Security=SSPI"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.Connection = $SqlConnection
$SqlCmd.CommandText = $Query1
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$a=$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$DataSet.Tables[0]}
Function Query2($Query2) {
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=db2.com;Initial
Catalog=Report Server;Integrated Security=SSPI"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.Connection = $SqlConnection
$SqlCmd.CommandText = $Query2
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$a=$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$DataSet.Tables[0]}
$Result1 = Query1 "SELECT [InstanceName]
,[ItemPath]
,ca.name as ObjectName
,(case
when ca.type = 1 then 'Folder'
when ca.type = 2 then 'Report'
when ca.type = 3 then 'Resources'
when ca.type = 4 then 'Linked Report'
when ca.type = 5 then 'Data Source'
when ca.type = 6 then 'Report Model'
when ca.type = 7 then ' Report Part (SQL 2008 R2, unverified)'
when ca.type = 8 then 'Shared Dataset (SQL 2008 R2)'
when ca.type = 13 then 'PowerBI Report'
End ) as ObjectType
,[UserName]
,[ExecutionId]
,[RequestType]
,[Format]
,[Parameters]
,[ItemAction]
,[TimeStart]
,[TimeEnd]
,[TimeDataRetrieval]
,[TimeProcessing]
,[TimeRendering]
,[Source]
,[Status]
,[ByteCount]
,[RowCount]
,[AdditionalInfo]
FROM [dbo].[$reportServerView] e
join Catalog ca
on ca.Path = e.ItemPath where ItemAction = 'ConceptualSchema'or ItemAction
= 'QueryData'";
$Result1 | Export-Csv -Path $file_Path\file.csv -NoTypeInformation
$Result2 = Query2 "SELECT [InstanceName]
,[ItemPath]
,ca.name as ObjectName
,(case
when ca.type = 1 then 'Folder'
when ca.type = 2 then 'Report'
when ca.type = 3 then 'Resources'
when ca.type = 4 then 'Linked Report'
when ca.type = 5 then 'Data Source'
when ca.type = 6 then 'Report Model'
when ca.type = 7 then ' Report Part (SQL 2008 R2, unverified)'
when ca.type = 8 then 'Shared Dataset (SQL 2008 R2)'
when ca.type = 13 then 'PowerBI Report'
End ) as ObjectType
,[UserName]
,[ExecutionId]
,[RequestType]
,[Format]
,[Parameters]
,[ItemAction]
,[TimeStart]
,[TimeEnd]
,[TimeDataRetrieval]
,[TimeProcessing]
,[TimeRendering]
,[Source]
,[Status]
,[ByteCount]
,[RowCount]
,[AdditionalInfo]
FROM [dbo].[$reportServerView] e
join Catalog ca
on ca.Path = e.ItemPath where ItemAction = 'ConceptualSchema'or ItemAction
= 'QueryData'";
$Result2 | Export-Csv -Path $file_Path\file.csv -NoTypeInformation
第二个查询将覆盖csv文件中第一个查询结果的结果。我需要两个查询都将其追加到同一文件,但是,我真的不想使用append,因为每次我运行脚本时,我都希望文件被新数据覆盖,因为每天都会运行
答案 0 :(得分:2)
您使用的是Powershell v3还是更高版本? 如果是这样,您应该能够按原样进行第一个查询,对于第二个查询,请使用“ -Append”添加到文件中。
但是,这将使您在文件中间出现第二个标题行。 为避免这种情况,您可能需要添加
| Select-Object -Skip 1 |
答案 1 :(得分:0)
现在我考虑一下...我可以在第二个查询上使用-Append ...因为第一个查询将始终重新创建文件,并且仅第二个查询需要追加到现有查询中!
$Result1 | Export-Csv -Path $file_Path\file.csv -NoTypeInformation
$Result2 | Export-Csv -Path $file_Path\file.csv -NoTypeInformation -append