none
Need help with collecting combined data into a variable and out to another variable to collect the combined data for multiple servers RRS feed

  • Question

  • I have modified some code to work with what I need to collect data from two different commands into one.  It works great for one server but when I try to run it against a list of servers it falls apart and I'm stuck in trying to figure out the solution.

    I understand I'm still learning so please be gentle.

    Single Run:

    $s = "exmb01"
    # https://stackoverflow.com/questions/11721835/in-powershell-how-can-i-combine-the-results-of-two-commands-that-have-a-1-to-1
    $outputCollection = @()
    $disk = Get-WmiObject -Class win32_volume -cn $s | 
         where-object {$_.label -ne $null -and $_.label -ne “System Reserved” -and $_.drivetype -eq 3 -and $_.freespace -lt '10000000000' -and $_.label -notlike "data*" -and $_.label -notlike "Logs*" -and $_.label -ne 'L - Exch Logs'} | 
           Select-Object @{LABEL='Computer';EXPRESSION={$s}}, 
             driveletter, label,  
             @{LABEL='DiskGBfreespace';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)}}
    $mailboxes = Get-MailboxDatabase -server $s -Status | select Name,edbfilepath,AvailableNewMailboxSpace
    
    $disk | Foreach-Object {
        #Associate objects
        $diskObject = $_
    #    $mbxObject = $mailboxes | Where-Object {$diskObject.Label -Like $mailboxes.edbfilepath}
        $mbxObject = $mailboxes | where-object {$_.edbfilepath -match $diskObject.Label}
        #Make a combined object
        $outputObject = "" | Select Computer, Label,Name, DiskGBfreespace, AvailableNewMailboxSpace, edbfilepath
        $outputObject.Computer = $DiskObject.Computer
        $outputObject.label = $DiskObject.label
        $outputObject.DiskGBfreespace = $DiskObject.DiskGBfreespace
        $outputObject.edbfilepath = $mbxObject.edbfilepath
        $outputObject.Name = $mbxObject.Name
        $outputObject.AvailableNewMailboxSpace = $mbxObject.AvailableNewMailboxSpace
    
        #Add the object to the collection
        $outputCollection += $outputObject
    }
    
    $outputCollection | FT

    This is now where I'm trying to use the above to output many servers to a html based email report.  The report right now works with the data split but we need it combined.

    #=================================================
    Function Get-Space 
    { 
     Param ([string[]]$servers) 
     $outputCollection = @()
      Foreach ($s in $servers)  
       { 
         $disk = Get-WmiObject -Class win32_volume -cn $s | 
         where-object {$_.label -ne $null -and $_.label -ne “System Reserved” -and $_.drivetype -eq 3 -and $_.freespace -lt '10000000000' -and $_.label -notlike "data*" -and $_.label -notlike "Logs*" -and $_.label -ne 'L - Exch Logs'} | 
           Select-Object @{LABEL='Computer';EXPRESSION={$s}}, 
             driveletter, label,  
             @{LABEL='DiskGBfreespace';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)}}
    $mailboxes = Get-MailboxDatabase -server $s -Status | select Name,edbfilepath,AvailableNewMailboxSpace
    
    $disk | Foreach-Object {
        #Associate objects
        $diskObject = $_
        $mbxObject = $mailboxes | where-object {$_.edbfilepath -match $diskObject.Label}
        #Make a combined object
        $outputObject = "" | Select Computer, Label,Name, DiskGBfreespace, AvailableNewMailboxSpace, edbfilepath
        $outputObject.Computer = $DiskObject.Computer
        $outputObject.label = $DiskObject.label
        $outputObject.DiskGBfreespace = $DiskObject.DiskGBfreespace
        $outputObject.edbfilepath = $mbxObject.edbfilepath
        $outputObject.Name = $mbxObject.Name
        $outputObject.AvailableNewMailboxSpace = $mbxObject.AvailableNewMailboxSpace
    
        #Add the object to the collection
        $outputCollection += $outputObject
              } #end $Disk ForEach
        } #end foreach $s 
    } #end function Get-Space 
    #=================================================
    write-host "Space Start $(Get-Date)"
    $Space = Get-Space -servers $servers |  
    Sort-Object Name | 
    ConvertTo-Html -As Table -Fragment -PreContent "
      <h2>Space Report</h2> "| Out-String   
    write-host "Space End $(Get-Date)"    
    #=================================================
    

    I know the issue is that I'm running through the script and basically wiping out the variable $outputCollection and it is not writing to the variable $space which would be the entire collection.

    Please help me in figuring out what it is I'm missing.

    Thank you

    • Moved by Bill_Stewart Thursday, December 20, 2018 9:40 PM This is not "debug/fix/rewrite my script for me" forum
    Monday, October 15, 2018 9:16 PM

