How to stop Powershell credential request from prompting in a long script?

hdifen

rather new to PowerShell and Office365 cmdlets.

Currently I've created a script which gets all the e-mail addresses from my Office365 service (around 140,000). After it has retrieved all the emails using get-mailbox cmdlet I use the get-mailboxStatistics cmdlet to get the Last-Logon-Date. It outputs all of this to a CSV file so I end up with an e-mail address that users last-logon-date.

The script is currently running as intended but a PowerShell Credential Request will randomly prompt after the script has been running. From what I've seen it will prompt completely randomly, sometimes it takes hours to prompt and other times it takes only 10 minutes. The script is meant to run about once a year which will be scheduled, I don't want to have to manually run it and re-enter in my details every time.

I've looked around and can't find anything to solve my problem. Office 365 Login Code below.

$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

Notes: -I'm pretty sure there is no loss of connection on my end.
-The script will most likely take around 10 hours in total to run.

If anyone needs further details feel free to ask.

Dino Padilla

One approach you can use is to reconnect after X number of records have been processed. In the code below a new connection will be made after 1000 records have been processed.

# 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++
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Passing credentials from one powershell script to another

来自分类Dev

How to write/get "long long int" to/from NSUserDefaults

来自分类Dev

how to write a script to read double quotes in powershell

来自分类Dev

How to stop UILabel from trimming the space at the end?

来自分类Dev

Linux how to stop ROS script that starts after reboot

来自分类Dev

Year wrong using Powershell script to pull dates from AD

来自分类Dev

How to remove xmlns="" from xml request

来自分类Dev

How to stop the Console from pausing my Threads and Tasks?

来自分类Dev

How can I stop controls from using a KeyEvent?

来自分类Dev

How can I stop myself from using 'git commit -a'?

来自分类Dev

How to stop UISearchBar from Scrolling with UITableview in iOS 8

来自分类Dev

How to stop ScrollView from scrolling when panning/zooming on a MapBox view?

来自分类Dev

How stop rpcbind from being started on Arch Linux?

来自分类Dev

How to remove script initialisation from javaScript file

来自分类Dev

How to test writing to a file share path using credential?

来自分类Dev

具有PSCredential的Powershell调用命令无法处理参数'Credential'上的参数转换

来自分类Dev

How can I request an Image from a URL when the Response is 302?

来自分类Dev

JAX-RS How to get a cookie from a request?

来自分类Dev

How to stop an alarm in android

来自分类Dev

How to stop at in IDLE breakpoint

来自分类Dev

How to make Loop stop

来自分类Dev

How to build-run vNext application from Windows Powershell?

来自分类Dev

Powershell script.exe消失了

来自分类Dev

How to stop the normal force from being too small on a 2D rigidbody? (Java 2D)

来自分类Dev

How to open xterm from script being runned by cron?

来自分类Dev

How to remove junk characters from the file generated by script command in linux

来自分类Dev

django country from request

来自分类Dev

授予IUSR使用PowerShell Stop-Process命令的权限?

来自分类Dev

在PowerShell中使用Stop-Process关闭特定网站

Related 相关文章

  1. 1

    Passing credentials from one powershell script to another

  2. 2

    How to write/get "long long int" to/from NSUserDefaults

  3. 3

    how to write a script to read double quotes in powershell

  4. 4

    How to stop UILabel from trimming the space at the end?

  5. 5

    Linux how to stop ROS script that starts after reboot

  6. 6

    Year wrong using Powershell script to pull dates from AD

  7. 7

    How to remove xmlns="" from xml request

  8. 8

    How to stop the Console from pausing my Threads and Tasks?

  9. 9

    How can I stop controls from using a KeyEvent?

  10. 10

    How can I stop myself from using 'git commit -a'?

  11. 11

    How to stop UISearchBar from Scrolling with UITableview in iOS 8

  12. 12

    How to stop ScrollView from scrolling when panning/zooming on a MapBox view?

  13. 13

    How stop rpcbind from being started on Arch Linux?

  14. 14

    How to remove script initialisation from javaScript file

  15. 15

    How to test writing to a file share path using credential?

  16. 16

    具有PSCredential的Powershell调用命令无法处理参数'Credential'上的参数转换

  17. 17

    How can I request an Image from a URL when the Response is 302?

  18. 18

    JAX-RS How to get a cookie from a request?

  19. 19

    How to stop an alarm in android

  20. 20

    How to stop at in IDLE breakpoint

  21. 21

    How to make Loop stop

  22. 22

    How to build-run vNext application from Windows Powershell?

  23. 23

    Powershell script.exe消失了

  24. 24

    How to stop the normal force from being too small on a 2D rigidbody? (Java 2D)

  25. 25

    How to open xterm from script being runned by cron?

  26. 26

    How to remove junk characters from the file generated by script command in linux

  27. 27

    django country from request

  28. 28

    授予IUSR使用PowerShell Stop-Process命令的权限?

  29. 29

    在PowerShell中使用Stop-Process关闭特定网站

热门标签

归档