Zum Inhalt springen

Azure SQL Export to CSV with powershell

$databases= ('DB01', 'Bb02')
 $destpath = 'C:\Export\'
 $ServerInstance = 'Servername.database.windows.net'
 $UserName = 'userman'
 $Password = 'password'
 get all tables from databases
 foreach ($database in $databases) {
 $tables = $null
 $query = 'select name from sys.tables where is_ms_shipped =0'
 $tables = Invoke-Sqlcmd -ServerInstance $ServerInstance -Database $database -Username $UserName -Password $Password -Query $query
     foreach ($table in $tables) {
         $filepath = $destpath +  $database +'-' + $table.name + '.csv'
         $query = 'select * from ' + $table.name
         Invoke-Sqlcmd -ServerInstance $ServerInstance -Database $database -Username $UserName -Password $Password -Query $query | Export-Csv -Path $filepath -NoTypeInformation
 <code>    } }
Schlagwörter: