0

I am really new to PowerShell and still learning so I am having a requirement to run some of the commands from dbatools and save the results.

$servers = 'E:\DBA\servers.txt'
$outfile = 'E:\DBA\out.csv'

Get-Content $servers | ForEach-Object {Invoke-Command DbaBackupHistory -SQLServer $_ | ConvertTo-CSV -NoTypeInformation | Select-Object -Skip 1 | Out-File -Append $outFile}

I am unsure if this is the correct way to doit https://dbatools.io/functions/get-dbabackuphistory/

Enrique
  • 153
  • 3
  • 13
  • 4
    Why was this question moved off of the DBA site. It is PowerShell and related to the dbatools module....which both deal with SQL Server, so it would fall under a DBA question. –  Sep 01 '17 at 16:05

1 Answers1

2

I modified you script and tested. Worked for me. I added 2 more switches to limit result set. -database and -lastfull. You can check documentation for details.

$outfile = 'c:\out.csv'

Get-Content c:\servers.txt|foreach-object {get-DbaBackupHistory -SqlServer $_  
-database dbadatabase -lastfull | ConvertTo-CSV -NoTypeInformation | 
Select-Object -Skip 1 | Out-File -Append $outFile}
SqlWorldWide
  • 329
  • 4
  • 20
  • Thanks, I could solve it a bit after that but no less I appreciate the help – Enrique Aug 31 '17 at 19:46
  • 2
    `Get-DbaBackupHistory` supports piping so you could just as easily do `Get-Content 'C:\servers.text | Get-DbaBackupHistory -Database DbaDatabase -LastFull | Select-Object -Skip 1 | ExportTo-Csv -NoClobber -NoTypeInformation -Path $outfile` –  Sep 01 '17 at 16:03