【问题标题】:How to pass Windows credential in a PowerShell script?如何在 PowerShell 脚本中传递 Windows 凭据?
【发布时间】:2018-12-31 09:52:35
【问题描述】:

我正在编写一个 PS 脚本以在 Chrome 浏览器中自动打开一个 URL。我创建了一个凭证对象并将其传递给Start-Process,如下所示。

$username = Read-Host 'What is your username?'
$password = Read-Host 'What is your password?' -AsSecureString
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
Start-Process -FilePath Chrome -ArgumentList $url -Credential $credentials

我希望 Chrome 浏览器通过使用凭证对象的 URL 打开。但它抛出的错误如下。

Start-Process : 由于以下错误无法执行此命令:登录失败:未知用户名或密码错误。

注意:如果我手动将 Windows 安全凭据传递给 URL,则该 URL 可以正常工作,因此我的凭据很好。感觉在传递 Windows 安全凭据时出现问题。不知道怎么回事。

【问题讨论】:

  • “手动将 Windows 安全凭据传递给 URL”到底是什么意思?您是否尝试使用不同的凭据运行 浏览器?或者您是否正在尝试针对网络服务器进行身份验证?您问题中的方法仅适用于前者。

标签: powershell windows-security start-process credential-manager


【解决方案1】:
$userName = "Pandiyan"
$secpasswd = ConvertTo-SecureString "mypasswordiscool" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secpasswd
$url = "localhost/atomicscope"
Start-Process -FilePath "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -ArgumentList $url -Credential $mycreds

您还应该指定 chrome 的路径,然后再试一次。而不是 readhost 直接提供凭据,它应该立即触发 chrome。如果有用户等待在控制台中输入,那很好。否则,使用 powershell 自动执行此操作是没有用的

【讨论】:

    【解决方案2】:

    您已将密码作为安全字符串读取,因此在创建凭证对象时无需再次执行此操作。仅当$password 包含明文密码时才需要ConvertTo-SecureString

    这会做你想做的事:

    $credentials = New-Object Management.Automation.PSCredential $username, $password
    

    【讨论】:

      猜你喜欢
      • 2016-09-25
      • 2022-01-08
      • 2015-08-10
      • 1970-01-01
      • 2021-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多