[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 346
  • Last Modified:

Consolidating scripts

I have this script here which works well. I also have a command that I runs afterwards directly below. I would like to consolidate this into one script. The problem is I do not know how to gracefully end the foreach loop and then enter a new one to place this command in. For example if I have 4 users I would like for the script to run 4 times and as a last option run this command directly below. Any help would be appreciated, thanks.

get-content c:\list.txt | foreach { get-mailboxfolderstatistics -id $_ | select name, foldersize, itemsinfolder

}

Get-Content c:\list.txt |  foreach {Get-Mailboxstatistics -id $_ | foreach{ 
$mbx = $_ | select DisplayName, @{Label=’MailboxSize("MB")’;Expression={$_.TotalItemSize/1MB}}, ItemCount
$date_captured=get-date | select datetime
Get-Mailbox -id $_ | foreach{
$mbx | add-member -type noteProperty -name Alias -value $_.Alias
$mbx | add-member -type noteProperty -name ServerName -value $_.ServerName
$mbx | add-member -type noteProperty -name ProhibitSendReceiveQuota -value $_.ProhibitSendReceiveQuota
$mbx | add-member -type noteProperty -name UseDatabaseQuotaDefaults -value $_.UseDatabaseQuotaDefaults
$mbx | add-member -type noteProperty -name IssueWarningQuota -value $_.IssueWarningQuota



}



$mbx, $date_captured




$mbc

}}

Open in new window

0
techdrive
Asked:
techdrive
  • 2
1 Solution
 
slidingfoxCommented:
Try this. It'll write the output to C:\results.txt rather than to the screen. Because your outputting two different object types, the formatting of the output is a bit crazy when you write to the host. This is why I went for a txt file instead.

Hope it does what your after...
--------------------------------------------------------------------------
Get-Content C:\list.txt |
    Foreach {
       
        $mbx = Get-Mailbox -id $_
        $mbxstats = Get-Mailboxstatistics -id $_ | select DisplayName,@{l=’MailboxSize(MB)’;e={$_.TotalItemSize/1MB}},ItemCount
        $fldstats = Get-Mailboxfolderstatistics -id $_ | select name,foldersize,itemsinfolder
        $date = Get-Date
       
        $out = New-Object -Type PSObject
        $out | add-member -type noteProperty -name Alias -value $mbx.Alias
        $out | add-member -type noteProperty -name DisplayName -value $mbxstats.DisplayName
        $out | add-member -type noteProperty -name ServerName -value $mbx.ServerName
        $out | add-member -type noteProperty -name ProhibitSendReceiveQuota -value $mbx.ProhibitSendReceiveQuota
        $out | add-member -type noteProperty -name UseDatabaseQuotaDefaults -value $mbx.UseDatabaseQuotaDefaults
        $out | add-member -type noteProperty -name IssueWarningQuota -value $mbx.IssueWarningQuota      
        $out | add-member -type noteProperty -name ’MailboxSize(MB)’ -value $mbxstats.’MailboxSize(MB)
        $out | add-member -type noteProperty -name ItemCount -value $mbxstats.ItemCount
       
        Write-Output $out | Out-File C:\Results.txt -append
        Write-Output $fldstats | Out-File C:\Results.txt -append
}
0
 
techdriveAuthor Commented:
You Sir are awesome. This works grrreeeaaatttt.
0
 
slidingfoxCommented:
Glad it works for you. :)

If your happy with it, could you mark it as an accepted solution please.

Cheers.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now