When calling a cmdlet, is it possible to expand the value of a powershell variable somehow so it acts as a parameter (with associated values) for the cmdlet?
Here's an example of what I'm trying:
$CREDENTIALED_SECTION = "-Username $USER_NAME -Password $PASSWORD"
.
.
.
Invoke-Sqlcmd -ServerInstance "$SERVER_NAME" -Query "$SQL_STATEMENT" "$CREDENTIALED_SECTION" -Database "$DATABASE"
The problem comes when Invoke-Sqlcmd runs. It tells me that a positional parameter cannot be found that accepts "-Username my username
-Password my password
" So it's expanding the variable but not properly sending it as a set of parameters. Is there a way to do what I'm trying here?