使用PowerShell使用read-host将密码分配给Word Doc

发布于 2025-02-07 17:53:35 字数 2154 浏览 2 评论 0原文

我有以下脚本可以使用自动化的32个字符密码更新密码。然后,它可以制作一个受密码保护的Word Doc,以便我们可以更新我们的安全密码库。生成密码可以正常工作。

当我尝试将密码分配给受密码保护的Word Doc时,就会发生问题。我可以直接硬编码字符串,例如:

$ pl_document.password ='blah'

在我硬代码的情况下,一切正常。我获得了带有登录信息的密码保护文档。

但是,当我尝试使用Read-host阅读它然后分配它时,脚本会悬挂。

Add-Type -AssemblyName System.Web

cls

#************ Create Document ******************************

function CreateDocument 
{

$PL_Word = New-Object -ComObject Word.Application

#$PL_Word.Visible = $true

$PL_Document = $PL_Word.Documents.Add()

$PL_Report = 'C:\TEMP\MyDoc.docx'

$PL_Document.SaveAs([ref]$PL_Report,[ref]$SaveFormat::wdFormatDocument)

$PL_Selection = $PL_Word.Selection

#****************** Password Protect the Word File ********

$PL_PwdEntry = Read-Host ("Enter the password for the text document record") -AsSecureString

$PL_WdPWD = ConvertFrom-SecureString $PL_PwdEntry

$PL_Document.Password =  $PL_WdPWD


#************** Write Password to Document *************************************


$PL_Selection.TypeParagraph()

$PL_Selection.TypeText("Username: $PL_UN")

$PL_Selection.TypeParagraph()

$PL_Selection.TypeText("Password: $PL_PWD")

#************** Close Document *************************************

$PL_Document.Close()

$PL_Word.Quit()
}

#****************** Create Password ************************

$PL_PWD = [System.Web.Security.Membership]::GeneratePassword(32,3)

Write-Host "`n`n"

$PL_UN = "Prime\"+(Read-Host ("Enter the username. Entering the Primelending domain is not neccessary."))

Write-Host "`nSummary of the change" -f Yellow

Write-Host "============================" -f Yellow

Write-Host "`nUsername: " -NoNewline

Write-Host "$PL_UN" -f Yellow

Write-Host "New Password: " -NoNewline

Write-Host "$PL_PWD`n" -f Yellow

Write-Host "Do you want to update AD (Y/N)" -NoNewline -f Yellow

$PL_Query = Read-Host (" ")


If ($PL_Query.ToUpper() -eq "Y") {
  
  Write-Host "`nMaking change" -f Green
  
  #Set-ADAccountPassword -Identity $PL_UN -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "$PL_PWD" -Force)
  
  CreateDocument
}
  
else {Write-Host "`nAbandoning change" -f Green } 

I have the following script to update passwords with an autogenerated 32 character password. It then makes a password-protected Word doc so that we can update our secure password library. Generating the password works fine.

The problem occurs when I try to assign a password to the password-protected word doc. I can hard code a string directly such as:

$PL_Document.Password = 'blah'

In the case where I hard code it everything works fine. I get a password protected word doc with the login info.

However, when I try to read it in using Read-host then assign it, the script hangs.

Add-Type -AssemblyName System.Web

cls

#************ Create Document ******************************

function CreateDocument 
{

$PL_Word = New-Object -ComObject Word.Application

#$PL_Word.Visible = $true

$PL_Document = $PL_Word.Documents.Add()

$PL_Report = 'C:\TEMP\MyDoc.docx'

$PL_Document.SaveAs([ref]$PL_Report,[ref]$SaveFormat::wdFormatDocument)

$PL_Selection = $PL_Word.Selection

#****************** Password Protect the Word File ********

$PL_PwdEntry = Read-Host ("Enter the password for the text document record") -AsSecureString

$PL_WdPWD = ConvertFrom-SecureString $PL_PwdEntry

$PL_Document.Password =  $PL_WdPWD


#************** Write Password to Document *************************************


$PL_Selection.TypeParagraph()

$PL_Selection.TypeText("Username: $PL_UN")

$PL_Selection.TypeParagraph()

$PL_Selection.TypeText("Password: $PL_PWD")

#************** Close Document *************************************

$PL_Document.Close()

$PL_Word.Quit()
}

