0

Currently, I am struggling to run a powershell script within another powershell GUI. I am trying to program it so that once a button is pressed, another powershell script would begin to run. The issue is that the second powershell script being ran does not run correctly when called. (It runs perfectly fine on its own.)

Code for the button:

$handler_runBTN_Click = 
{
    if($compliance){#runs Compliance training scripts
        .\"Follow-up Email Generator.ps1"
    }
}
$runBTN.Text = "Run"
$System_Drawing_Point.X = 140
$System_Drawing_Point.Y = 170
$runBTN.Location = $System_Drawing_Point 
$runBTN.add_Click($handler_runBTN_Click)
$runBTN.FlatStyle = "Standard"
$emailForm.Controls.Add($runBTN)

Code not running properly when being called by button (It is called by other code but that runs correctly):

function csvCheck($intervalDate, $reminderDate) { #gathers all courses due on a specific date for a user through a csv file

    for ($j = 0; $j -lt $row_count.Count; $j++) {#goes through every row of CSV file
        if ($global:skip -eq $j ) {continue} #if the emails under $j are in the array $skip, the loop will skip it and continue 

        $requiredDate = $file[$j].'Required Date' 

        if (($requiredDate -eq $reminderDate)) { #courses that are coming due

            $global:skip += $j #skips the iteration so it does not occur again
            $bodyText = "<li>" + $file[$j].'Curriculum #' + "-&nbsp;<strong>Due:&nbsp;" + $file[$j].'Required Date' + " </strong></li>"
            for ($k = $j + 1; $k -lt $row_count.Count; $k++) { #checks every other date for roccurances
                if ($file[$j].'Employee ID' -eq $file[$k].'Employee ID' -and $file[$j].'Required Date' -eq $file[$k].'Required Date') {
                    #checks for any other courses for the same user due on the same id                 
                    $file[$k].'Last Sent' = $today
                    $bodyText += "<li>" + $file[$k].'Curriculum #' + "-&nbsp;<strong>Due:&nbsp;" + $file[$k].'Required Date' + " </strong></li>" #Adds onto the default bullet form text
                    $global:skip += $k 
                }
            }  
            email $bodyText "due in $intervalDate day(s):" "comingDue"
        }
    }
}

I was wondering if it was my code that was causing this to happen or if it had something to do with the nature of the button.

Thanks,

8
  • Your script has spaces... you're using the wrong operator: & '.\Follow-up Email Generator.ps1' Commented Aug 22, 2018 at 17:41
  • I'd advise against using relative paths and opt for $PSScriptRoot. Commented Aug 22, 2018 at 17:43
  • your function csvCheck is a function declared inside the Follow-up Email Generator.ps1? Commented Aug 22, 2018 at 17:43
  • Yes @polzka90 I didn't want to paste the entire script as it was fairly long. Commented Aug 22, 2018 at 17:44
  • but what is not working properly? it is not executing or executing a part? can you give more details please? Commented Aug 22, 2018 at 17:46

1 Answer 1

1

Turns out that I was calling my second script incorrectly.

This line of code ran the second script under the same context as the first causing $global: variables to not work correctly.

.\"Follow-up Email Generator.ps1"

To solve this, I changed the code that runs the second script to:

Powershell -File ".\Follow-up Email Generator.ps1"

This ran the second script in its separate context allowing the $global: variables to work

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.