2

I have created the following variable $cred:

$User = "sa"
$PWord = ConvertTo-SecureString -String "teste" -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord

Then, I open a connection with dbatools script.

$server1 = Connect-DbaInstance `
-SqlInstance '192.168.0.61\TESTE2017' `
-SqlCredential $cred`
-TrustServerCertificate;

But powershell still prompt asking for a credential:

enter image description here

Why?

mklement0
  • 382,024
  • 64
  • 607
  • 775
Potter
  • 434
  • 8
  • 21
  • 4
    I cant’t test to confirm right now, but think you need a space between the ```-SqlCredential $cred``` and the back-tick, otherwise Powershell treats it as part of the parameter value rather than as a line continuation character. – mclayton May 21 '23 at 16:26
  • 1
    @mclayton evidently. One of many good reason why not to use backticks for line continuations and use splatting instead – Santiago Squarzon May 21 '23 at 16:49
  • @mclayton I can't believe it was just that, thank you. – Potter May 21 '23 at 17:01
  • 1
    @SantiagoSquarzon- even more confusing is a trailing space *after* a backtick, especially if the IDE doesn’t show a visual indicator (I use notepad a lot). Having said that, I tend to prefer line continuations as opposed to splatting - just the way I grew up I guess :-). – mclayton May 21 '23 at 17:30
  • 1
    You included a space on each line before the backtick, why'd you omit it on that line? The dead give away was the entire trustservercertificate parameter being in the credential username field. – Doug Maurer May 21 '23 at 20:48

1 Answers1

1

There's great information in the comments; let me try to summarize:

PowerShell commands (pipelines) situationally require explicit line continuation using a line-ending ` (the so-called backtick, which is PowerShell's general escape character), notably when spreading distinct arguments across multiple lines.

  • By contrast, ` is not needed if a command's line is syntactically incomplete, because PowerShell then automatically looks for the command's completion on the next line; e.g.:

    # Example 1
    Get-ChildItem -File | # NO ` needed: | implies that the command continues
      Sort-Object Length -Descending
    
    # Example 2
    1..3 | ForEach-Object { # NO ` needed: { implies that the command continues
      1 + $_
    }
    
    # Example 3
    (Get-Date). # NO ` needed: . implies that the command continues
      Year
    

If and when you do need ` for line-continuation, the following strict rules apply:

  • The line-ending ` must NOT be followed by any additional characters - not even whitespace.

  • The line-ending ` must NOT be directly attached to the last argument to the line - make sure that it is preceded by a space.[1]

    • If you do accidentally directly attach ` to a (syntactically incomplete) last argument, it is (potentially) continued on the next line, including the newline, IF that newline starts with a non-whitespace character.

    • In your case, this meant that verbatim -TrustServerCertificate was appended to the stringified value of variable $cred, which resulted in a string value such as the following ($cred - uselessly - stringified to its type name):

      System.Management.Automation.PSCredential
      --TrustServerCertificate
      
      • It is this string value that became the argument to the -SqlCredential parameter, resulting in the symptom you saw.

[1] Technically, a space is not needed if the preceding argument is itself syntactically complete, but not only is it safe to always use a space, it also provides visual clarity.

mklement0
  • 382,024
  • 64
  • 607
  • 775