首页 > 解决方案 > 转换为字符串 Powershell 问题

问题描述

我想像这样使用我的信用连接到 Exchange 在线:

$Username = "bigdaddy@love.onmicrosoft.com"
$PasswordPath = "C:\PowerShell\password.txt"

#Read the password from the file and convert to SecureString
$SecurePassword = Get-Content $PasswordPath | ConvertTo-SecureString
$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $SecurePassword

但是我在尝试转换为字符串时遇到了一个问题,如下所示

在此处输入图像描述

我只使用了 .txt 文件

在此处输入图像描述

标签: type-conversionpowershell-5.0

解决方案


只需添加-AsPlainText -Force

$Username = "bigdaddy@love.onmicrosoft.com"
$PasswordPath = "C:\PowerShell\password.txt"

#Read the password from the file and convert to SecureString
$SecurePassword = Get-Content $PasswordPath | ConvertTo-SecureString -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $SecurePassword

推荐阅读