I need to get the instance names and database names in a SQL 2008R2 server into a CSV file using PowerShell for running my backup script.
Desired Format.
Instance,Database
Instance1,Database1
Instance1,Database2
Instance2,Database1
Instance2,Database2
I hope my requirement is clear.
I m currently using
import-modulesqlps
cd
SQLServer:\SQL\Localhost
$instances
=Get-childitem
foreach($instancein$instances)
{
$instancename=$instance.name
$path="sqlserver:\sql\$($instance.name)\databases"
get-childitem-Force-path$path|Select-ObjectName,$instancename|Export-csvj:\instance_4.csv-NoTypeInformation-append-force
}