powershell - 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运行时,它可能是一些权限吗?
解决方案
我的脚本正在调用一个文件,该文件使用加密的标准字符串作为密码。这是作为管理员加密的。Certify 作为设置为本地系统的服务运行。因此,当脚本尝试访问密码文件时,由于权限错误而失败。将服务设置为以管理员身份运行可以解决问题。
感谢 Ansgar Wiechers 帮助我解决问题。
推荐阅读
- python - 近侧透视投影缺失数据轮廓图
- c++ - 为什么 QCustomPlot 在绘制大数据时速度太慢?
- python - 从 python 更新 postgres DB 中的 json 数据类型列
- powershell - ZPL/Powershell 打印问题 - M of N 文档处理
- c - 通过 C 中的库包装器自动进行函数重定向
- react-native - 修改 mapStateToProps 以实现通用访问
- apache-spark - Spark 按 Key 分组并对数据进行分区
- java - 从 JRE 7 或 8 升级到 JRE11 后,Java Swing JFrame 大小不同。如何使帧大小保持一致?
- asp.net-mvc - ASP.NET Core Razor Pages,有没有办法配置链接目录文件夹路径?
- perl - 从 tk perl 接口中的子例程传递变量