Last active
July 11, 2017 16:49
-
-
Save auberginehill/074df92d7ef7103d37374ab20355840d to your computer and use it in GitHub Desktop.
Tries to retrieve a "number of views" -number from a Google My Maps map and write it to a log file (map_log.csv at $env:temp) - a Windows PowerShell script.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Get-MapUsage.ps1 | |
#> | |
# Description: Tries to retrieve a "number of views" -number from a Google My Maps map and write it to a log file (map_log.csv at $env:temp). | |
# Example URL: https://www.google.com/maps/d/viewer?mid=QuiteAFewRandomLettersAndNumbers | |
# Note: Please replace the string below (between the single quotation marks) with a correct Google My Maps mid, which could be | |
# either (A) the string after = symbol in the original URL | |
# or (B) the string after = symbol until the first & symbol (not including either = or & characters in the original URL) | |
$google_my_maps_mid = 'QuiteAFewRandomLettersAndNumbers' | |
# Check if the computer is connected to the Internet | |
# Credit: ps1 "Test Internet connection": http://powershell.com/cs/blogs/tips/archive/2011/05/04/test-internet-connection.aspx | |
$empty_line = "" | |
If (([Activator]::CreateInstance([Type]::GetTypeFromCLSID([Guid]'{DCB00C01-570F-4A9B-8D69-199FDBA5723B}')).IsConnectedToInternet) -eq $false) { | |
$empty_line | Out-String | |
Return "The Internet connection doesn't seem to be working. Exiting without checking any Google My Maps usage statistics." | |
} Else { | |
$path = $env:temp | |
# $timestamp = Get-Date -Format g | |
$timestamp = Get-Date -Format "d.M.yyyy HH:mm" | |
$start_time = Get-Date -Format HH:mm:ss | |
$empty_line | Out-String | |
$statistics = @() | |
$url = "https://www.google.com/maps/d/viewer?mid=$google_my_maps_mid" | |
$download_text = "$start_time - Reading a Google My Maps web page at $url" | |
Write-Output $download_text | |
} # Else | |
# Try to retrieve a Google My Maps web page as a string | |
Try { | |
If ($PSVersionTable.PSVersion.Major -ge 3) { | |
# Use Invoke-WebRequest cmdlet with Windows PowerShell 3.0 and with above versions of Windows PowerShell. | |
$raw_map = Invoke-WebRequest $url | |
$data = $raw_map.Content | |
} Else { | |
# Use WebClient.DownloadString Method (NET Framework 2.0 and above) with older versions of Windows PowerShell. | |
$web_crawler = New-Object System.Net.WebClient | |
$data = $web_crawler.DownloadString($url) | |
} # Else (If $PSVersionTable.PSVersion.Major) | |
} Catch { | |
$empty_line | Out-String | |
Write-Warning $_.Exception | |
$help_text = "Please double check that the Google My Maps MID string is typed correctly on line 12 in this Get-MapUsage.ps1 script." | |
$empty_line | Out-String | |
Write-Output $help_text | |
$empty_line | Out-String | |
Exit | |
} # Try | |
# Extract the number of views from the data | |
# Credit: http://www.lazywinadmin.com/2014/09/powershell-tip-escape-regex.html | |
$string = [string]'kml?mid\\u003d' + $google_my_maps_mid + '\",' | |
$raw_number = ($data -split [System.Text.RegularExpressions.Regex]::Escape($string))[-1] | |
$regex = $raw_number -match "\d+" | |
$number = $matches | Select-Object -ExpandProperty Values | |
$raw_date = ($timestamp).Split(" ")[0] | |
$day = ($raw_date).Split(".")[0] | |
$month = ($raw_date).Split(".")[1] | |
$year = ($raw_date).Split(".")[2] | |
$time = (($timestamp).Split(" ")[-1]).Replace(".",":") | |
$hour = ($time).Split(":")[0] | |
$minute = ($time).Split(":")[1] | |
$statistics += $obj_stats = New-Object -TypeName PSCustomObject -Property @{ | |
'Date' = $timestamp | |
'Year' = $year | |
'Month' = $month | |
'Day' = $day | |
'Hour' = $hour | |
'Minute' = $minute | |
'Time' = $time | |
'Number' = $number | |
} # New-Object | |
$statistics.PSObject.TypeNames.Insert(0,"Statistics") | |
$statistics_selection = $statistics | Select-Object 'Date','Year','Month','Day','Hour','Minute','Time','Number' | |
If ($number -ne $null) { | |
# Make a log entry | |
# Note: Append parameter of Export-Csv was introduced in PowerShell 3.0. | |
# Source: http://stackoverflow.com/questions/21048650/how-can-i-append-files-using-export-csv-for-powershell-2 | |
# Source: https://blogs.technet.microsoft.com/heyscriptingguy/2011/11/02/remove-unwanted-quotation-marks-from-csv-files-by-using-powershell/ | |
$logfile_path = "$path\map_log.csv" | |
$empty_line | Out-String | |
$end_time = Get-Date -Format HH:mm:ss | |
$log_text = "$end_time - Writing the logfile at $logfile_path" | |
Write-Output $log_text | |
If ((Test-Path $logfile_path) -eq $false) { | |
$statistics_selection | Export-Csv $logfile_path -Delimiter ';' -NoTypeInformation -Encoding UTF8 | |
} Else { | |
# $statistics_selection | Export-Csv $logfile_path -Delimiter ';' -NoTypeInformation -Encoding UTF8 -Append | |
$statistics_selection | ConvertTo-Csv -Delimiter ';' -NoTypeInformation | Select-Object -Skip 1 | Out-File -FilePath $logfile_path -Append -Encoding UTF8 | |
(Get-Content $logfile_path) | ForEach-Object { $_ -replace ('"', '') } | Out-File -FilePath $logfile_path -Force -Encoding UTF8 | |
} # Else (If Test-Path $logfile_path) | |
$empty_line | Out-String | |
Write-Output $number | |
$empty_line | Out-String | |
} Else { | |
$continue = $true | |
} # If $number -ne $null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[reserved]