All replies

  • Start with this.

    Function Get-Space{
        Param (
            [string[]]$servers
        )
        
        Foreach ($s in $servers){ 
            $mailboxes = Get-MailboxDatabase -server $s -Status 
            Get-WmiObject -Class win32_volume -Filter 'DriveType=3 and Freespace<10000000000'-cn $s | 
                Where-Object{$_.label -notmatch 'System Reserved|data|Logs|L - Exch Logs'} |
                Foreach-Object {
                    $mbxObject = $mailboxes | where-object {$_.edbfilepath -match $_.Label}
                    $outputObject = "" | Select Computer, Label,Name, DiskGBfreespace, AvailableNewMailboxSpace, edbfilepath
                    [pscustomobject]@{
                        Computer = $_.Computer
                        Label = $_.label
                        DiskGBfreespace = $_.Freespace
                        EdbFilePath = $mbxObject.edbfilepath
                        Name = $mbxObject.Name
                        AvailableNewMailboxSpace = $mbxObject.AvailableNewMailboxSpace
                    }
                }  
        } #end foreach $s
    } #end function Get-Space
        
    write-host "Space Start $(Get-Date)"
    $Space = Get-Space -servers $servers |  
        Sort-Object Name | 
        ConvertTo-Html -Fragment -PreContent '<h2>Space Report</h2>'| Out-String   
    write-host "Space End $(Get-Date)"    
    

     



    \_(ツ)_/


    • Edited by jrv Monday, October 15, 2018 9:39 PM
    Monday, October 15, 2018 9:34 PM
  • jrv,

    Sorry I have not responded but will update you that I'm having troubles getting your script to work.  I just wanted to provide an update.

    Friday, October 19, 2018 2:07 AM
  • Unclear.  What is the issue?

    This may resolve some issues,

    Function Get-Space{
        Param (
            [string[]]$servers
        )
        
        Foreach ($s in $servers){ 
            $mailboxes = Get-MailboxDatabase -server $s -Status 
            Get-WmiObject -Class win32_volume -Filter 'DriveType=3 and Freespace<10000000000'-cn $s | 
                Where-Object{$_.label -notmatch 'System Reserved|data|Logs|L - Exch Logs'} |
                Foreach-Object {
                    $mbxObject = (@($mailboxes | where-object {$_.edbfilepath -match $_.Label}))[0]
                    [pscustomobject]@{
                        Computer = $_.Computer
                        Label = $_.label
                        DiskGBfreespace = $_.Freespace
                        EdbFilePath = $mbxObject.edbfilepath
                        Name = $mbxObject.Name
                        AvailableNewMailboxSpace = $mbxObject.AvailableNewMailboxSpace
                    }
                }  
        }
    }
        
    write-host "Space Start $(Get-Date)"
    $Space = Get-Space -servers $servers |  
        Sort-Object Name | 
        ConvertTo-Html -Fragment -PreContent '<h2>Space Report</h2>'| Out-String   
    write-host "Space End $(Get-Date)"    
    


    \_(ツ)_/

    Friday, October 19, 2018 2:18 AM
  • jrv,

    Not sure what is going on but for some reason what you provided still is not working.  Data is basically blank for every variable.  I understand this is one way to skin the cat but is there a way to make what I originally posted work as I know this provides me data.

    Monday, October 22, 2018 7:39 PM