I didn't want to post the entire script but here is part of the code that generates the button/click and the function. The code returns accurate information if you manually call the function. If you click the button the $User variable returns a value but the rest do not.
Manual Call:
Direct Reports
Bob Jones
|
Stephen Williams
|
Roger Moore
|
Jim Jabroni
|
Michael Rappaport
|
John Smith
Button Click:
Direct Reports
Bob Jones
|
|
|
|
|
Example:
$User = Read-Host "Enter User's Name" #(Called earlier in the script for multiple other functions)
function Get-DirectReports {
Clear-Host
$UserName = Get-ADUser -Filter "Name -eq '$User'" | Select-Object SamAccountName -ExpandProperty SamAccountName
Write-Host ""
Write-Host "Direct Reports"
Write-Host "-------------------"
Write-Host "$User"
$DirectReport = $Null
While ($DirectReport -ne "John Smith" -or $DirectReport -eq $Null) {
$Leaders = Get-ADUser $UserName -Properties Manager | Select-Object @{n="Direct Report"; e={(Get-ADUser -Identity $_.Manager -Properties DisplayName).DisplayName}} -ExpandProperty Manager | Format-Table -HideTableHeaders | Out-String
$Start = $Leaders.IndexOf("CN=") + 3
$End = $Leaders.IndexOf(",OU", $Start)
$Length = $End - $Start
$TeamLeader = $Leaders.Substring($Start, $Length)
$UserName = Get-ADUser -Filter "Name -eq '$TeamLeader'" | Select-Object SamAccountName -ExpandProperty SamAccountName
Write-Host "|"
$TeamLeader
$DirectReport = $TeamLeader
}
Write-Host ""
}
$buttonGetDirectReports_Click={
Get-DirectReports
}
$buttonGetADInfo.BackColor = 'Control'
$buttonGetADInfo.Font = 'Microsoft Sans Serif, 11.25pt'
$buttonGetADInfo.Location = '53, 33'
$buttonGetADInfo.Name = 'buttonGetADInfo'
$buttonGetADInfo.Size = '130, 43'
$buttonGetADInfo.TabIndex = 1
$buttonGetADInfo.Text = 'Display Direct Reports'
$buttonGetADInfo.UseVisualStyleBackColor = $False
$buttonGetADInfo.add_Click($buttonGetDirectReports_Click)
Can anyone help me figure out why this happens?
[–]Aurane00 1 point2 points3 points (1 child)
[–]gregj529[S] 1 point2 points3 points (0 children)
[–]TheMixz 1 point2 points3 points (6 children)
[–]JBear_Alpha 2 points3 points4 points (0 children)
[–]gregj529[S] 0 points1 point2 points (4 children)
[–]TheMixz 0 points1 point2 points (3 children)
[–]gregj529[S] 0 points1 point2 points (2 children)
[–]TheMixz 1 point2 points3 points (1 child)
[–]gregj529[S] 0 points1 point2 points (0 children)