#****************** Create Password ************************

$PL_PWD = [System.Web.Security.Membership]::GeneratePassword(32,3)

Write-Host "`n`n"

$PL_UN = "Prime\"+(Read-Host ("Enter the username. Entering the Primelending domain is not neccessary."))

Write-Host "`nSummary of the change" -f Yellow

Write-Host "============================" -f Yellow

Write-Host "`nUsername: " -NoNewline

Write-Host "$PL_UN" -f Yellow

Write-Host "New Password: " -NoNewline

Write-Host "$PL_PWD`n" -f Yellow

Write-Host "Do you want to update AD (Y/N)" -NoNewline -f Yellow

$PL_Query = Read-Host (" ")


If ($PL_Query.ToUpper() -eq "Y") {
  
  Write-Host "`nMaking change" -f Green
  
  #Set-ADAccountPassword -Identity $PL_UN -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "$PL_PWD" -Force)
  
  CreateDocument
}
  
else {Write-Host "`nAbandoning change" -f Green } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

临风闻羌笛 2025-02-14 17:53:35

$ pl_wdpwd = convertfrom-securestring $ pl_pwdentry脚本的一部分不会返回纯文本密码,而是密码的编码版本,其长度将比原始的长度更长密码。

从我记得的话来看,单词的密码限制为255个字符,该值可能会超过该字符,这可能是悬挂的原因,因为单词无法处理。

如果您使用的是PowerShell V7或更高版本$ pl_wdpwd = convertfrom-securestring $ pl_pwdentry $ pl_wdpwd = convertfrom-securestring $ pl_pwdentring $ pl_pwdentry-plaintry -asplaintry-asplaintry-asplaintext以获取普通文本的文本文本 或者,随着您需要立即使用纯文本密码

并尝试将其存储在单独的变量中时,只需获取 read-host cmdlet,通过更改返回您的纯文本密码:

$PL_PwdEntry = Read-Host ("Enter the password for the text document record") -AsSecureString
$PL_WdPWD = ConvertFrom-SecureString $PL_PwdEntry

编辑

$PL_WdPWD = Read-Host ("Enter the password for the text document record")

以下注释

在传递$ pl_wdpwd变量$ pl_document.password.password 是由于缺乏报价。

为了解决此问题,您可以更改此行

$ pl_document.password = $ pl_wdpwd

到此

$ pl_document.password =“ $ pl_wdpwd”

Strong>在更新的代码中,您仍在使用read-host -asecurestring,然后使用convertfrom-securestring将其转换。这不会为您提供您期望的密码,而是返回该密码的编码版本。有关此的详细信息,请参阅我的原始答案。

The $PL_WdPWD = ConvertFrom-SecureString $PL_PwdEntry part of your script will not be returning a plain text password and instead will be an encoded version of the password that will have a much longer length than that of the original password.

From what I recall Word has a password limit of 255 characters, which this value would likely exceed and is probably the cause of the hang as Word cannot handle it.

If you're using PowerShell v7 or above change $PL_WdPWD = ConvertFrom-SecureString $PL_PwdEntry to $PL_WdPWD = ConvertFrom-SecureString $PL_PwdEntry -AsPlainText in order to get the plain text version of the password

Alternatively, seeing as you're needing the plain text password straight away and trying to store it in a separate variable, just get the Read-Host cmdlet to return you a plain text password instead by changing:

$PL_PwdEntry = Read-Host ("Enter the password for the text document record") -AsSecureString
$PL_WdPWD = ConvertFrom-SecureString $PL_PwdEntry

to

$PL_WdPWD = Read-Host ("Enter the password for the text document record")

Edit following comments

It appears that Word hangs when passing the $PL_WdPWD variable to $PL_Document.Password is due to a lack of quotes.

In order to resolve this, you can change this line

$PL_Document.Password = $PL_WdPWD

to this

$PL_Document.Password = "$PL_WdPWD"

Note: in your updated code, you're still using Read-Host -AsSecureString and then converting it with ConvertFrom-SecureString. This will not be giving you the password you expect and instead return an encoded version of that password. See my original answer for details on this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文