如何正确地将变量传递给invoke-sqlcmd -serverinstance参数? AmbiguousMatchException

时间:2014-11-17 17:04:38

标签: powershell

Import-Module sqlps  
@serverObjects = @()  
cd "some location"   #This location contains a list of serverName\instance  
$serverObjects += ls  
cd "another location" #This location contains another list of serverName\instance  
$serverObjects += ls 

此时$serverObjects.Name会返回所有serverName\instance的列表 示例运行:$serverObjects

server1\instanceA  
server2\instanceB

现在我想对serverName\instance中保存的每个$serverObjects运行一些SQL

foreach ($server in $serverObjects)  
{  
invoke-sqlcmd -inputfile "Direct path to file.sql" -serverinstance $server.Name   
}

返回以下错误:

 invoke-sqlcmd : Ambiguous match found.
At line:4 char:5  
+     invoke-sqlcmd -inputfile "Direct path to file.sql" -serverinstance ($s ...  
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
    + CategoryInfo          : InvalidResult: (serverName\instance:PSObject) [Invoke-Sqlcmd],AmbiguousMatchException  
    + FullyQualifiedErrorId : ExecutionFailed,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand  

如何将我的servernames \ instances作为变量传递给-serverinstance参数,而不是遇到模糊匹配发现错误?

1 个答案:

答案 0 :(得分:-1)

我也遇到了同样的情况,我无法将变量传递给 -ServerInstance 参数,但是当我编写主机输出时,它绝对正确并且是字符串类型变量。