Measure-TechNetContribution.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<#PSScriptInfo
 
.VERSION 1.0
 
.GUID 5c48978f-a2a6-4422-9919-a63e193ae0bd
 
.AUTHOR Evgenij Smirnov
 
.COMPANYNAME it-pro-berlin.de
 
.COPYRIGHT
 
.TAGS technet forum contribution community
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS Export-TechNetContributionToCSV
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
#>


<#
.SYNOPSIS
 Processes the CSV file generated by Export-TechNetContributionToCSV.ps1 and counts replies, answers, proposed answers and upvotes per Forum (or Subforum) category.
 
.DESCRIPTION
 This script processes the CSV file generated by Export-TechNetContributionToCSV.ps1 and counts replies, answers, proposed answers and upvotes per Forum (or Subforum) category.
 The output is a collection of objects (forum or subforum, #replies, #answers, #proposed answers, #upvotes, date of first and last post) that can be piped to Out-GridView, Format-Table or wherever you like.
 
.PARAMETER FilePath
 Full path to CSV file that shouls be processed. The default value is identical to that of Export-TechNetContributionToCSV.ps1 so that you can omit this parameter in both scripts if you do not need the CSV stored in a particular folder.
 
.PARAMETER FromDate
 Sets the begin of the date range. If omitted or passed a value that cannot be converted to a datetime object, 01.01.1601 will be set. As far as we know, TechNet wasn't around back then so it should be fairly safe.
 
.PARAMETER ToDate
 Set the end of the date range. If omitted or passed a value that cannot be converted to a datetime object, the currend date and time will be assumed.
 
.PARAMETER MainForums
 If specified, the contribution will be categorised by main TN forum; otherwise, by subforum.
 
.NOTES
 No error handling whatsoever has been implemented to prevent having the script process a foreign CSV structure.
 
#>
 

[CmdletBinding()]
Param(
   [Parameter(Mandatory = $false, Position = 1, valueFromPipeline = $false)][string]$FilePath = "$($env:TEMP)\my_technet_contribution.csv"
  ,[Parameter(Mandatory = $false)][string]$FromDate = "01.01.1601 00:00:00"
  ,[Parameter(Mandatory = $false)][string]$ToDate = (Get-Date -Format "dd.MM.yyyy HH:mm:ss")
  ,[Parameter(Mandatory = $false,HelpMessage="If specified, the contribution will be categorised by main TN forum; otherwise, by subforum.")][switch]$MainForums
)
If (Test-Path $FilePath) {
    $rawdata = Import-CSV $FilePath -Delimiter ";" 
    $i = 0
    try {
        $dtlow = Get-Date $FromDate
    } catch {
        Write-Host "WARNING! Cannot convert FromDate value of $FromDate to DateTime, assuming 01.01.1601..." -ForegroundColor Yellow
        $dtlow = Get-Date "01.01.1601 00:00:00"
    }
    try {
        $dthigh = Get-Date $ToDate
    } catch {
        Write-Host "WARNING! Cannot convert ToDate value of $ToDate to DateTime, assuming NOW..." -ForegroundColor Yellow
        $dthigh = Get-Date
    }
    $contrib = $rawdata | where {((Get-Date $_.TimeStamp) -ge $dtlow) -and ((Get-Date $_.TimeStamp) -le $dthigh)}
    if ($MainForums) {
        $sub_forums = $contrib.Forum | Sort -Unique    
    } else {
        $sub_forums = $contrib.Subforum | Sort -Unique    
    }
    $contrib_replies = @{}
    $contrib_answers = @{}
    $contrib_proposed = @{}
    $contrib_upvotes = @{}
    $contrib_earliest = @{}
    $contrib_latest = @{}
    foreach ($sf in $sub_forums) {
        $contrib_replies.Add($sf,0)
        $contrib_answers.Add($sf,0)
        $contrib_proposed.Add($sf,0)
        $contrib_upvotes.Add($sf,0)
        $contrib_earliest.Add($sf,(Get-Date))
        $contrib_latest.Add($sf,(Get-Date "01.01.1601 00:00:00"))
    }
    foreach ($entry in $contrib) {
        if ($MainForums) {
            if ($contrib_earliest."$($entry.Forum)" -gt (Get-Date $entry.TimeStamp)) { $contrib_earliest."$($entry.Forum)" = (Get-Date $entry.TimeStamp) }
            if ($contrib_latest."$($entry.Forum)" -lt (Get-Date $entry.TimeStamp)) { $contrib_latest."$($entry.Forum)" = (Get-Date $entry.TimeStamp) }
            $contrib_upvotes."$($entry.Forum)" += $entry.Votes
            $contrib_replies."$($entry.Forum)" ++
            if ($entry.MsgKind -eq "proposed") { $contrib_proposed."$($entry.Forum)" ++ }
            if ($entry.MsgKind -eq "answer") { $contrib_answers."$($entry.Forum)" ++ }
        } else {
            if ($contrib_earliest."$($entry.SubForum)" -gt (Get-Date $entry.TimeStamp)) { $contrib_earliest."$($entry.SubForum)" = (Get-Date $entry.TimeStamp) }
            if ($contrib_latest."$($entry.SubForum)" -lt (Get-Date $entry.TimeStamp)) { $contrib_latest."$($entry.SubForum)" = (Get-Date $entry.TimeStamp) }
            $contrib_upvotes."$($entry.SubForum)" += $entry.Votes
            $contrib_replies."$($entry.SubForum)" ++
            if ($entry.MsgKind -eq "proposed") { $contrib_proposed."$($entry.SubForum)" ++ }
            if ($entry.MsgKind -eq "answer") { $contrib_answers."$($entry.SubForum)" ++ }
        }
    }
    foreach ($sf in $sub_forums) {
        $oo = New-Object PSCustomObject
        $oo | Add-Member -MemberType NoteProperty -Name "ForumCategory" -Value $sf
        $oo | Add-Member -MemberType NoteProperty -Name "Replies" -Value "$($contrib_replies."$sf")"
        $oo | Add-Member -MemberType NoteProperty -Name "Answers" -Value "$($contrib_answers."$sf")"
        $oo | Add-Member -MemberType NoteProperty -Name "Proposed" -Value "$($contrib_proposed."$sf")"
        $oo | Add-Member -MemberType NoteProperty -Name "Upvotes" -Value "$($contrib_upvotes."$sf")"
        $oo | Add-Member -MemberType NoteProperty -Name "FirstPost" -Value "$($contrib_earliest."$sf")"
        $oo | Add-Member -MemberType NoteProperty -Name "LastPost" -Value "$($contrib_latest."$sf")"
        $oo
    }
} else {
    Write-Host "File not found: $FilePath" -ForegroundColor Red
}