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参数,而不是遇到模糊匹配发现错误?
答案 0 :(得分:-1)
我也遇到了同样的情况,我无法将变量传递给 -ServerInstance 参数,但是当我编写主机输出时,它绝对正确并且是字符串类型变量。