4

This code does what I need. But I am trying to replace a call to Invoke-SqlCmd with either Windows Authentication or SQL Authentication on the same line using some kind of switch. Can someone help me with a more elegant way? I was thinking about Invoke-Expression but I don't get any good result out of it.

Function MyFunction{
    Param
    (
        [string]$S = "MyServer",
        [string]$U,
        [string]$P
    )
    # Find the SQL Engine version
    $Q = "select serverproperty('ProductVersion')"

    if($U)
    {
        $R = Invoke-Sqlcmd -ServerInstance $S -Query $Q -Username $U -Password $P -Verbose
        Write-Host "SQL Athenticated"
    }
    else
    {
        $R = Invoke-Sqlcmd -ServerInstance $S -Query $Q -Verbose
        Write-Host "Windows Athenticated"
    }
    return $R.column1
}

Here is my Invoke-Expression. It just doesn't work...

Invoke-Expression "$R = Invoke-Sqlcmd -ServerInstance $S -Query $Q $(if($true){""-Username $U -Password $P""})"

Here is the elegant solution I was looking for. Thanks to Mike:

Function MyFunction{
    Param
    (
        [string]$S = "xps15",
        [string]$U,
        [string]$P
    )
    # Find the SQL Engine version
    $Q = "select serverproperty('ProductVersion')"
    $auth=@{}
    if($U){$auth=@{UserName=$U;Password=$P}}
    $R = Invoke-Sqlcmd -ServerInstance $S -Query $Q -Verbose @Auth
    return $R.column1
}
3
  • does it have to be a single line solution? what about 2 lines? if so, first line will set a variable to the credentials desired, second line will be your invoke, utlizing the variable in the first line. I'm not the biggest fan of single line solutions for everything, it can make reading/interpreting more difficult. Commented Sep 10, 2015 at 21:59
  • So which code in your question doesn't work? The blob of code at the top or the Invoke-Expression bit at the end. If the later then why even mention the first blob? "It just doesn't work.." - in what way does it "just doesn't work?", do you get any error messages, if so why not post them. If you want folks to help you here then please learn how to ask a question because quite frankly I have no idea what you're asking: codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question - please don't waste our time. Commented Sep 10, 2015 at 23:34
  • 2
    This is actually a good question. Not wanting to express the Invoke-SQLCmd twice is natural, and there's a good PowerShell solution. Commented Sep 11, 2015 at 0:07

1 Answer 1

12

In my opinion the best solution here is called splatting (really). You build a hashtable with a set of parameters (potentially empty) and then use the splat operator (@) to present those arguments to the cmdlet.:

Function MyFunction{
    Param
    (
        [string]$S = "MyServer",
        [string]$U,
        [string]$P
    )
    # Find the SQL Engine version
    $Q = "select serverproperty('ProductVersion')"

    if($U)
    {
        $auth=@{UserName=$u;Password=$p}
        Write-Host "SQL Authenticated"
    }
    else
    {
    $auth=@{}
        Write-Host "Windows Authenticated"
    }
    $R = Invoke-Sqlcmd -ServerInstance $S -Query $Q -Verbose @Auth
    return $R.column1
}

See get-help about_splatting for more information.

Sign up to request clarification or add additional context in comments.

1 Comment

I wish I could upvote this one more than once!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.