Powershell仍然提示要求凭证,即使使用System.Management.Automation.PSCredential。

huangapple go评论60阅读模式
英文:

Powershell still prompting for credential even when using System.Management.Automation.PSCredential

问题

我已经创建了以下变量 $cred

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

然后,我使用 dbatools 脚本打开了一个连接。

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

但是 PowerShell 仍然提示请求凭据。

英文:

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:

Powershell仍然提示要求凭证,即使使用System.Management.Automation.PSCredential。

Why?

答案1

得分: 1

以下是已经翻译好的内容:

有很多有用的信息在评论中;让我试着总结一下:

PowerShell 命令(管道)在*特定情况下*需要使用*明确的*行继续符,即所谓的反引号,它是 PowerShell 的[通用转义字符](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Special_Characters)(the so-called backtick),特别是当在多行中传递*不同的参数*时。

相比之下,如果命令的行在语法上不完整,则*不需要*使用 `` ` ``,因为 PowerShell 会自动查找命令的下一行来完成它;例如:

    # 示例 1
    Get-ChildItem -File | # 不需要 `:| 意味着命令继续
      Sort-Object Length -Descending

    # 示例 2
    1..3 | ForEach-Object { # 不需要 `:{ 意味着命令继续
      1 + $_
    }

    # 示例 3
    (Get-Date). # 不需要 `:. 意味着命令继续
      Year

如果您确实需要使用 `` ` `` 进行行继续,那么将适用以下*严格的规则*:

    * **行继续的 `` ` `` 后面*不能*跟随*任何*额外字符** - 即使是空格也不行。
 
    * **行继续的 `` ` `` 不能*直接附加*到行上的最后一个参数** - **确保它之前有一个空格**。<sup>[1]</sup>

    * 如果您不小心将 `` ` `` 直接附加到(语法上不完整的)最后一个参数上,它(可能)将在下一行继续,包括换行符,如果下一行以非空格字符开头。

在您的情况下,这意味着 *verbatim* `-TrustServerCertificate` 被附加到了变量 `$cred` 的*字符串化*值上,从而导致了一个字符串值,如下所示(`$cred` 被*无用地*字符串化为其*类型名称*):

     System.Management.Automation.PSCredential
     --TrustServerCertificate

正是这个字符串值成为 `-SqlCredential` 参数的参数,导致了您看到的症状。

---

<sup>[1] 从技术上讲,如果前面的参数本身*在语法上完整*,则不需要空格,但始终使用空格不仅安全,而且提供了视觉清晰度。<sup>
英文:

<!-- language-all: sh -->

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.<sup>1</sup>

    • 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.

<sup>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.</sup>

huangapple
  • 本文由 发表于 2023年5月21日 23:36:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300672.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定