Author Posts

December 8, 2016 at 4:12 pm

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...

December 8, 2016 at 5:10 pm

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
}

December 8, 2016 at 6:06 pm

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?

December 9, 2016 at 11:54 am

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 😉