0

I create a script to do 2 jobs:

  1. Import CSV file with users email list then assign it to $emails

  2. Connect to Skype for business online using -UseOAuth command within the command Move-CsUser with Try and catch switch so if any user didn't move it will save the error to

    Error: $($Error[0])" |  Out-File c:\temp\CantFind.txt -Append
    

    and once the script is done I use notepad.exe c:\temp\CantFind.txt to open the text file with a list of the users that error out and the reason why they error out.

The script works perfectly the first time I run it. it shows the list of the users in text file call cantfind.txt. Once I import a new user email list and rerun the script still works perfectly but the text file cantfind.txt doesn't get updated with the new errors related to the new set of users but it keeps showing the first errors from the first user emails list.

I did try to clear and remove variable commands and I even included a function I founded here on the forums but nothing works cantfind.txt doesn't store the new errors anymore and even if I deleted the file it will generate a new file with the same errors I got from the first set of users that I run the first time. any suggestions would be welcome.

$emails =  Import-Csv  -path C:\temp\Userlist1.csv -Header Identity
 
foreach ($email in $emails) 
{
     try {
        Move-CsUser -Identity $email.Identity -Target sipfed.online.lync.com -Confirm:$false  -MoveToTeams -Force -UseOAuth
         -BypassAudioConferencingCheck -BypassEnterpriseVoiceCheck -HostedMigrationOverrideUrl 
             https://adminxx.online.lync.com/HostedMigration/hostedmigrationService.svc
     } 
     catch  
     {
         "Error: $($Error[0])" |  Out-File  c:\temp\CantFind.txt -Append
     }  
}  
notepad.exe c:\temp\CantFind.txt
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Yallabina
  • 107
  • 1
  • 4
  • 17
  • 1
    If you are already using `try-catch`, then `$_` in your catch block contains your caught exception. – AdminOfThings Jul 07 '20 at 04:18
  • What are you talking about?? what exception – Yallabina Jul 07 '20 at 05:17
  • When you are in a catch block, you can call an 'error' also an exception. – Alex_P Jul 07 '20 at 07:19
  • 1
    Thus: `"Error: $_" | Out-File c:\temp\CantFind.txt -Append`. Also: with version of PowerShell do you use? (Note that PowerShell 5.1 has a `$MaximumErrorCount` preference variable set to: `256`) – iRon Jul 07 '20 at 10:18
  • Thanks, @iRon you are the man!! I did update my code to add 'MaximumErrorCount = 512' and my release the value for error form error '"Error: $($Error)" | Out-File c:\temp\CantFind.txt' and now works as expected. thanks again. this is the final version **foreach ($email in $emails) { try {Move-CsUser -Identity $email.Identity -Target sipfed.online.lync.com -Confirm:$false -UseOAuth -MoveToTeams -Force -BypassAudioConferencingCheck -BypassEnterpriseVoiceCheck -HostedMigrationOverrideUrl https:xsvc } catch {"Error: $($Error)" | Out-File c:\temp\CantFind.txt }}c:\temp\CantFind.txt ** – Yallabina Jul 07 '20 at 15:24

1 Answers1

0

As per comment from @AdminOfThings, you better use the current item ($PSItem or just $_) which holds the exception raised in the try section:

try {
    $a = 1/0
}
catch {
    "$_"
}

Which returns:

Attempted to divide by zero.

Besides, it is good to know that the $Error object holds a limited amount of errors (by default 256). The latest error will be added to the top of the list ($Error[0]) and the rest of the error will shift down until the limit is reached and then they will simply drop of.

In Windows PowerShell (up and till version 5), you can set the size of the error list with the $MaximumErrorCount preference variable. In PowerShell core there is an issue, see: Remove most Maximum* capacity variables

To capture all errors not yet captured, see: Catching Cascading Errors in PowerShell

iRon
  • 20,463
  • 10
  • 53
  • 79