Getting error when connecting two Azure Analysis Services from child scripts

159 views Asked by At

I am working on PowerShell scripts. I have two scripts in those scripts I am connecting with two Azure Analysis Servers one by one. And these scripts are calling by the main script. I am getting errors

"Exception calling "Connect" with "1" argument(s): "Object reference not set to an instance of an object."

My Scripts code is below

Child1.ps1

param(
    [String]
    $envName1,
    [String]
    $toBeDisconnect1
)

$loadInfo1 = [Reflection.Assembly]::LoadWithPartialName("Microsoft.AnalysisServices")
$server1 = New-Object Microsoft.AnalysisServices.Server

if($toBeDisconnect1 -eq "No")
{
    $server1.Connect($envName1)
    return $server1
}
elseif($toBeDisconnect1 -eq "Yes")
{
    $server1.Disconnect()
    Write-Host $server1 " has been disconnected."
}

Child2.ps1

param(
    [String]
    $envName1,
    [String]
    $toBeDisconnect1
)

$loadInfo1 = [Reflection.Assembly]::LoadWithPartialName("Microsoft.AnalysisServices")
$server1 = New-Object Microsoft.AnalysisServices.Server

if($toBeDisconnect1 -eq "No")
{
    $server1.Connect($envName1)
    return $server1
}
elseif($toBeDisconnect1 -eq "Yes")
{
    $server1.Disconnect()
    Write-Host $server1 " has been disconnected."
}

MainParent.ps1

param(
    $filePath = "C:\Users\user1\Desktop\DBList.txt"
)

$command = "C:\Users\User1\Desktop\test1.ps1 –envName1 
asazure://aspaaseastus2.asazure.windows.net/mydevaas -toBeDisconnect1 No"
$Obj1 = Invoke-Expression $command
Start-Sleep -Seconds 15

$command1 = "C:\Users\User1\Desktop\test2.ps1 –envName2 
asazure://aspaaseastus2.asazure.windows.net/myuataas -toBeDisconnect2 No"
$Obj2 = Invoke-Expression $command1

Error is below in MainParent.ps1

 Exception calling "Connect" with "1" argument(s): "Object reference not set to an instance of an 
 object."
 At C:\Users\User1\Desktop\test1.ps1:13 char:5
 +     $server1.Connect($envName1)
 +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
     + FullyQualifiedErrorId : NullReferenceException

 Exception calling "Connect" with "1" argument(s): "Object reference not set to an instance of an 
 object."
 At C:\Users\User1\Desktop\test2.ps1:13 char:5
        +     $server2.Connect($envName2)
        +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : NullReferenceException

My AzureAnalysis Services dll version is 13.0.4495.10. I am sharing this info, may be it can be a issue. enter image description here

2

There are 2 answers

10
krishg On BEST ANSWER

It seems Invoke-Expression is not passing the param envName1. I would rather call the child script normally like below:

$Obj1 = &"C:\Users\User1\Desktop\test1.ps1" -envName1 "asazure://aspaaseastus2.asazure.windows.net/mydevaas" -toBeDisconnect1 No | select -Last 1

$Obj2 = &"C:\Users\User1\Desktop\test2.ps1" -envName2 "asazure://aspaaseastus2.asazure.windows.net/myuataas" -toBeDisconnect2 No | select -Last 1 
0
krishg On

$command = "C:\Users\User1\Desktop\test1.ps1 –envName1 asazure://aspaaseastus2.asazure.windows.net/mydevaas -toBeDisconnect1 No"

Look closely at the hyphen before envName1. It's actually – (en-dash) instead of - (hyphen). That's the reason envName1 is not being passed. The second param toBeDisconnect1 has it correct.

How to know the difference? - (hyphen) is shorter than – (en-dash) :)