【问题标题】:PowerShell script to connect to multiple accounts用于连接到多个帐户的 PowerShell 脚本
【发布时间】:2022-08-24 21:43:56
【问题描述】:

我需要在多个 office365 帐户上运行脚本,因此我在一个文件中拥有用户名(电子邮件)和在另一个文件中的密码(所有密码相同)。但我收到此错误消息,并且我没有在帐户上启用 2FA。

Connect-MsolService:身份验证错误:无法完成身份验证请求(可能是代理问题)

这就是整个脚本:

    $passwordFile = \"C:\\password.txt\"
$username = \"C:\\users.txt\"

# First time create password file
if (! (Test-Path $passwordFile))
{
  Read-Host -AsSecureString | convertfrom-securestring | out-file $passwordFile
}
$password = ConvertTo-SecureString -String $passwordFile -AsPlainText -Force

$credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password

$licensedUsers = Connect-MsolService -Credential $credential

$licensedUsers  = Get-MsolUser -All | ? { $_.UserType -ne \"Guest\" }

$Report = [System.Collections.Generic.List[Object]]::new() # Create output file
Write-Host \"Processing\" $licensedUsers.Count \"accounts...\" 
ForEach ($User in $licensedUsers) {
    $MFAEnforced = $User.StrongAuthenticationRequirements.State
    $MFAPhone = $User.StrongAuthenticationUserDetails.PhoneNumber
    $DefaultMFAMethod = ($User.StrongAuthenticationMethods | ? { $_.IsDefault -eq \"True\" }).MethodType
    If (($MFAEnforced -eq \"Enforced\") -or ($MFAEnforced -eq \"Enabled\")) {
        Switch ($DefaultMFAMethod) {
            \"OneWaySMS\" { $MethodUsed = \"One-way SMS\" }
            \"TwoWayVoiceMobile\" { $MethodUsed = \"Phone call verification\" }
            \"PhoneAppOTP\" { $MethodUsed = \"Hardware token or authenticator app\" }
            \"PhoneAppNotification\" { $MethodUsed = \"Authenticator app\" }
        }
    }
    Else {
        $MFAEnforced = \"Not Enabled\"
        $MethodUsed = \"MFA Not Used\" 
    }
  
    $ReportLine = [PSCustomObject] @{
        User        = $User.UserPrincipalName
        Name        = $User.DisplayName
        MFAUsed     = $MFAEnforced
        MFAMethod   = $MethodUsed 
        PhoneNumber = $MFAPhone
    }
                 
    $Report.Add($ReportLine) 
}

Write-Host \"Report is in c:\\temp\\MFAUsers.CSV\"
$Report | Select User, Name, MFAUsed, MFAMethod, PhoneNumber | Sort Name | Out-GridView
$Report | Sort Name | Export-CSV -NoTypeInformation -Encoding UTF8 c:\\temp\\MFAUsers.csv
  • 您将密码文件提供给转换命令,您需要获取密码内容

标签: powershell script


【解决方案1】:

您需要从文件中“读取”密码。
这是一个例子。

$password = Get-Content $passwordFile| ConvertTo-SecureString

【讨论】:

  • 嗨,我已将第一部分修改为: $Secure = Read-Host -AsSecureString $Encrypted = ConvertFrom-SecureString -SecureString $Secure -Key (1..16) $Encrypted | Set-Content Encrypted.txt $Secure2 = Get-Content Encrypted.txt | ConvertTo-SecureString -Key (1..16) $username = "C:\script\user.txt" $credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $Secure2 现在我明白了错误:Connect-MsolService:身份验证错误:无法完成身份验证请求(可能是代理问题)
  • 我设法解决了登录问题,但现在如何遍历 txt 文件中的用户?
  • 你想对循环中的用户做什么?
【解决方案2】:

我的脚本有类似的问题,它显示相同的错误消息。您如何解决身份验证问题?这是我收到的错误消息。

Connect-Msolservice:身份验证错误:无法完成身份验证请求(可能是代理问题)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 2019-04-30
    • 2022-10-24
    • 2017-05-02
    • 1970-01-01
    相关资源
    最近更新 更多