【问题标题】:How to stop Powershell credential request from prompting in a long script?如何阻止 Powershell 凭据请求在长脚本中提示?
【发布时间】:2015-02-16 12:46:11
【问题描述】:

对 PowerShell 和 Office365 cmdlet 相当陌生。

目前我已经创建了一个脚本,它可以从我的 Office365 服务中获取所有电子邮件地址(大约 140,000 个)。在它使用 get-mailbox cmdlet 检索到所有电子邮件后,我使用 get-mailboxStatistics cmdlet 来获取 Last-Logon-Date。它将所有这些输出到一个 CSV 文件,所以我最终得到一个用户上次登录日期的电子邮件地址。

脚本当前正在按预期运行,但脚本运行后会随机提示 PowerShell 凭据请求。据我所见,它会完全随机提示,有时需要几个小时才能提示,有时只需要 10 分钟。该脚本计划每年运行一次,我不想每次都手动运行它并重新输入我的详细信息。

我环顾四周,找不到任何可以解决我的问题的方法。 Office 365 登录代码如下。

$password = ConvertTo-SecureString -String $arg -AsPlainText -Force
$msolcred = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $username, $password
connect-msolservice -credential $msolcred
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $msolcred -Authentication Basic -AllowRedirection 
    Import-PSSession $Session

注意事项: -我很确定我的连接没有丢失。
- 脚本很可能总共需要大约 10 个小时才能运行。

如果有人需要更多详细信息,请随时询问。

【问题讨论】:

    标签: powershell office365


    【解决方案1】:

    您可以使用的一种方法是在处理 X 条记录后重新连接。在下面的代码中,将在处理 1000 条记录后建立新的连接。

    # Save credential to a file
    Get-Credential | Export-Clixml credentialFile.xml
    
    # Load credential
    $credential = Import-Clixml credentialFile.xml
    
    # Set reconnect threshold
    $reconnectThreshold = 1000
    
    foreach($element in $array)
    {
        # Reconnect if threshold is reached
        if($processedCount -ge $reconnectThreshold)
        {
            # Close all sessions
            get-pssession | Remove-PSSession -Confirm:$false
    
            # Start a new session
            Import-PSSession $(New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $credential -Authentication Basic -AllowRedirection)
    
            # Reset processed counter
            $processedCount = 0
        }
    
        # Action on an item
        Action-Item $element
    
        # Increment processed counter
        $processedCount++
    }
    

    【讨论】:

      【解决方案2】:

      如果您通过调用 powershell.exe 来安排此操作,请使用 -NonInteractive 开关。这将引发错误而不是提示,但您大概可以Catch 并以编程方式处理。

      也可以在这里查看我的答案:https://stackoverflow.com/a/25172234/3905079

      【讨论】:

      • 我会将 -NonInteractive 添加到我的脚本中并尝试正确捕获它。我会让你知道我是怎么走的。谢谢!
      • 我运行了以下 powershell.exe -NoProfile -NonInteractive -File 'C:\myFile'。不幸的是没有运气。仍然收到提示。我在 Windows powershell 中运行它
      • 在我看来是 msol-service 提示您进行这些登录。
      【解决方案3】:
      $Username = "XXXX@XXXX.onmicrosoft.com"
      $password= "xxx@12345"
      $secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force 
      $creds = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, 
                $secureStringPwd
      

      【讨论】:

      • 虽然这段代码可能有用,但如果答案能解释它是如何工作的以及它如何解决问题会更好。
      猜你喜欢
      • 1970-01-01
      • 2021-12-01
      • 2012-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-24
      相关资源
      最近更新 更多