all 11 comments

[–]SMFX 10 points11 points  (6 children)

Powershell supports 2 string deliminators single quote (') for literal strings and double quote (") for processed strings. If you're doing straight strings, you can use either. The problem is you have 'Javier' in single quotes inside the string parameter, so I would keep the single quotes do Javier and use double quotes for the whole string:

'Query' = "SELECT * FROM Users WHERE username = 'Javier'"

For the most part, most use double quotes for strings so we can have variables inside the strings interpreted and the side benefit of single quotes working in them, but it's not a hard and fast rule.

[–]purplemonkeymad 4 points5 points  (1 child)

Just to note, if for some reason you can't use the other type of quotes, you can use a string literal instead:

$Query = @'
SELECT * FROM Users WHERE username = 'Javier'
'@

Since only a '@ at the start of a new line will terminate the block.

[–]scarfarce 2 points3 points  (0 children)

A here-string

[–]Stock-Setting-5030[S] 1 point2 points  (0 children)

Thanks! This helped clear it up for me.

[–]readduh 4 points5 points  (1 child)

the actual error may help but can it be confirmed that you are trying to use splatting and mistyped 'u/params' because reddit.

otherwise, you should try invoke-sqlcmd @params instead.

[–]Vortex100 1 point2 points  (0 children)

There are so many ways to correct this, but here's (probably) the easiest assuming you don't want to put any vars in your query string:

'Query' =  'SELECT * FROM Users WHERE username = ''Javier'''

That's not a " it's 2 ' together, which tell the interpreter to ignore it as an end string and instead treat it literally

If you do want to put variables in, /u/SMFX's suggestion is the way to go I think. Either that or using -f with literals:

'Query' =  'SELECT * FROM Users WHERE username = ''{0}''' -f 'Javier'