Foreach Loop Output to Richtextbox failed

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 10 months and 3 weeks 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
McStarProjekt
Posts: 2
Last visit: Sun May 07, 2023 1:51 am

Foreach Loop Output to Richtextbox failed

Post by McStarProjekt »

Hello,

i want to count the numbers of mailboxes stored in a exchange database.
When i try to show the output with a richtextbox i get an error.

# First i get the mailboxdatabse names
try
{

$DatenbankNamen = Get-MailboxDatabase | select Name

$rtb_log.Text += "`r`n Get-MailboxDatabase| Select Name `r`n"
$rtb_log.Text += $DatenbankNamen | Out-String
$rtb_Log.SelectionStart = $rtb_Log.TextLength;
$rtb_Log.ScrollToCaret()

$rtb_datenbanken.Text += $DatenbankNamen | out-string
$rtb_datenbanken.Text.SelectionStart = $rtb_datenbanken.Text.TextLength;
$rtb_datenbanken.Text.ScrollToCaret()

}

#then i want foreach Database count the mailboxes with measure-object and put the output to a richtextbox
# but the output dont work.
# how can i show the foreache result in a richtextbox ?

foreach ($dbnamen in $DatenbankNamen)
{
$datenbank = $dbnamen | Select-Object -ExpandProperty Name

$rtb_log.Text += "`r`n ..starte Abfrage auf Datenbank: " + $datenbank
$rtb_Log.SelectionStart = $rtb_Log.TextLength;
$rtb_Log.ScrollToCaret()



$anzahlPostfaecher = get-mailbox -database $datenbank -resultsize unlimited | Measure-Object


$rtb_log.Text += "`r`n get-mailbox -database " + $datenbank + " -resultsize unlimited | Measure-Object"
$rtb_log.Text += $anzahlPostfaecher | Out-String
$rtb_Log.SelectionStart = $rtb_Log.TextLength;
$rtb_Log.ScrollToCaret()

}
McStarProjekt
Posts: 2
Last visit: Sun May 07, 2023 1:51 am

Re: Foreach Loop Output to Richtextbox failed

Post by McStarProjekt »

i solved it like this:

$mailboxDBS = get-mailboxdatabase

foreach ($db in $mailboxDBS)
{
$count = (get-mailbox -database $db.name).count
$rtb_datenbanken.AppendText("Datenbank: " + $db.name + " Anzahl der Postfächer " + $count + "`n")
$rtb_datenbanken.SelectionStart = $rtb_Log.TextLength;
$rtb_Datenbanken.ScrollToCaret()

$rtb_log.Text += $count | Out-String
$rtb_Log.SelectionStart = $rtb_Log.TextLength;
$rtb_Log.ScrollToCaret()
}
This topic is 10 months and 3 weeks 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