New-UDButton -Text “Reset Password” -OnClick (Reset-PW -Subject $_)
New-UDButton -Text “Reset Password” -OnClick {Reset-PW -Subject $_}
So, i discovered this today, the top line will successfully call the Custom Function Reset-PW, however the second line will fail.
If i use this:
New-UDButton -Text “Get Details” -OnClick {Invoke-UDRedirect -Url “/$sid”}
It behaves correctly.
Does anyone have any input as to why using the curly braces in the Reset-PW line would fail? Nothing shows up in the log, it will just not do anything.
Hi @Jacob-Evans
Is the normal brackets working fine, and the curly ones fail?
Should be the other way around, as the -onclick param is a scriptblock.
Scriptblock or UDEndpoint i believe.
Could you share the foreach loop you’re doin?
$Objects = $Cache:AllUsers | Where-Object {($_.name -like "*$accLookup*") -or ($_.SamAccountName -like "*$accLookup*") -or ($_.Description -like "*$accLookup*")}
New-UDGrid -Title "Search Results for: $accLookup" -Headers @("Username", "Name", "More Info", "Password Reset") -Properties @("Username", "Name", "MoreInfo", "ResetPW") -Endpoint {
$Objects | ForEach-Object {
$sid = $_.Description
[PSCustomObject]@{
Username = $_.UserPrincipalName
Name = $_.Name
MoreInfo = New-UDButton -Text "Get Details" -OnClick {Invoke-UDRedirect -Url "/$sid"}
ResetPW = New-UDButton -Text "Reset Password" -OnClick (Reset-PW -Subject $_)
}
} | Out-UDGridData
}
I swapped out the Curlys for the normals just to get it working for the time being, but this is where it is being called.
Thats weird.
Far fetched, but could you try to add a space after your “$_”, ie:
New-UDButton -Text "Reset Password" -OnClick { Reset-PW -subject $_ }
````
Try escaping the $_?
{ Reset-PW -subject $($_) }
````
Nope, no luck with that either…
adam
January 13, 2020, 6:24pm
8
It’s probably the ForEach-Object doing something weird with the scope.
What about this?
$Objects = $Cache:AllUsers | Where-Object {($_.name -like "*$accLookup*") -or ($_.SamAccountName -like "*$accLookup*") -or ($_.Description -like "*$accLookup*")}
New-UDGrid -Title "Search Results for: $accLookup" -Headers @("Username", "Name", "More Info", "Password Reset") -Properties @("Username", "Name", "MoreInfo", "ResetPW") -Endpoint {
$Objects | ForEach-Object {
$sid = $_.Description
[PSCustomObject]@{
Username = $_.UserPrincipalName
Name = $_.Name
MoreInfo = New-UDButton -Text "Get Details" -OnClick (New-UDEndpoint -Endpoint {Invoke-UDRedirect -Url "/$ArgumentList"} -ArgumentList $sid)
ResetPW = New-UDButton -Text "Reset Password" -OnClick (New-UDEndpoint -Endpoint { Reset-PW -Subject $ArgumentList[0] } -ArugmentList $_ )
}
} | Out-UDGridData
}
No need to change the Get Details Button, it behaves normally. Only the Reset Password button is having a problem, I’ll give it a shot, but its far easier to just hit the ( ) instead of { } if it is going to save all of that extra code.
adam
January 13, 2020, 6:29pm
11
Are you sure it’s not calling Reset-PW for all your accounts when you use ()? I would think that would evaluate those while it was looping and call that function for each row in the grid.
2 Likes
I would say the same Adam
I am sure.
I have logging into a database every time that function is called and the only log entries are for when it is actually pressed.
Here is the function for reference:
function Reset-PW {
param (
$subject
)
$epid = $subject.Name + "Reset-PW"
New-UDEndpoint -Id $epid -Endpoint {
Add-Log -requester $User -message "Reset button Pressed"
try {
Add-Log -requester $User -message "Attempting to Reset $($subject.Name)"
Set-ADAccountPassword -Identity $subject.SamAccountName -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "Spartans123" -Force)
$PWreset = "Success"
Add-Log -requester $User -message "Password Reset Successful"
}
catch {
Add-Log -requester $User -message "Password Reset Failed."
Add-Log -requester $User -message $Error
$PWreset = "Failed"
}
try {
Set-ADUser -Identity $subject.SamAccountName -ChangePasswordAtLogon $true
$CPreset = "Success"
Add-Log -requester $User -message "Password Change Required"
}
catch {
Add-Log -requester $User -message "Require Password Change failed."
Add-Log -requester $User -message $Error
$CPreset = "Failed"
}
try {
Unlock-ADAccount -Identity $subject.SamAccountName
$UAreset = "Success"
Add-Log -requester $User -message "Account Unlocked."
}
catch {
Add-Log -requester $User -message "Unlock Account Failed"
Add-Log -requester $User -message $Error
$UAreset = "Failed"
}
$TableDataPW = @(
[PSCustomObject]@{ Name = "Username"; Value = $subject.UserPrincipalName; Sort = "1" }
[PSCustomObject]@{ Name = "Name"; Value = $subject.Name; Sort = "2" }
[PSCustomObject]@{ Name = "Reset Password"; Value = $PWReset; Sort = "3" }
[PSCustomObject]@{ Name = "Password Reset to"; Value = "Spartans123"; Sort = "4" }
[PSCustomObject]@{ Name = "Password Change"; Value = $CPreset; Sort = "5" }
[PSCustomObject]@{ Name = "Unlock Account"; value = $UAreset; Sort = "6" }
).GetEnumerator()
Show-UDModal -Header {
New-UDHeading -Size 4 -Text "Here's the results for $($subject.Name)"
} -Content {
New-UDCard -Title "Account Details" -Content {
New-UDTable -Headers @("Attribute", "Value") -Endpoint {
$TableDataPW | Sort-Object Sort | Out-UDTableData -Property @("Name", "Value")
}
}
}
}
}
adam
January 13, 2020, 6:37pm
14
Weird. No idea why that works.
The reason this is weird is because of the way UD stores variable values for endpoints. While going through the loop, the _ variable value is being stored with the endpoint so that when the user clicks it, it can correctly call the script block with that value. The problem is that for whatever reason, when you try to set _ yourself it gets overridden during PS execution, which is happening to UD.
So you could try to also do this:
$Objects = $Cache:AllUsers | Where-Object {($_.name -like "*$accLookup*") -or ($_.SamAccountName -like "*$accLookup*") -or ($_.Description -like "*$accLookup*")}
New-UDGrid -Title "Search Results for: $accLookup" -Headers @("Username", "Name", "More Info", "Password Reset") -Properties @("Username", "Name", "MoreInfo", "ResetPW") -Endpoint {
$Objects | ForEach-Object {
$sid = $_.Description
$Item = $_
[PSCustomObject]@{
Username = $_.UserPrincipalName
Name = $_.Name
MoreInfo = New-UDButton -Text "Get Details" -OnClick { Invoke-UDRedirect -Url "/$sid" }
ResetPW = New-UDButton -Text "Reset Password" -OnClick { Reset-PW $Item }
}
} | Out-UDGridData
}
2 Likes