/
Popular Cmdlet v2.ps1
67 lines (48 loc) · 1.79 KB
/
Popular Cmdlet v2.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
#
# Popularity (via Bing) of the PowerShell cmdlets in a module
#
#
# Function to see how many pages Bing finds for a given term
#
# Adds assembly to support URL encoding and the web client
Add-Type -Assembly System.Web
$WebClient = New-Object system.Net.WebClient
Function Get-BingCount([string] $Term) {
# Add plus and quotes, encodes for URL
$Term = '+"' + $Term + '"'
$Term = [System.Web.HttpUtility]::UrlEncode($Term)
# Load the page as a string
$URL = "http://www.bing.com/search?q=" + $Term
$Page = $WebClient.DownloadString($URL)
# searches for the string before the number of hits on the page
$String1 = '<span class="sb_count">'
$Index1 = $Page.IndexOf($String1)
# if found the right string, finds the exact end of the number
If ($Index1 -ne -1) {
$Index1 += $String1.Length
$Index2 = $Page.IndexOf(" ", $Index1)
$result = $Page.Substring($Index1, $Index2 - $index1)
} else { $result = "0" }
# Return the count
return $result
}
#
# Main code
#
$CmdletList = Get-Command -Module Storage | Select Name, ModuleName
$CmdletCount = $CmdletList.Count -1
0..$CmdletCount | % {
# Tracks progress
Write-Progress -Activity "Checking cmdlet popularity" -PercentComplete ($_ * 100 / $CmdletCount)
# Check the popularity with Bing
$count = [int64] (Get-BingCount $CmdletList[$_].Name)
# Format as a row, output it
$Row = "" | Select CmdletName, ModuleName, BingCount
$Row.CmdletName = $CmdletList[$_].Name
$Row.ModuleName = $CmdletList[$_].ModuleName
$Row.BingCount = $count
$Row
}
Write-Progress -Activity "Checking cmdlet popularity" -Completed
# Releases resources used by the web client
$WebClient.Dispose()