I am currently working on a PowerShell script that interacts with a SQL Server database, at one point I need to see if there is a customer by the same name in the table, as such I am running the following query from PowerShell.
NB. Invoke-SQL
is a helper function which calls Invoke-SQLCMD
with specified parameters and allows database calls to be turned off with a global $Testing
flag
$Customercount = Invoke-SQL "SELECT Count(Cust_ID) FROM Customers WHERE Cust_Name LIKE '$CustName'"
This correctly returns an object which can be read with $Customercount.itemarray
however when this returns a 0 the matching fails as per the example below.
PS> $CustName = "Joe Bloggs"
PS> $Customercount = Invoke-SQL "SELECT Count(Cust_ID) FROM Customers WHERE Cust_Name LIKE '$CustName'"
PS> $Customercount.itemarray
0
PS> if ($Customercount.itemarray -eq 0) {Write-Host "equals 0"}
PS> if ($Customercount.itemarray -eq '0') {Write-Host "equals 0"}
PS> if ($Customercount.itemarray -eq "0") {Write-Host "equals 0"}
PS> if ($Customercount.itemarray -eq $Null) {Write-Host "equals 0"}
PS> $CustName = "Gael Test"
PS> $Customercount = Invoke-SQL "SELECT Count(Cust_ID) FROM Customers WHERE Cust_Name LIKE '$CustName'"
PS> $Customercount.itemarray
1
PS> if ($Customercount.itemarray -eq 1) {Write-Host "equals 1"}
equals 1
PS>
I feel like I am missing something fundamental here but I haven't been able to place it, can anyone shed light on what I'm missing