首页 > 解决方案 > Get-Content -Path 在 Certify (Lets Encrypt) 运行的脚本中不起作用,但在 powershell 中运行脚本时运行良好

问题描述

我的问题是我制作了一个启动 Exchange shell PSsession 的脚本。如果我在 PowerShell 中逐行执行脚本,或者在资源管理器中右键单击它并运行,脚本运行良好。但是,在生成新证书后通过 certify 调用它时会失败。

这是脚本的部分:

$password = Get-Content -Path 'c:\Certificate_Update\securepassword.txt'
$pw = ConvertTo-SecureString -String $password
#$pw = ConvertTo-SecureString -AsPlainText -Force -String "admin pass here"

$cred = New-Object System.Management.Automation.PSCredential ("Wookies-Domain\Administrator", $pw)
$uri = 'http://Exchange-Server/PowerShell/'
# Starts remote Exchange shell session
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $uri -Authentication Kerberos -Credential $Cred

# Imports remote Exchange shell session to this Machine
Import-PSSession $Session

我得到的错误是:

ConvertTo-SecureString : 系统找不到指定的路径。

在 C:\Certificate_Update\Update_Old_Cert.ps1:40 char:7
+ $pw = ConvertTo-SecureString -String $password
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [ConvertTo-SecureString], CryptographicException
    + FullyQualifiedErrorId : ImportSecureString_InvalidArgument_CryptographicError,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand

TerminatingError(New-Object):“异常调用“.ctor”与“2”参数:
“无法处理参数,因为参数“密码”的值为空。
将参数“密码”的值更改为非空值。""

新对象:使用“2”参数调用“.ctor”的异常:“无法处理
参数,因为参数“密码”的值为空。更改值
将“密码”参数设置为非空值。”

它是说$password是空的?无法弄清楚我做错了什么。当脚本由certify运行时,它可能是一些权限吗?

标签: powershellcertify

解决方案


我的脚本正在调用一个文件,该文件使用加密的标准字符串作为密码。这是作为管理员加密的。Certify 作为设置为本地系统的服务运行。因此,当脚本尝试访问密码文件时,由于权限错误而失败。将服务设置为以管理员身份运行可以解决问题。

感谢 Ansgar Wiechers 帮助我解决问题。


推荐阅读