I wrote a powershell script that runs two SQL queries, saves the result in 2 variables and compares them. I receive an email if the results are different from one another.
However, I noticed that the results are always the same. When I Write-Host the two variables, I get this: System.Data.DataRow
. I assume that when I compare the two variables, I am actually comparing System.Data.DataRow
to System.Data.DataRow
.
我编写了一个运行两个SQL查询的powershell脚本,将结果保存在2个变量中并进行比较。如果结果彼此不同,我会收到一封电子邮件。但是,我注意到结果总是一样的。当我写这两个变量时,我得到了这个:System.Data.DataRow。我假设当我比较两个变量时,我实际上是将System.Data.DataRow与System.Data.DataRow进行比较。
The IF I use is quite simple:
我使用的IF非常简单:
if ($resultDB = $resultCube) {$SMTPClient.Send( $emailMessageSuccess )} ELSE {$SMTPClient.Send( $emailMessageFailure )}
Am I doing it wrong, or is there a way to store the actual value of an SQL query in a variable?
When I Write-Output the variables, I get:
我做错了,还是有办法将SQL查询的实际值存储在变量中?当我写输出变量时,我得到:
Column1
-------
993991
That number is what I am looking for.
这个号码正是我要找的。
1 个解决方案
#1
3
This happens, as assignment and comparison operators are mixed. In Powershell, =
is assignment and -eq
is equal comparison. Thus,
发生这种情况,因为赋值和比较运算符是混合的。在Powershell中,=是赋值,-eq是相等的比较。从而,
if ($resultDB = $resultCube) { ... }
is parsed as
被解析为
if $resultCube was assigned into $resultDB without any issues, do...
If you wonder how assignment could fail, consider read-only variables:
如果您想知道赋值如何失败,请考虑只读变量:
New-Variable -Name myConstant -Value 1
$myConstant
1
if($myConstant = 2) { "aye"} else { "nay" } # Try and assign new value
aye
$myConstant # It works
2
Remove-Variable -Name myconstant
New-Variable -Name myConstant -Value 1 -Option readonly # Now, let's try with a read-only variable
if($myConstant = 2) { "aye"} else { "nay" } # Kaboom!
Cannot overwrite variable myConstant because it is read-only or constant.
At line:1 char:4
+ if($myConstant = 2) { "aye"} else { "nay" }
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (myConstant:String) [], SessionStateUnauthorizedAccessException
+ FullyQualifiedErrorId : VariableNotWritable
As how to compare column values, try something akin
如何比较列值,尝试类似的东西
if ($resultDB["myColumn"] -eq $resultCube["myColumn"]) { ... }
#1
3
This happens, as assignment and comparison operators are mixed. In Powershell, =
is assignment and -eq
is equal comparison. Thus,
发生这种情况,因为赋值和比较运算符是混合的。在Powershell中,=是赋值,-eq是相等的比较。从而,
if ($resultDB = $resultCube) { ... }
is parsed as
被解析为
if $resultCube was assigned into $resultDB without any issues, do...
If you wonder how assignment could fail, consider read-only variables:
如果您想知道赋值如何失败,请考虑只读变量:
New-Variable -Name myConstant -Value 1
$myConstant
1
if($myConstant = 2) { "aye"} else { "nay" } # Try and assign new value
aye
$myConstant # It works
2
Remove-Variable -Name myconstant
New-Variable -Name myConstant -Value 1 -Option readonly # Now, let's try with a read-only variable
if($myConstant = 2) { "aye"} else { "nay" } # Kaboom!
Cannot overwrite variable myConstant because it is read-only or constant.
At line:1 char:4
+ if($myConstant = 2) { "aye"} else { "nay" }
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (myConstant:String) [], SessionStateUnauthorizedAccessException
+ FullyQualifiedErrorId : VariableNotWritable
As how to compare column values, try something akin
如何比较列值,尝试类似的东西
if ($resultDB["myColumn"] -eq $resultCube["myColumn"]) { ... }