Speed up Powershell script

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 3 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
max Inform
Posts: 1
Last visit: Fri Dec 16, 2022 8:59 am

Speed up Powershell script

Post by max Inform »

I have created such a function and query a group name, get only the digits from the name and then search with the digits other group. From this I create a PS Custom Object to show it in DataGriedview.

Unfortunately, with about 1000 groups, the query takes about 10 seconds. How can I do it better to speed up the process?
Here is the script:

Code: Select all

function Get-UserNotMemberIn
{
	$script:NotMemberIn = @()
	Get-ADGroup -Filter { Name -like "tol.*" -and Name -notlike "tol.tm.*" } -Properties * | Where-Object { $_.DistinguishedName -notin $selectedUser.MemberOf } | ForEach-Object {
		
		$script:NotMemberIn += [PSCustomObject] @{
			SamAccountName = $_.SamAccountName
			Name		   = (((($_.Name) -replace 'tol.tt.', '') -replace 'tol.tt_', '') -replace 'tol.total.', '').Split('_')[0]
			PrettyName	   = $_.PrettyName # is custom attribut
			TTPrettyName   = if (($_.SamAccountName -replace '\D+', '') -ne '')
			{
				$replacer = $_.SamAccountName -replace '\D+', ''
				$costNr = "*." + $replacer
				(Get-ADGroup -SearchBase $global:TTOU -Filter { Name -like $costNr } -Properties *).PrettyName # is custom attribut
			}
		}
	}
	$script:NotMemberIn
}

$dataTableNotMemberIn = ConvertTo-DataTable -InputObject (Get-UserNotMemberIn)

$datagridview3.DataSource = $dataTableNotMemberIn
Thx
User avatar
brittneyr
Site Admin
Posts: 1654
Last visit: Thu Mar 28, 2024 6:54 am
Answers: 39
Been upvoted: 30 times

Re: Speed up Powershell script

Post by brittneyr »

[Topic was moved by moderator to PowerShell GUIs forum]
Brittney
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Speed up Powershell script

Post by jvierra »

Why are you doing so many conversions. The return from Get-AdGroup can be just generated as a table.

It is very difficult to guess the problem you are trying to solve. A clear and simple English language description of what you are trying to accomplish would be more helpful.

You are executing a new "Get" for every first element. This cannot be sped up. Other approaches like directly using the AD Net classes or ADSI can be faster, but the approach needs to get all of the data in one call to AD.

If this is usable and just slightly slow then you might consider just accepting that some things are slow. This also depends on your network and your AD performance. Multiple fast sequential accesses will cause a busy AD to slow you down. If you can get everything in one query that issue would be less impacting.

Again, knowing exactly what you are trying to do would make my guesses much better. Also, I don't know your AD implementation or network as those things could be the source of your performance issue.

I would also skip the array and create the whole table and add it to the DGV then add rows to the table as they are acquired from AD. Next enumerate the table and grab the missing fields. Avoid string conversions of AD objects. This can add a lot of overhead and makes the code harder to understand, debug and maintain.
This topic is 1 year and 3 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