Page 1 of 1

Script Runs Fine in Debug, Errors in Normal Run

Posted: Tue Jun 09, 2020 8:16 am
by kdwisdom
To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product: PowerShell Studio 2020 (64 Bit)
Build: v5.7.178
OS: Windows 10 Enterprise (64 Bit)
Build: v10.0.18362.0

*** Please add details and screenshots as needed below. ***

DO NOT POST LICENSES, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM

I use the below script ( a little more comes above it, but this is where it is breaking) to update our Employee ID's in the AD Extension employeeID field.

Something weird is happening, I am not sure if it is with the way it counts the INTs or what, but when I run it with just the play button (or through a compiled EXE) it errors out and says check the spelling of "*S*", but when I run it in DEBUG mode, no errors whatsoever.

This makes it difficult to debug when debug mode works fine but production mode does not. Any ideas?

  1. function Update-EIDs
  2.  
  3. {
  4.  
  5.     $logfile = "Log File for Employee EID Updates in AD`r`n`r`n" #Preps the logfile
  6.  
  7.     $todaysDate = get-date #Gets the date for the logfile
  8.  
  9.     $logDate = $todaysDate.ToShortDateString() #Formats the date for the logfile
  10.  
  11.     $LogfilenameDate = get-date -format MMddyy #Gets the date for the filename
  12.  
  13.     $logfileName = "EID Update Log for " + $LogfilenameDate #Sets the filename for the log
  14.  
  15.     $logfilePath = "\\server\homedrives\myuser\" + $logfileName + ".txt" #Sets the full path for the log file
  16.  
  17.     $global:logDate = (Get-Date -Format "MMM dd yyyy")
  18.  
  19.     try
  20.  
  21.     {
  22.  
  23.        
  24.  
  25.         $i = 0
  26.  
  27.         [int]$total = ($eids | Measure-Object).Count
  28.  
  29.         while ($i -lt $total)
  30.  
  31.         {
  32.  
  33.             $wildcard = "*" + $eidusers[$i] + "*"
  34.  
  35.             $currentuser = Get-ADUser -Filter { name -like $wildcard -and name -notlike "*(Elevated)*" } | select -ExpandProperty samaccountname
  36.  
  37.             Set-ADUser $currentuser -EmployeeID $eids[$i]
  38.  
  39.             $i++
  40.  
  41.         }
  42.  
  43.        
  44.  
  45.         [System.Windows.Forms.MessageBox]::Show('Success')
  46.  
  47.     }
  48.  
  49.     catch
  50.  
  51.     {
  52.  
  53.         if ($i -eq ($lines | Measure-Object).Count) { Return }
  54.  
  55.         else
  56.  
  57.         {
  58.  
  59.            
  60.  
  61.             $errorMessage = "Check spelling of" + $wildcard
  62.  
  63.             [System.Windows.Forms.MessageBox]::Show($errorMessage) #Shows an exception error message if there is one. This will happen if the list you received has a misspelled name or if it couldn't find the user (if they have spaces in their name where they shouldn't, etc...)
  64.  
  65.         }
  66.  
  67.        
  68.  
  69.     }

Re: Script Runs Fine in Debug, Errors in Normal Run

Posted: Tue Jun 09, 2020 8:26 am
by Alexander Riedel
Can you please add the PowerShell version you are using and the exception error message if any?

Re: Script Runs Fine in Debug, Errors in Normal Run

Posted: Tue Jun 09, 2020 9:45 am
by kdwisdom
V5 64 bit.

I have a custom error message there, I don't get an exception error, I would need to tweak the code to do that.

This has run fine for the past 6+ months and just recently stopped working

Re: Script Runs Fine in Debug, Errors in Normal Run

Posted: Wed Jul 01, 2020 11:05 am
by kdwisdom
This issue is still persisting. Works absolutely fine when I go through step by step in debug mode, but running it in production or via the compiled executable, it is breaking somewhere along the line.

What I am doing is having it run get-aduser -filter {name -like "*last, first*"}

For some reason, it is only grabbing 1 character in the wildcard when I run it in regular (non-debug) mode. It will pop up an error message "Check the Spelling of "*M*", but runs with flying colors in debug mode.

It is very strange that it would break in non-debug mode. Makes it hard to troubleshoot what is going on.

Re: Script Runs Fine in Debug, Errors in Normal Run

Posted: Wed Jul 08, 2020 4:10 pm
by Alexander Riedel
Very odd. Usually something like this results from a timing issue. I will move this over to the general PowerShell section, maybe someone else has an idea about this.

Re: Script Runs Fine in Debug, Errors in Normal Run

Posted: Wed Jul 08, 2020 4:36 pm
by jvierra
It would be best if you took time to correctly format your code. As posted it is mostly unreadable. I copied and fixed the formatting so I could figure it out.

First the code is incomplete as teh function is missing the end closure.
The IF/ELSE is incorrect as it may not be executed correctly as formatted. It is a subtle issue in PS that has always been there.

IF/ELSE should be formatted like this:

Code: Select all

if(...){
    ...
}else{
     ...
}
This will always avoid these subtle issues.

You are never handling errors short of a message when one condition is true.

It is best practice to pass values to functions as parameters. This avoids many issues, Makes the function self-contained and makes debugging much easier.


The following link will help you understand how to correctly construct code.
https://poshcode.gitbooks.io/powershell ... and-style/

The following would be the better way to do this. Any logging should be external to any function.

Code: Select all

function Update-EIDs{
    param(
        [array]$eid,
        [array]$eiduser
    )
 
    try{
        for($i=0;$i -lt $eids.Count;$i++){
            $user = $eidusers[$i]
            if($currentuser = Get-ADUser -Filter "Name -like '*$user*' -and name -notlike '*(Elevated)*"){
                Set-ADUser $currentuser -EmployeeID $eids[$i]
                [System.Windows.Forms.MessageBox]::Show("$user Success")
            }else{
                [System.Windows.Forms.MessageBox]::Show("$user not found")
            }
        }
    }
    catch{
        [System.Windows.Forms.MessageBox]::Show("$_")
    }
}
Notice how my formatting is much easier to read.