Reading Multiline textbox and output to Datagridview

Ask your PowerShell-related questions, including questions on cmdlet development!
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 6 years and 4 months 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
User avatar
seanlich1
Posts: 1
Last visit: Tue Jul 27, 2021 1:59 pm

Reading Multiline textbox and output to Datagridview

Post by seanlich1 »

I am having an issue when I am reading a multiline textbox and taking users first and last name, then running a search and returning values from AD.
Here is the code:
function GetUserInfoName
{

$usersearch1 = $textbox15.Text.Split("`n")
foreach ($obj in $usersearch1)
{
$usersearchsplit = -split $obj
$trackuser0 = $usersearchsplit[0]
$trackuser1 = $usersearchsplit[1]


if ($usersearchsplit[0] -eq $null)
{ (new-object -ComObject wscript.shell).Popup("Please enter Search Criteria.", 0, "Search Criteria Error", 0) }
else
{
$usersearchresult = @()
$findusersearch1 = Get-ADUser -Filter { GivenName -like $trackuser0 -and Surname -like $trackuser1 } -Properties ipphone
$findusersearch1 | ForEach-Object { $usersearchresult += New-Object psobject -Property @{ 'User ID' = $_.SamAccountName; 'Email Address' = $_.UserPrincipalName; 'Phone Number' = $_.ipPhone } }
}

}
$usersearchresultDB = ConvertTo-DataTable -InputObject $usersearchresult
Update-DataGridView -DataGridView $datagridview15 -Item $usersearchresultDB
}

I am ending up with just the last user in a list of three people that I am testing in $usersearchresult.
Any suggestions would be greatly appreciated. I know the code is wrong somewhere, my brain is just not finding it and fresh eyes would probably snag it in a second. Thanks.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Reading Multiline textbox and output to Datagridview

Post by jvierra »

To gather user names why not just use another DataGridView with two columns. Grab the selections and gate all users in one loop into and array then just show them in the grid.

In any case use the search and grid like this to get all results.

Code: Select all

$users = Get-ADUser -Filter { GivenName -like $trackuser0 -and Surname -like $trackuser1 } -Properties ipphone |
	ForEach-Object { 
		[pscustomobject]@{
			'User ID' = $_.SamAccountName
			'Email Address' = $_.UserPrincipalName
			'Phone Number' = $_.ipPhone 
		}
	}
$datagridview15.DataSource = ConvertTo-Datable $users
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Reading Multiline textbox and output to Datagridview

Post by jvierra »

To use "-Like" you need to add a wildcard.
This topic is 6 years and 4 months 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