Question about Calculated Properties - hash tables

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Stanisław Wiśniewski Stanisław Wiśniewski 7 months, 2 weeks ago.

  • Author
    Posts
  • #59796

    Hello again,

    I have this:

    $CALC = "Name",
    @{Name="Phone";Expression={$_.telephoneNumber}},
    @{Name="Office";Expression={$_.office}},
    @{Name="Homepage";Expression={$_.homepage}},
    @{Name="Country";Expression={$_.co}},
    @{Name="Company";Expression={$_.company}},
    @{Name="Fax";Expression={$_.Fax}},
    @{Name="Script";Expression={$_.scriptpath}}
    #now count
    $COUNT = @{Name="Phone";Expression={$_.telephoneNumber.count}}

    The first one works exactly as i want.

    But i need second with will give me only counts for phones.

    But the result i get is:

    Phone
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1

    And so on till 152...
    I want to look like that at least

    Phone
    152
    152
    152
    152
    152
    152
    152
    152

    With is not really ideal – but -unique fix the problem and results look good in CSV.

    I can get this second output if put in full name of $:

    $COUNT = @{Name="Phone";Expression={$userspoland.telephoneNumber.count}}

    Why this happens?

    I can do it this way – but it will become bloated as i have many countries filtered...

  • #59803
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Well, $_ is 'this record' or the current record. You are getting the count of the phonenumber in the telephone attribute of the user. You don't have to filter each country, try using Group-Object:

    $users | Where {$_.telephone} | Group-Object -Property Country -NoElement | Sort-Object Count -Descending
    

    You could append them to your $Users with something like:

    $grouped = $users | Where {$_.telephone} | Group-Object -Property Country -NoElement | Sort-Object Count -Descending
    foreach ($group in $grouped) {
        $users | Add-Member -MemberType NoteProperty -Name ("TotalPhone{0}" -f $group.Name) -Value $group.Count
    }
    
  • #59805

    The only result it gives me:

    Add-Member : Cannot add a member with the name "TotalPhone" because a member with that name already exists. To overwrite the member anyway, add the Force parameter to
    your command.

    Other thing – is it possible to get phone number itself – and then how many times it is used?

  • #59881

    I done this way:

    $offices = {}
    $offices | select @{n="Phone"; e={($userspol | ? telephonenumber -eq "5698").count}},
    @{n="Office"; e={($userspol | ? office -eq "Warsaw").count}},
    @{n="Homepage"; e={($userspol | ? homepage -eq "google.pl").count}},
    @{n="Country"; e={($userspol | ? Co -eq "Poland").count}},
    @{n="Company"; e={($userspol | ? Company -eq "google").count}},
    @{n="Fax"; e={($userspol | ? Fax -EQ '+901').count}},
    @{n="Script"; e={($userspo | ? scriptpath -EQ 'warsaw\warsaw.bat').count}} | export-csv $LocationFileWarsaw\WarsawCOUNTS.CSV -NoTypeInformation

    I guess it can be done differently – but at least this way does what it should 😉

You must be logged in to reply to this topic.