所以我的脚本差不多了,但我在插入带引号的变量时遇到了麻烦。
以下是代码,具体请看$sqlresult
。我用单引号将它显示出来,所以显然它只是作为文本输入字符串error[0]
,我希望让它调用变量和索引的那一部分。
#error handle
if ($error)
{
#Assemble the Query
$sqlresult = '$error[0]'
$sqlcontext = ($strfilename + ' ' + $content.length + ' bytes')
$strquery =
"INSERT INTO [logs].[dbo].[EventLog] (SourceID, Started, Completed, Result, Context, Machine)
values (50,'$starttime','$endtime','$sqlresult','$sqlcontext','1sl-den-db03-CAFEEDSCRIPT')"
#Create Command and Execute.
$sqlCommand = $sqlConnection.CreateCommand()
$sqlCommand.CommandText = $strQuery
$sqlCommand.ExecuteNonQuery()
双引号使脚本返回
Exception calling "ExecuteNonQuery" with "0" argument(s): "Incorrect syntax near 'calling'.
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a
semicolon."
At line:40 char:5
+ $sqlCommand.ExecuteNonQuery()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SqlException
我基本上已经学会了#哟哟,你的sql语句是有效的!"
我的猜测是我必须在查询中使用sqlresult变量做一些事情?我不确定。
答案 0 :(得分:0)
$Error[0]
总是有回报,前提是它不是空的,也许你要找的是$sqlresult = $Error[0].Exception.Message
如果您在catch块中处理此问题,可以执行$sqlresult = $_.Exception.Message