bug:: function not returning value

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 2 years and 10 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
Rixtter747
Posts: 11
Last visit: Tue Nov 08, 2022 2:44 am

bug:: function not returning value

Post by Rixtter747 »

Hi folks, I'm on version 2019, would like to upgrade but the one thing i use this for doesn't work correctly.

Here is the code:

Code: Select all

function SelectSmallestDatabase {
	$count = 0
	$arrayTarget = 0
	$getDBs = get-MailboxDatabase "DAG2*"
	$countDBs = $getDBs.Count
	$getDBStats = get-MailboxDatabase $getDBs[$count] -status | select databasesize
	$smallestDBSize = $getDBStats.databasesize.tomb()
	for ($count = 1; $count -lt $countDBs; $count++) {
		$getDBStats = get-MailboxDatabase $getDBs[$count] -status | select databasesize
		$tempDBSize = $getDBStats.databasesize.tomb()
		if ($tempDBSize -lt $smallestDBSize) {
			$smallestDBsize = $tempDBSize
			$arrayTarget = $count
		}
	}
	[string]$smallestDB = $getDBs[$arrayTarget].name
	return $smallestDB
}
This functions perfectly using powershell, but doesn't return anything through the GUI. I have traced through it, and it doesn't store $smallestDBSize which renders it useless.

I don't want to upgrade until I know this works, or why it doesn't. Please help...

Richard.
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 1:00 pm
Answers: 39
Been upvoted: 30 times

Re: bug:: function not returning value

Post by brittneyr »

Assuming Get-MailBoxDatabase isn't returning, not all commands work directly from a GUI. Try to call your function before the form is created. It is possible that you cannot do that from an event handler. Any code outside of any event will execute before the form is created.

Also, try running your form with STA Mode enabled.

If this doesn't help, please specify the module you are using and provide more of your script.
Brittney
SAPIEN Technologies, Inc.
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: bug:: function not returning value

Post by Alexander Riedel »

"it doesn't store $smallestDBSize". Can you elaborate on that? What does that mean?
Does this:
$smallestDBSize = $getDBStats.databasesize.tomb()
not return anything?
Or is this:
if ($tempDBSize -lt $smallestDBSize) {
$smallestDBsize = $tempDBSize
$arrayTarget = $count
}
not updating the variable?

Or is the variable $smallestDBSize empty after you return from this function?
Alexander Riedel
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: bug:: function not returning value

Post by jvierra »

Consider that the logic allows the variable to be empty then "$smallestDBSize" can be empty.

The function always returns the "name" and never the DB size.

I agree with Alex. Not enough information. Also What "GUI" are you referencing, PowerSHell Studio or some other GUI?
This topic is 2 years and 10 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