Need Help with Advanced Function Using Input GUI and Active Directory Cmdlets

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Ron Ron 3 years, 3 months ago.

  • Author
    Posts
  • #14071
    Profile photo of Ron
    Ron
    Participant

    Attached is a fairly simple script that contains two functions. One function is a Read-InpuGUI function that I found online and the other is my Check-ADUserExist advanced function.

    The script works fine for one user, but when I run the following command to call on the Read-InputGUI function to display the multi line input box to enter multiple users, and then click OK, the script will NOT produce any results – period! If I enter only one user in the multi line GUI, it will produce output just fine.

    It only has issues with multiple users.

    If I output the data contained in the $UserList variable to a text file to hold all the user names and then use Get-Content to grab that data, then all the users will get processed. I don't want to have to do it this way though, since I think producing text files like this is kind of sloppy when I know there is a better way of doing it.

    Command to use the input GUI:

    Check-ADUserExist -ADUserName "userlist"

    To check if my $UserList variable was holding multiple users, I simply output the data to the screen and all the users were displayed with no issue.

    The problem looks to be that the Active Directory cmdlet may an an issue processing multiple users in this fashion using an input GUI, but that is just speculation.

    Here is where it appears the issue is:

    ElseIf ($ADUserName -eq "UserList")
    		{
    			$UserList = Read-InputGUI "Please Enter User Names Here:" "User List" "Input user names here and hit the Enter button for each user name entry"
    			
    			Foreach ($ADDomain in $ADDomains)
    			{
    				Foreach ($ADUser in $UserList)
    				{
    					$ADUserQuery = (Get-ADUser -Filter {sAMAccountName -eq $ADUser} -Server $ADDomain).sAMAccountName
    					
    					If ($ADUserQuery -eq $ADUser)
    					{
    						$OutHostNewLine
    						
    						Write-Output "$ADUser exist in $ADDomain"
    						
    						$OutHostNewLine
    					}

    I would greatly appreciate an explanation why this cannot get results for multiple users and\or some help to resolve this.

    Thanks everyone

    -Ron

  • #14080
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    Looking at your attachment you have this line

    $QueryADUserName = (Get-ADUser -Filter {sAMAccountName -eq $ADUserName} -Server $ADDomain).AMAccountName

    AMAccountName should be samAccountName

    I'd put some write-verbose statements into your code to test which domain you are in, that your call to Get-ADuser has actually worked, that the correct values are coming though as $ADuser. Also check that the data you are getting from $userlist doesn't contain trailing spaces as that will impact your results

  • #14088
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Read-InputGUI is returning a single String value (which happens to contain multiple lines). PowerShell won't automatically enumerate those lines with a foreach loop; you need to split the string into an array first. It's probably also a good idea to filter out any blank / whitespace lines, in case the user entered something weird. Try this:

    Foreach ($ADUser in $UserList -split '\r?\n' -match '\S')
    
  • #14112
    Profile photo of Ron
    Ron
    Participant

    DAVE!!!

    YOU ARE BADASS!!!

    It worked dude!

    Thanks again for your help brother.

    Richard, yeah that was a typo bro, sorry about that.

You must be logged in to reply to this topic.