title: Invoke-ASCmd to Deploy from Json File throws Error - Using Powershell and Azure Analysis Services with OAuth and Azure Data Lake
body: <p>Summary: I am using <strong>Invoke-ASCmd –InputFile "$templateLocation" -Server $serverName</strong> on PowerShell to deploy a model/table to Azure Analysis Services (AAS) server. The problem is that I get the error <strong>"Invoke-ASCmd : Index was outside the bounds of the array"</strong>.</p>
  
  <p>Details: I already have a database deployed to AAS server with OAuth and it has only one table. I want to add another table using a Json file template. I have done the same procedure using an on-premises Analysis Services server with Windows Authentication and a model created with SQL Server Tables - and it worked perfectly.</p>
  
  <p>Now the difference is that my AAS server requires OAuth authentication. Moreover, the model was created with tables that are txt files in a Azure Data Lake. Bottom line, I wanted to know how I can add a table to the existing AAS model <strong>without</strong> having to use credentials automation, runbooks, etc.</p>
  
  <p>Here is my PowerShell script.</p>
  
  <pre><code>###########################
  #Location of Templates
  ###########################
  $masterFolderLocation = "C:\Templates\"
  $templateLocation = $masterFolderLocation + 'MyModel.json' # xmla/json with one table to be added to model
  Write-Output "Template location:" $templateLocation
  
  ###########################
  #Variables
  ###########################
  $databaseName = "MyDatabase" # This database already exists in the AAS server
  $serverName = "asazure://aspaaseastus2.asazure.windows.net/myserver"; # Azure Analysis Services Server
  
  ###########################
  #Server Connection
  ###########################
  [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.AnalysisServices.Tabular")
  $svr = New-Object Microsoft.AnalysisServices.Tabular.Server
  $svr.Connect($serverName)
  
  ###########################
  #Count number of databases
  ###########################
  $database = $svr.databases[1]
  Write-Host "Number of Databases in the server:" $svr.databases[1].model.tables.count
  
  
  ###########################
  #Refresh Model
  ###########################
  Write-Host "Refreshing model..."
  $database = $svr.Databases[$databaseName]
  $model = $database.Model
  $model.RequestRefresh([Microsoft.AnalysisServices.Tabular.RefreshType]::Full)
  
  ###########################
  #Save Changes
  ###########################
  Write-Host "Saving model..."
  $database.Model.SaveChanges()
  
  ###########################
  #Deploy
  ###########################
  Invoke-ASCmd –InputFile "$templateLocation" -Server $serverName
  
  </code></pre>
  
  <p>Here is the error I get:</p>
  
  <pre><code>Invoke-ASCmd : Index was outside the bounds of the array.
  At C:\Users\StackOverflow.ps1:45 char:1
  + Invoke-ASCmd –InputFile "$templateTabular" -Server $serverName
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [Invoke-ASCmd], IndexOutOfRangeException
      + FullyQualifiedErrorId : System.IndexOutOfRangeException,Microsoft.AnalysisServices.PowerShell.Cmdlets.ExecuteScriptCommand
  </code></pre>
  
  <p>Here is my Json template (meant to be used to add only one table to already existing database/model)</p>
  
  <pre><code>{
    "create": {
      "parentObject": {
        "database": "MyDatabase"
      },
      "table": {
        "name": "Department",
        "columns": [
          {
            "name": "DepartmentId",
            "dataType": "int64",
            "isHidden": true,
            "sourceColumn": "DepartmentId",
            "summarizeBy": "none"
          },
          {
            "name": "Department",
            "dataType": "string",
            "sourceColumn": "Department"
          }
        ],
        "partitions": [
          {
            "name": "Partition",
            "dataView": "full",
            "source": {
              "type": "m",
              "expression": [
                "let",
                "    Source=GetFileList(),",
                "    #\"Department txt\" = Source{[Name=\"Department.txt\"]}[Content],",
                "    #\"Imported CSV\" = Csv.Document(#\"Department txt\",[Delimiter=\"|\", Columns=5, Encoding=65001, QuoteStyle=QuoteStyle.None]),",
                "    #\"Promoted Headers\" = Table.PromoteHeaders(#\"Imported CSV\", [PromoteAllScalars=true]),",
                "    #\"Changed Type\" = Table.TransformColumnTypes(#\"Promoted Headers\",{{\"DepartmentId\", Int64.Type}, {\"Department\", type text})",
                "in",
                "    #\"Changed Type\""
              ]
            }
          }
        ]
      }
    }
  }
  
  
  
  </code></pre>