Thursday, February 6, 2014

Quickie - IndexOf

Working on a simple report using the scripts posted yesterday I needed a way to update objects for each person.

For example, if I build an array like:

$Array = get-mailbox  | select displayname, @{Name="DB",Expression={$_.database.tostring()}},@{Name="MailboxSize",Expression={(Get-mailboxStatistics $_.identity).totalItemSize.ToMB()}},@{Name="ActiveSyncDeviceCount",Expression={0}}



This will create an array of all mailboxes on the system, including the database they are on, the size of the mailbox and an empty field for the # of active sync devices associated with the mailbox.

Now, if I isolate a specific person in that array...

$FoundMailbox = $array | Where { $_.displayname -eq "Eric Woodford"}

$FoundMailbox should be a single object containing my information.

$FoundMailbox | FL

Displayname: Eric Woodford
DB: IT-DB01
MailboxSize: 1024
ActiveSyncDeviceCount: 0

Now, I did something like:

$ASDevices = get-activeSyncDevice -mailbox $FoundMailbox.Displayname
if ($ASDevices -ne $null) {     # If there actually is a device continue
   $ASCount = 1                    # Assume that there is only 1 device attached.
   if ($asDevice -is [array]) {   # If the returned value is an array, get the count of items in the array.
       $ASCount = $asDevice.count
   } 
}
 
If they have a device(s) associated with their mailbox, $ASCount will equal that number of devices. But to record the value, I need to update the original object in the array.

$ASDevices = get-activeSyncDevice -mailbox $FoundMailbox.Displayname
if ($ASDevices -ne $null) {     # If there actually is a device continue
   $ASCount = 1                    # Assume that there is only 1 device attached.
   if ($asDevice -is [array]) {   # If the returned value is an array, get the count of items in the array.
       $ASCount = $asDevice.count
   } 
   $ndx = [array]::IndexOf($array, $FoundMailbox)
   $Array[$ndx].ActiveSyncDeviceCount = $ASCount
}

When I cycle through all my mailboxes, I now have an array of objects each with their active device count.

$FoundMailbox = $array | Where { $_.displayname -eq "Eric Woodford"}
$FoundMailbox | FL

Displayname: Eric Woodford
DB: IT-DB01
MailboxSize: 1024
ActiveSyncDeviceCount: 2



No comments:

Post a Comment