I have the following script that displays the connections of each database in the server
I want to create and export to csv the “server”, “database”, and “connection info” column headers, and basically output the server name,DBName,and connection information to corresponding column.
I looked into PSCustomObject
but i am not sure how to implement this exactly in my script. I have so far defined the pscustomobjects but not sure how they get exported to csv
write-host "`r`n Server | DataBase | Connection info `r`n" -foregroundcolor white -backgroundcolor darkyellow
Import-Module SqlServer
$oldAS = New-Object Microsoft.AnalysisServices.Server
$oldAS.connect("server1")
foreach ($db in $oldAS.Databases){
$compatibility_level = $db.CompatibilityLevel
if($compatibility_level -lt 1200)
{
$OLDdbName = $db.Name
Write-Host $OLDdbName -Fore green
[PSCustomObject]@{
DataBase = $OLDdbName
}
$db.DataSources | ForEach-Object{write-host "$($_.ConnectionString)";
[PSCustomObject]@{
"Connection Info" = $_.ConnectionString
}
}
}
else
{
$AS = new-Object Microsoft.AnalysisServices.Tabular.Server
$AS.Connect("server1")
foreach ($dbt in $AS.Databases | Where-Object{$compatibility_level -ge 1200} ){
$dbName = $dbt.Name
Write-Host $dbName
[PSCustomObject]@{
DataBase = $dbName
}
if(($dbt.model.datasources[0]).GetType().Name -match "ProviderDataSource")
{
write-host "$($dbt.model.datasources[0].ConnectionString)"
}
else {
#$dbt.model.datasources[0].ConnectionDetails.ToString(); #ToJson
write-host "$($dbt.model.datasources[0].Credential.ToString())"
[PSCustomObject]@{
"Connection Info" = "$($dbt.model.datasources[0].Credential.ToString())"
}
}
}
}
} | Export-Csv -Path .\CONNECTIONS_LOG.csv -NoTypeInformation -Append