Как экспортировать несколько строк данных в запросе sql в одну ячейку в формате CSV с сервера SQL с помощью Invoke-Sqlcmd? - PullRequest
0 голосов
/ 05 мая 2020

** Я могу легко экспортировать данные в файл csv, например: *

$Array = @()
$DateTime = (Get-Date).ToString('MM/dd/yyyy hh:mm:ss tt')
$sqlserver = "MyServer"

$name = Invoke-Sqlcmd -ServerInstance $sqlserver -Database Mydb  -Query "
 SELECT name FROM sys.databases
where name='ManagementReporter';" | Select * -ExcludeProperty RowError, RowState, Table, ItemArray, HasErrors | % {$_.name}


$size = Invoke-Sqlcmd -ServerInstance $sqlserver -Database Mydb  -Query "
SELECT 
      total_size_mb = CAST(SUM(size) * 8. / 1024 AS DECIMAL(8,2))
FROM sys.master_files WITH(NOWAIT)
WHERE database_id = DB_ID() -- for current db 
GROUP BY database_id;" | Select * -ExcludeProperty RowError, RowState, Table, ItemArray, HasErrors  | % {$_.total_size_mb}



$datafile = Invoke-Sqlcmd -ServerInstance $sqlserver -Database Mydb  -Query "
        SELECT DB_NAME() AS DbName, 
        name AS FileName, type_desc,
        size/128.0 AS CurrentSizeMB, 
        size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT)/128.0 AS FreeSpaceMB ,
        physical_name as [Database_File_Location]
        FROM sys.database_files
        where type_desc='ROWS'" | Select * -ExcludeProperty RowError, RowState, Table, ItemArray, HasErrors



$io = Invoke-Sqlcmd -ServerInstance $sqlserver -Database Mydb  -Query "
        SELECT mf.physical_name AS Name,  CAST(io_stall_read_ms/(1.0 + num_of_reads) AS NUMERIC(10,1)) AS [avg_read_stall_ms],
CAST(io_stall_write_ms/(1.0+num_of_writes) AS NUMERIC(10,1)) AS [avg_write_stall_ms],
CAST((io_stall_read_ms + io_stall_write_ms)/(1.0 + num_of_reads + num_of_writes) AS NUMERIC(10,1)) 
AS [avg_io_stall_ms]
FROM sys.dm_io_virtual_file_stats(null,null) AS fs
INNER JOIN sys.master_files AS mf WITH (NOLOCK)
ON fs.database_id = mf.database_id
AND fs.[file_id] = mf.[file_id]
where mf.physical_name LIKE '%ManagementReporter.mdf'
ORDER BY avg_io_stall_ms ;
" | Select * -ExcludeProperty RowError, RowState, Table, ItemArray, HasErrors 



# Creating custom object
$Object = New-Object PSObject
$Object | Add-Member -MemberType NoteProperty -name "DateTime" -value $DateTime
$Object | Add-Member NoteProperty -Name "Name" -Value $name
$Object | Add-Member NoteProperty -Name "Data Size" -Value $size
$Object | Add-Member NoteProperty -Name "Datafile " -Value $Empdetails
$Object | Add-Member NoteProperty -Name "IO " -Value $io

$Object
$Array += $Object


#Invoke-Sqlcmd -ServerInstance $sqlserver -Database Test -Query $io | Export-Csv $outfile -NoTypeInformation
If ($Array) { 
    $Array | Out-GridView -Title "Database Information"
    $Array | Export-Csv -Path "C:\Mypath\statdb.csv" -NoTypeInformation -Force -Append

}

Файл csv будет экспортировать данные, только запрос содержит только одну строку информации. Если есть несколько строк, это не может отображаться в ячейке. например, файл данных, io

Мой вывод в этом скрипте выше

...