Iterate server list with dbatools?

944 Views Asked by At

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/

1

There are 1 best solutions below

2
On BEST ANSWER

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}