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,

J. Tam
  • 105
  • 1
  • 9
  • Your script has spaces... you're using the wrong operator: `& '.\Follow-up Email Generator.ps1'` – Maximilian Burszley Aug 22 '18 at 17:41
  • I'd advise against using relative paths and opt for `$PSScriptRoot`. – Maximilian Burszley Aug 22 '18 at 17:43
  • your function csvCheck is a function declared inside the Follow-up Email Generator.ps1? – polzka90 Aug 22 '18 at 17:43
  • Yes @polzka90 I didn't want to paste the entire script as it was fairly long. – J. Tam Aug 22 '18 at 17:44
  • but what is not working properly? it is not executing or executing a part? can you give more details please? – polzka90 Aug 22 '18 at 17:46
  • @polzka90 Specifically for this set of code, `$global:skip += $k` and `$global:skip += $j` do not work. It is skipped entirely causing the code to generate repeat emails for the same user. – J. Tam Aug 22 '18 at 17:50
  • Have a look at the answer given [here](https://stackoverflow.com/questions/50843217/powershell-global-variable-is-not-inherited-by-child-scripts) – Theo Aug 22 '18 at 18:03
  • @Theo Thank you so much! This had my answer. I did not realize that I was running the second script under the same context as the first script. – J. Tam Aug 22 '18 at 18:14

1 Answers1

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

J. Tam
  • 105
  • 1
  • 9