转换为字符串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

但是在尝试将“如下转换为字符串”时遇到问题

enter image description here

我仅使用.txt文件

enter image description here

解决方法

只需添加-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