Answered by:
Powershell loop til status completed

Question
-
Hi All,I want to export some mailbox to a .pst file and when status completed i want to remove the export request. A part of the script works, the pst file is created but in the loop of waiting till the status completed something is wrong. The loop never stops.I thought that when use WHILE and IF the WHILE loops until status is completed and then start the IF?Here the script and hope someone can help me :)
#Import-Module ActiveDirectory # Variabelen # Connect een Exchange powershell sessie in dit Powershell script $exchangeps = New-PSSession -ConfigurationName microsoft.exchange -ConnectionUri http: # Voer de naam van de gebruiker in die status disabled krijgt $gebruiker = Read-Host -Prompt 'Voer de gebruikersnaam in' # gebruik de ingevoerde waarde bij het opvragen van de gebruiker in AD $ad_user = Get-ADUser $gebruiker -Properties Surname, initials # Start hier de gemaakte Exchange sessie Import-PSSession $exchangeps # Start met export van de ingevoerde gebruiker en sla op met "achternaam en initialen.pst" New-MailboxExportRequest –Mailbox $ad_user.Name -filepath \\srvr\PST_exports\$($ad_user.SurName)$($ad_user.initials).pst # Start de loop om elke 30 seconden te bekijken wat de status is van de export $Exportstatus = Get-MailboxExportRequest -Mailbox $ad_user | Get-MailboxExportRequestStatistics WHILE ($Exportstatus.Status -notlike "Completed") { Get_MailboxExportRequest -Mailbox $ad_user | Get-MailboxExportRequestStatistics Write-Host "wachten..." Start-Sleep 10 cls } IF ($Exportstatus.Status -eq "Completed") { Write-Host "Export is klaar en de export taak zal worden opgeschoond" Get-MailboxExportRequest -Status Completed -Name "$ObjectName-Export" | Remove-MailboxExportRequest -Confirm:$false }
Tuesday, January 14, 2020 1:53 PM
Answers
-
This is what the "Do" construct is for:
do{ Write-Host "wachten..." Start-Sleep 10 } until( 'Completed' -eq (Get_MailboxExportRequest -Mailbox $ad_user).Status)
There are also numerous other coding errors in your script.
\_(ツ)_/
- Proposed as answer by Stoyan ChalakovMVP Wednesday, January 15, 2020 12:28 PM
- Marked as answer by jrv Wednesday, January 15, 2020 1:14 PM
Tuesday, January 14, 2020 3:38 PM
All replies
-
You have to retrieve the status inside the loop or you will not get the updated status.
\_(ツ)_/
Tuesday, January 14, 2020 2:46 PM -
Maybe set the Export status to not completed then enter the loop and update the status there?
# Start de loop om elke 30 seconden te bekijken wat de status is van de export $Exportstatus = 'Not Completed' WHILE ($Exportstatus.Status -notlike "Completed") { $Exportstatus = Get_MailboxExportRequest -Mailbox $ad_user | Get-MailboxExportRequestStatistics Write-Host "wachten..." Start-Sleep 10 cls }
Seth
A user just like you
Tuesday, January 14, 2020 3:30 PM -
This is what the "Do" construct is for:
do{ Write-Host "wachten..." Start-Sleep 10 } until( 'Completed' -eq (Get_MailboxExportRequest -Mailbox $ad_user).Status)
There are also numerous other coding errors in your script.
\_(ツ)_/
- Proposed as answer by Stoyan ChalakovMVP Wednesday, January 15, 2020 12:28 PM
- Marked as answer by jrv Wednesday, January 15, 2020 1:14 PM
Tuesday, January 14, 2020 3:38 PM -
Thanks for the quick answers..
The Do construct is indeed working the best. Now the loop is doing what I want :)
Wednesday, January 15, 2020 7:43 AM