【问题标题】:Getting error when connecting two Azure Analysis Services from child scripts从子脚本连接两个 Azure 分析服务时出错
【发布时间】:2020-10-08 05:07:18
【问题描述】:

我正在处理 PowerShell 脚本。我在这些脚本中有两个脚本,我与两个 Azure 分析服务器一一连接。这些脚本由主脚本调用。我收到错误

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

我的脚本代码如下

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

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

我的 AzureAnalysis Services dll 版本是 13.0.4495.10。我正在分享此信息,可能是一个问题。

【问题讨论】:

    标签: powershell azure-analysis-services


    【解决方案1】:

    似乎Invoke-Expression 没有传递参数envName1。我宁愿通常像下面这样调用子脚本:

    $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 
    

    【讨论】:

    • 您将在何处以及如何存储子脚本返回的对象?
    • 我已经执行了你的建议。对于使用“1”参数调用“Connect”的 test2.ps1 异常,我收到如下错误:“对象引用未设置为对象的实例。”在 C:\Users\User1\Desktop\test2.ps1:13 char:5 + $server2.Connect($envName2) + ~~~~~~~~~~~~~~~~~~~~~~ ~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : NullReferenceException
    • 对于我添加的返回对象 | (管)最后如果你注意到了。而且,您可以在 envName1 值周围包含“(引号)吗?更新了答案。
    • 谢谢@Krishg,我可以看到你更新了答案,这是有道理的,但不知何故,当我使用相同的方式时,我得到了以下错误。在 line:1 char:149 + ... westus.asazure.windows.net/mydevbiaas" -toBeDisconnect1 "No" | $Obj1 + ~~~~~~ 表达式只允许作为管道的第一个元素。 + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline
    • 当您在末端移除管道或将第一部分包裹在 () 中时会发生什么。
    【解决方案2】:

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

    仔细查看envName1 之前的连字符。它实际上是 -(短划线)而不是 -(连字符)。这就是没有通过 envName1 的原因。第二个参数toBeDisconnect1 正确。

    如何知道区别? -(连字符)短于 -(短划线):)

    【讨论】:

      猜你喜欢
      • 2019-12-03
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      • 1970-01-01
      • 2014-03-25
      相关资源
      最近更新 更多