I have a sql query that outputs its result as a bit. I am trying to capture this output in a variable in PowerShell, and then use it in an IF/ELSE to decide the next step to run. The sql query is saved to a file and called using invoke-sqlcmd. I can't run this as a stored procedure.
The code snippet below always returns the else result "Create Mydb", and the Write-Output always displays 'You specified 0'. The database I am testing on should be returning a 1, so I don't think the result of the sql query is being set. I'd prefer to execute a .sql file rather than using -Query.
[int]$result = invoke-sqlcmd -Server $SQLServer -Database $DatabaseName -InputFile $InputFile
Write-Output "You specified $result"
If ($result.result -eq 1) {
write-host("Mydb database already exists")
}
else {
write-host("Create Mydb")
}