Script Runs Fine in Debug, Errors in Normal Run

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 3 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
kdwisdom
Posts: 25
Last visit: Fri Jan 26, 2024 10:39 am

Script Runs Fine in Debug, Errors in Normal Run

Post 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.     $logfile = "Log File for Employee EID Updates in AD`r`n`r`n" #Preps the logfile
  4.     $todaysDate = get-date #Gets the date for the logfile
  5.     $logDate = $todaysDate.ToShortDateString() #Formats the date for the logfile
  6.     $LogfilenameDate = get-date -format MMddyy #Gets the date for the filename
  7.     $logfileName = "EID Update Log for " + $LogfilenameDate #Sets the filename for the log
  8.     $logfilePath = "\\server\homedrives\myuser\" + $logfileName + ".txt" #Sets the full path for the log file
  9.     $global:logDate = (Get-Date -Format "MMM dd yyyy")
  10.     try
  11.     {
  12.        
  13.         $i = 0
  14.         [int]$total = ($eids | Measure-Object).Count
  15.         while ($i -lt $total)
  16.         {
  17.             $wildcard = "*" + $eidusers[$i] + "*"
  18.             $currentuser = Get-ADUser -Filter { name -like $wildcard -and name -notlike "*(Elevated)*" } | select -ExpandProperty samaccountname
  19.             Set-ADUser $currentuser -EmployeeID $eids[$i]
  20.             $i++
  21.         }
  22.        
  23.         [System.Windows.Forms.MessageBox]::Show('Success')
  24.     }
  25.     catch
  26.     {
  27.         if ($i -eq ($lines | Measure-Object).Count) { Return }
  28.         else
  29.         {
  30.            
  31.             $errorMessage = "Check spelling of" + $wildcard
  32.             [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...)
  33.         }
  34.        
  35.     }
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

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

Post by Alexander Riedel »

Can you please add the PowerShell version you are using and the exception error message if any?
Alexander Riedel
SAPIEN Technologies, Inc.
kdwisdom
Posts: 25
Last visit: Fri Jan 26, 2024 10:39 am

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

Post 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
kdwisdom
Posts: 25
Last visit: Fri Jan 26, 2024 10:39 am

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

Post 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.
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

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

Post 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.
Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post 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.
This topic is 3 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked