我正在尝试构建提取有关SQL Server实例的信息的函数,并使用以下信息更新表:
Import-Module "sqlps" -DisableNameChecking
. "D:\V1\CMS\Get-SQLInstance.ps1"
$serverName = 'VIRTSQL120S'
$tableName = 'SQL_SERVER_PRD'
function Update-Table ($srv, $tableName) {
$get_sql_info = Get-SQLInstance -Computername $serverName
foreach ($serverinstance in $get_sql_info) {
$connectionstring=$serverinstance.FullName
$instance=$serverinstance.SQLInstance
$version=$serverinstance.Version
$edition=$serverinstance.edition
$caption=$serverinstance.Caption
$port=$serverinstance.Instance_Port
Invoke-Sqlcmd -ServerInstance 'VIRTSQL152S' -Database 'SQL_info' -Query "
UPDATE $tableName SET instance = '$instance',
connection_name = '$connectionstring',
version = '$version',
edition = '$edition',
caption = '$caption',
port = '$port'
WHERE servername = '$serverName';"
}
}
Update-Table $serverName, $tableName`
我得到的错误是:
Invoke-Sqlcmd:关键字“SET”附近的语法不正确。 在行:19 char:13 + Invoke-Sqlcmd -ServerInstance'VIRTSQL152S'-Database>'SQL_info'-Qu ... +> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidOperation:(:) [Invoke-Sqlcmd],> SqlPowerShellSqlExecutionException + FullyQualifiedErrorId:> SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
如果我在函数外执行它,它没有问题。有什么帮助吗?
感谢。
我尝试使用查询变量,错误是相同的:
$ query =“UPDATE $ tableName SET instance ='$ instance',`
connection_name ='$ connectionstring',`
version ='$ version',`
edition ='$ edition',`
caption ='$ caption',`
port ='$ port'`
WHERE servername ='$ serverName';“