我不知道如何在 Invoke-Sqlcmd 中使用 Powershell 变量。
我在msdn 示例中看到:
$MyArray = "MyVar1 = 'String1'", "MyVar2 = 'String2'"
Invoke-Sqlcmd -Query "SELECT `$(MyVar1) AS Var1, `$(MyVar2) AS Var2;" -Variable $MyArray
,但似乎不是我需要的。而且我也无法理解示例,$MyArray = "MyVar1 = 'String1'"
是MyVar1
变量名String1
还是变量?填充到sql命令中会是SELECT MyVar1 = 'String1' AS Var1
;但是,是什么var1
意思呢?
我的代码:
$FirstDayOfLastMonth = (Get-Date -Day 1 -hour 0 -Minute 0 -Second 0).AddMonths(-1)
$LastDayOfLastMonth = (Get-Date -Day 1 -hour 23 -Minute 59 -Second 59).AddMonths(-1).AddDays([DateTime]::DaysInMonth($FirstDayOfLastMonth.year, $FirstDayOfLastMonth.month) - 1)
Write-Verbose "Invoke-Sqlcmd for winners"
Invoke-Sqlcmd -Query @"
use iMAG;
select L.Name AS LuckyDrawName, P.ParticipationTime, Z.Name as PrizeName, W.MobileNumber, T.ParticipantUniqueIdentifier
FROM LuckyDrawWinner W
INNER JOIN LuckyDrawParticipation P ON P.LuckyDrawParticipationId = W.LuckyDrawParticipationId
INNER JOIN LuckyDrawParticipant T ON T.LuckyDrawParticipantId = P.LuckyDrawParticipantId
INNER JOIN LuckyDrawPrize Z ON Z.LuckyDrawPrizeId = W.LuckyDrawPrizeId
INNER JOIN LuckyDraw L ON L.LuckyDrawId = Z.LuckyDrawId
WHERE cast(P.ParticipationTime AS date) < $LastDayOfLastMonth And cast(P.ParticipationTime AS date) > $FirstDayOfLastMonth
and L.LuckyDrawId IN (3) and Z.Grade <> 0
"@ | Export-Csv -Path $LuckyDrawWinnerReportFilePath -NoTypeInformation -Encoding "UTF8"
直接使用$LastDayOfLastMonth
是不行的。我也尝试cast($LastDayOfLastMonth AS date)
使用 Invoke-Sqlcmd 。
然后尝试$s = $FirstDayOfLastMonth.ToString("yyyy-MM-dd HH:mm:ss")
,然后使用cast($s AS date)
。两者都不起作用。
输出错误:
Invoke-Sqlcmd : Incorrect syntax near '23'.
At D:\ScheduledTasks\EmailReport\DailyEmailReport.ps1:32 char:14
+ Invoke-Sqlcmd <<<< -Query @"
+ CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
+ FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
我是powershell的新手,这个问题可能很简单,但我已经搜索了invoke-sqlcmd日期变量和其他密钥很长时间没有找到任何有用的东西。如果您能提供帮助,我将不胜感激。
更新:
我像 msdn 一样编辑我的代码:
$Vars="FirstDayOfLastMonth='$FirstDayOfLastMonth'", "LastDayOfLastMonth='$LastDayOfLastMonth'"
Invoke-Sqlcmd -Query @"
use iMAG;
select L.Name AS LuckyDrawName, P.ParticipationTime, Z.Name as PrizeName, W.MobileNumber, T.ParticipantUniqueIdentifier
FROM LuckyDrawWinner W
INNER JOIN LuckyDrawParticipation P ON P.LuckyDrawParticipationId = W.LuckyDrawParticipationId
INNER JOIN LuckyDrawParticipant T ON T.LuckyDrawParticipantId = P.LuckyDrawParticipantId
INNER JOIN LuckyDrawPrize Z ON Z.LuckyDrawPrizeId = W.LuckyDrawPrizeId
INNER JOIN LuckyDraw L ON L.LuckyDrawId = Z.LuckyDrawId
WHERE cast(P.ParticipationTime AS date) > $(FirstDayOfLastMonth) AND cast(P.ParticipationTime AS date) < $(LastDayOfLastMonth)
and L.LuckyDrawId IN (3) and Z.Grade <> 0
"@ -Variable $Vars | Export-Csv -Path $LuckyDrawWinnerReportFilePath -NoTypeInformation -Encoding "UTF8"
但事实证明:
The term 'FirstDayOfLastMonth' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try again.
At D:\ScheduledTasks\EmailReport\iDQDailyEmailReport.ps1:45 char:20
+ FirstDayOfLastMonth <<<<
+ CategoryInfo : ObjectNotFound: (FirstDayOfLastMonth:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : CommandNotFoundException