locked
Why is Enumeration Name property not working? RRS feed

  • Question

  • I have a script that i want to display the connection strings of a database

    Import-Module SqlServer
    $AS = New-Object Microsoft.AnalysisServices.Server
    $AS.connect("server1")

    Now if i use the FindByName() property

    $db = $AS.Databases.FindByName("database1")
    $db.DataSources[0].ConnectionString

    I get back the connection string successfully

    however if i use Enumerator

    foreach ($db in $AS.Databases.GetEnumerator())
    { $dbName = $db.Name
      $dbName
      $dbName.DataSources[0].ConnectionString
    }

    I get back the database name along with error/exception (because it couldnt get connection string for some reason):

    database1

    Cannot index into a null array.

    database2

    Cannot index into a null array.

    I tried the following also:

    $database1 = "database1"
    $database1.DataSources[0].ConnectionString

    and i also get back the same exception

    So why is it that only FindByName works?

    for additional info, this is what GetEnumerator lists:

    $AS.Databases.GetEnumerator()

    enumerator output

    but also $AS.Databases

    outputs the same thing...so whats even the point of the enumerator?

    gm -i $AS.Databases

    gmdb

    gm -i $AS.Databases..GetEnumerator()

    gmenu

    Wednesday, February 27, 2019 9:43 PM

Answers

  • Then there are no per-database connection strings.

    You can get feedback like this:

    foreach ($db in $AS.Databases){
      Write-Hst $db.Name -Fore green
      $db.DataSources | ForEach-Object{$_.ConnectionString}
    }
    I fixed one of your coding mistakes.  Avoid adding interim variables as they lead to this type of issue.


    \_(ツ)_/



    • Edited by jrv Wednesday, February 27, 2019 11:13 PM
    • Marked as answer by cataster Wednesday, February 27, 2019 11:26 PM
    Wednesday, February 27, 2019 11:12 PM

All replies

  • foreach ($db in $AS.Databases){
      $dbName = $db.Name
      #$dbName
      $db.DataSources | ForEach-Object{$_.ConnectionString}
    }
    PowerShell does its own enumerating.

    \_(ツ)_/




    • Edited by jrv Wednesday, February 27, 2019 11:12 PM
    Wednesday, February 27, 2019 10:49 PM
  • foreach ($db in $AS.Databases){
      $dbName = $db.Name
      #$dbName
      $dbName.DataSources | ForEach-Object{$_.ConnectionString}
    }
    PowerShell does its own enumerating.

    \_(ツ)_/



    interesting. however, that didn't output anything. 
    Wednesday, February 27, 2019 11:03 PM
  • Then there are no per-database connection strings.

    You can get feedback like this:

    foreach ($db in $AS.Databases){
      Write-Hst $db.Name -Fore green
      $db.DataSources | ForEach-Object{$_.ConnectionString}
    }
    I fixed one of your coding mistakes.  Avoid adding interim variables as they lead to this type of issue.


    \_(ツ)_/



    • Edited by jrv Wednesday, February 27, 2019 11:13 PM
    • Marked as answer by cataster Wednesday, February 27, 2019 11:26 PM
    Wednesday, February 27, 2019 11:12 PM
  • Then there are no per-database connection strings.

    You can get feedback like this:

    foreach ($db in $AS.Databases){
      Write-Hst $db.Name -Fore green
      $db.DataSources | ForEach-Object{$_.ConnectionString}
    }
    I fixed one of your coding mistakes.  Avoid adding interim variables as they lead to this type of issue.


    \_(ツ)_/



    Amazing! this did the trick!!!
    Wednesday, February 27, 2019 11:25 PM