如何解决从子脚本连接两个Azure Analysis Services时出现错误
我正在使用PowerShell脚本。在这些脚本中,我有两个脚本,我正在与两个Azure Analysis Server一对一地连接。这些脚本由主脚本调用。我遇到错误
"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。我正在分享此信息,可能是一个问题。
解决方法
似乎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
,
$command = "C:\Users\User1\Desktop\test1.ps1 –envName1 asazure://aspaaseastus2.asazure.windows.net/mydevaas -toBeDisconnect1 No"
仔细查看envName1
之前的连字符。实际上是-(连字符)而不是-(连字符)。这就是未传递envName1的原因。第二个参数toBeDisconnect1
正确。
如何知道区别? -(连字符)短于–(连字符):)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。