Powershell - 使用安全字符串屏蔽线

时间:2017-03-28 12:26:56

标签: powershell

要登录某些内容,我必须在第1页输入用户名,在第2页输入密码,然后在第3页点击提交。为了更容易,我写了一个PS脚本。我通过对密码进行硬编码来仔细检查它,并且成功了。

但是我不想硬编码我使用添加的$pass密码以及如何阅读密码。但是现在我运行脚本时输入的密码无效。

我想使用Get-Credentials,但不知道如何在其特定页面上传递用户名或密码。

我很感激任何建议,帮助我朝着正确的方向迈进。

$pass = Read-Host 'd\P What is your password?' -AsSecureString
[Runtime.InteropServices.Marshal]::PtrToStringAuto(
[Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass))
$IE = New-Object -ComObject InternetExplorer.Application
$URL = 'https://website/'
$IE.Visible = $true
$IE.Navigate($URL)
While ($IE.Busy -eq $true) {Start-Sleep -Milliseconds 1000} 
$ie.Document.getElementById('DATA').value = "P0523586"
$Submit = $ie.Document.getElementsByTagName('Input') | ? {$_.Type -eq "Submit"}
$Submit.click()
While ($IE.Busy -eq $true) {Start-Sleep -Milliseconds 1000} 
$ie.Document.getElementById('DATA').value = "$pass"
$Submit = $ie.Document.getElementsByTagName('Input') | ? {$_.Type -eq "Submit"}
$Submit.click()
While ($IE.Busy -eq $true) {Start-Sleep -Milliseconds 1000} 
$Submit = $ie.Document.getElementsByTagName('Input') | ? {$_.Type -eq "Submit"}
$Submit.click()

1 个答案:

答案 0 :(得分:1)

Get-Credential是一个很好的方法,因为你没有存储任何应该在脚本中保持安全的东西。

最简单的方法是将从Get-Credentials返回的对象分配给变量:

$credentials = Get-Credential

然后,您可以使用$credentials.UserName检索用户名:

PS> $credentials.UserName
myusername

$credentials.Password检索安全密码对象

PS> $credentials.Password
System.Security.SecureString

其他PowerShell命令可以使用它,但在将其发送到无法接受安全密码对象的进程之前,需要对其进行解码(使其不安全)。

要以纯文本格式检索密码,您可以使用GetNetworkCredential方法,如下所示:

PS> $credentials.GetNetworkCredential().Password
mypassword

在您的脚本中,您将在脚本开头添加$credentials = Get-Credential,并将用户名/密码提交到表单,如下所示:

$ie.Document.getElementById('DATA').value = $credentials.UserName

$ie.Document.getElementById('DATA').value = $credentials.GetNetworkCredential().Password