如何在长脚本中停止Powershell凭据请求提示?
对于PowerShell和Office365 cmdlet来说,这是一个全新的概念 目前,我已经创建了一个脚本,可以从我的Office365服务(大约140000)获取所有电子邮件地址。使用get-mailbox cmdlet检索所有电子邮件后,我使用get-mailboxStatistics cmdlet获取上次登录日期。它将所有这些输出到一个CSV文件,因此我最终得到了一个用户上次登录日期的电子邮件地址 脚本当前正在按预期运行,但在脚本运行后,PowerShell凭据请求将随机提示。据我所见,它会完全随机地提示,有时需要几个小时来提示,有时只需要10分钟。该脚本将按计划每年运行一次,我不想每次都手动运行并重新输入我的详细信息 我环顾四周,找不到任何解决我问题的办法。Office 365登录代码如下如何在长脚本中停止Powershell凭据请求提示?,powershell,office365,Powershell,Office365,对于PowerShell和Office365 cmdlet来说,这是一个全新的概念 目前,我已经创建了一个脚本,可以从我的Office365服务(大约140000)获取所有电子邮件地址。使用get-mailbox cmdlet检索所有电子邮件后,我使用get-mailboxStatistics cmdlet获取上次登录日期。它将所有这些输出到一个CSV文件,因此我最终得到了一个用户上次登录日期的电子邮件地址 脚本当前正在按预期运行,但在脚本运行后,PowerShell凭据请求将随机提示。据我所见
$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.exe来安排此操作,请使用
-非交互式开关。这将抛出一个错误,而不是提示,但这可能是您可以通过编程方式捕获和处理的
也可以在这里看到我的答案:您可以使用的一种方法是在处理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++
}
我将在脚本中添加-NonInteractive,并尝试正确捕获它。我会告诉你我怎么走的。谢谢我运行了以下powershell.exe-NoProfile-NonInteractive-File“C:\myFile”。不幸的是没有运气。仍然得到提示。我在Windows PowerShell中运行此代码在我看来,是msol服务提示您进行这些登录。虽然此代码可能很有用,但如果答案解释了它的工作原理以及如何解决问题,则会更好。
$Username = "XXXX@XXXX.onmicrosoft.com"
$password= "xxx@12345"
$secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,
$secureStringPwd