There is something weird going on here. What you’re doing should work. Will need some further debugging. I was able to get this to work (at least passing the variables to the endpoint, not creating the AD groups) in my lab with the following structure using the $Session:
scope.
New-UDInput -Title "Adding Folder Group Membership - Based on Dept." -Content {
New-UDInputField -Type textbox -Name 'Manager' -Placeholder "Enter Manager NetID"
New-UDInputField -Type textbox -Name 'NewUser' -Placeholder "Enter New Users NetID"
} -Endpoint {
param($newuser, $manager)
$strDomn = "MYDOMAIN.local"
$objCurrDomn = [System.DirectoryServices.DirectoryEntry]::new()
#Manager
$objUsrMngr = [System.Security.Principal.NTAccount]::new($strDomn, $manager)
$strSIDMngr = $objUsrMngr.Translate([System.Security.Principal.SecurityIdentifier])
$strMngr = "CN=" + $strSIDMngr.Value + ",CN=ForeignSecurityPrincipals," + $objCurrDomn.distinguishedName
#NewUser
$objUsr = [System.Security.Principal.NTAccount]::new($strDomn, $newuser)
$strSIDusr = $objUsr.Translate([System.Security.Principal.SecurityIdentifier])
$strusr = "CN=" + $strSIDusr.Value + ",CN=ForeignSecurityPrincipals," + $objCurrDomn.distinguishedName
[array]$grpstr = Get-ADGroup -Filter {member -eq $strMngr} | Select-Object -ExpandProperty Name
[array]$Session:fgp = Get-ADGroup -filter {name -like "*" -and member -eq $strMngr} | Select-Object -ExpandProperty distinguishedName
New-UDInputAction -Content @(
New-UDCard -Title "Select Groups to Add New User" -Content {
for($i=0; $i -lt $grpstr.Length;$i++) {
New-UDButton -Text $grpstr[$i] -OnClick(
New-UDEndpoint -Endpoint {
$group = New-Object DirectoryServices.DirectoryEntry("LDAP://$($Session:fgp[ArgumentList[0]])")
[void]$group.member.Add("<SID=$($ArgumentList[1])>")
$group.CommitChanges()
$group.Close()
#Add-ADGroupMember -Identity $ArgumentList[0] -Members $ArgumentList[1]
Show-UDToast -Message "User Added to Group"
} -ArgumentList @($i, $strSIDusr)
)
}
}
)
}
If you ever want to debug an endpoint, add a Wait-Debugger
to it.
for($i=0; $i -lt $grpstr.Length;$i++) {
New-UDButton -Text $item -OnClick(
New-UDEndpoint -Endpoint {
Wait-Debugger
Then run your dashboard and click the button where the Wait-Debugger
is hiding. Go back to your PS console and type Get-Runspace
.
The runspace that is marked InBreakpoint
is the one you want. It’s waiting at the Wait-Debugger
call.
PS C:\Users\adamr> get-runspace
Id Name ComputerName Type State Availability
-- ---- ------------ ---- ----- ------------
1 Runspace1 localhost Local Opened Busy
12 Runspace12 localhost Local Opened Available
31 Runspace31 localhost Local Opened InBreakpoint
Then you can debug the runspace with Debug-Runspace
.
PS C:\Users\adamr> debug-runspace 31
Debugging Runspace: Runspace31
To end the debugging session type the 'Detach' command at the debugger prompt, or type 'Ctrl+C' otherwise.
At line:4 char:37
+ $Session:fgp[$ArgumentList[0]]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can then examine the variables that are available in your runspace.
[DBG]: [Process:19516]: [Runspace31]: PS C:\Users\adamr>> $Session:fgp
Some
Items
[DBG]: [Process:19516]: [Runspace31]: PS C:\Users\adamr>> $ArgumentList[0]
1
Hope that’s helpful!