Combobox selection is not being transferred to variable

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 1 year and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
SCFreiburg-IT
Posts: 3
Last visit: Thu Feb 01, 2024 11:42 pm

Combobox selection is not being transferred to variable

Post by SCFreiburg-IT »

Hello everyone,

After trying several different ways, I need help :).
I'm reading certain OUs in a domain and displaying them in a combobox:

$excludedNames = "xxx", "xxx", "xxx", "xxx"
Update-ComboBox $comboboxOU (Get-ADOrganizationalUnit -Filter * -SearchBase "OU=xxx,DC=xxx,DC=xxx" -SearchScope Subtree | Where-Object { $_.Name -notin $excludedNames } | Select-Object Name) -DisplayMember "Name"
Then the selection is displayed again at the end as a summary:
$comboboxOU_SelInCh={
$SumOU.Text = $comboboxOU.Text
Update-NavButtons
}
This part works so far and outputs correctly. So I thought it couldn't be that hard to write the content of the $comboboxOU variable as a string to a new variable and then use it to create a new user:
$OU = $comboboxOU.Text
$OU = Get-ADOrganizationalUnit -Filter {Name -eq $OU} -SearchBase "DC=xxx,DC=xxx"
$OU = ($OU).DistinguishedName
This is where I now have a problem because I'm not getting anything passed on. After running the following command to create the user:
Invoke-Command -ComputerName $DC -Credential $cred -ScriptBlock {
New-ADUser -Name $using:Name -GivenName $using:GivenN -Surname $using:Surname -DisplayName $using:DispName -EmailAddress $using:UsrMail -SamAccountName $using:SamAcc -UserPrincipalName "$using:SamAcc@$using:UDomain" -Department $using:Departm -Description $using:Discrip -Title $using:Titel -Company $using:Company -City $using:City -Country $using:Country -State $using:State -PostalCode $using:PostalCode -StreetAddress $using:Street -Path $using:OU -AccountPassword ("xxx" | ConvertTo-SecureString -AsPlainText -Force) -ChangePasswordAtLogon $true -Enabled $true
}
...I get an error that the "Path" ia wrong and the $OU variable cannot be null (which I thought was filled in above?). What's the difference between this and passing it to the $SumOU.Text variable above?

I hope you have ideas...

I've already tried the following combinations:

$OU = $comboboxOU.SelectedItem, $OU = $comboboxSalto.SelectedItem.toString() - i don't get the String in $OU.
"If I manually fill the $OU variable, it works - which means to me that the problem must lie in the transfer from the combobox to the variable. This doesn't make sense to me again, because I already pass and output the selection of the combobox as text (which works).

I hope you can help me! :)

Best regards.
KW
SCFreiburg-IT
Posts: 3
Last visit: Thu Feb 01, 2024 11:42 pm

Re: Combobox selection is not being transferred to variable

Post by SCFreiburg-IT »

I found the "error" (even though it wasn't obvious to me): I had the variable transfer outside of the function where the user is created. Although I don't understand what difference it makes, but it's resolved now!

Thank you very much anyway! :D

KW
This topic is 1 year and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked