Get value from higher pipe level

Welcome Forums General PowerShell Q&A Get value from higher pipe level

This topic contains 5 replies, has 3 voices, and was last updated by

 
Participant
4 years, 3 months ago.

  • Author
    Posts
  • #17704

    Participant
    Points: 0
    Rank: Member

    I'm getting mobile device stats from Exchange against each box, but get-mobiledevicestatistics unfortunately doesn't return a friendly name of the box it's retrieving — the closest thing is the Identity field, which spits out the full CN with the username crammed in the middle. Here's what I'm using:

    $UserList = Get-CASMailbox -Filter {hasactivesyncdevicepartnership -eq $true -and -not displayname -like "CAS_{*"} | Get-Mailbox
    
    $UserList | foreach { Get-MobileDeviceStatistics -Mailbox $_.alias} | FL $userlist.Name, DeviceModel, LastSuccessSync

    Is there a way to retrieve the name field from the initial command? The "$userlist.Name" throws this:
    FL : Cannot convert System.Object[] to one of the following types {System.String,
    System.Management.Automation.ScriptBlock}.

  • #17705

    Participant
    Points: 159
    Helping Hand
    Rank: Participant

    Try a calculated expression:

    $UserList | foreach { Get-MobileDeviceStatistics -Mailbox $_.alias | Select DeviceModel, LastSuccessSync, @{Label="Name";Expression={$_.Name}}} | FL Name, DeviceModel, LastSuccessSync
  • #17710

    Participant
    Points: 0
    Rank: Member

    No error, but no values, either; "Name" is blank. Technically that's trying to call "Name" from Get-MobileDeviceStatistics, yes? And there's no property named that on that command. I need to somehow grab "Name" from "Get-Mailbox" on each ForEach.

    Would there be a way to cheat, of sorts, by writing the object's name to an out file and then running the mobile stats command? So, each time the loop ran, it'd write the Get-Mailbox value "Name", then run Get-MobileDeviceStatistics and write that to the file?

  • #17711

    Participant
    Points: 0
    Rank: Member

    What do you know, it worked! Here's my modified code, in case people would like to see.

    $UserList = Get-CASMailbox -Filter {hasactivesyncdevicepartnership -eq $true -and -not displayname -like "CAS_{*"} | Get-Mailbox
    
    $UserList | ForEach-Object {
        get-mailbox $_.alias | Select-Object -Property Name | Out-File -Append d:\mobiles.txt
        Get-MobileDeviceStatistics -Mailbox $_.alias | ft -Property DevicePhoneNumber, DeviceModel, DeviceOS, FirstSyncTime -AutoSize | out-file -Append d:\mobiles.txt
        }
  • #17713

    Moderator
    Points: 24
    Team Member
    Rank: Member

    Hello Riley,

    Here a modified variant of your first post which will add the mailbox name to the object returned by Get-MobileDeviceStatistics using the Add-Member cmdlet (a trick I've learned at the PowerShell Summit 2014 earlier this year).

    Get-CASMailbox -Filter { hasactivesyncdevicepartnership -eq $true -and -not displayname -like "CAS_{*" } | Get-Mailbox | ForEach-Object -Process { Get-MobileDeviceStatistics -Mailbox $_ | Add-Member -MemberType NoteProperty -Name MailboxName -Value $_.Name -PassThru } | Select-Object -Property MailboxName, DeviceModel, LastSuccessSync
    

    Regards,
    Daniel

  • #17725

    Participant
    Points: 159
    Helping Hand
    Rank: Participant

    Your initial post indicated that there was a Name property in $UserList. If you need to pull it from another command, you can do something like this:

    #Create a blank object
    $results = @()
    $UserList = Get-CASMailbox -Filter {hasactivesyncdevicepartnership -eq $true -and -not displayname -like "CAS_{*"} | Get-Mailbox
     
    #Take results returned data from foreach  and put it in the $results object
    $results = $UserList | ForEach-Object {
        #Create a variable and extract the name returned from Get-MailBox
        $MailBoxName = get-mailbox $_.alias | Select-Object -ExpandProperty Name 
        #Add the variable using a calculated property 
        Get-MobileDeviceStatistics -Mailbox $_.alias | ft -Property DevicePhoneNumber, DeviceModel, DeviceOS, FirstSyncTime, @{Label="Name";Expression={$MailBoxName}} -AutoSize
    }
    
    $results
    $results | Export-CSV C:\MailboxInfo.csv

    Also, in PowerShell, it's better practice to generate an object versus appending line by line to a file. So, try this and see if it works as expected.

The topic ‘Get value from higher pipe level’ is closed to new replies.