here is my code so far. It basically takes a google calendar event url and attempts to pull the event ID out of what it is given and then run a command.
function ParseGoogleEvent {
param (
[parameter(Mandatory = $true,
HelpMessage = "Enter entire url of event or event ID"
)][string]$EventURL,
[parameter(Mandatory = $False,
HelpMessage = "Enter to forum full GAM Command"
)][string]$Email,
[parameter(Mandatory = $False,
HelpMessage = "Will run the GAM command with -Email"
)][switch]$Run
)
Try {
[uri]$TestURI = $EventURL
$TestURI = $TestURI.segments[6]
}
Catch {
$TestURI = $TestURI.OriginalString
}
function Get-PaddingRequired {
param (
[Parameter(Mandatory = $true)]
[String]$InputString
)
$modulo = ($InputString.Length % 3)
switch ($modulo) {
0 { $paddingRequired = $null }
1 { $paddingRequired = '==' }
2 { $paddingRequired = '=' }
}
Return [string]$paddingRequired
}
$Padding = Get-PaddingRequired -InputString $TestURI
$TestURI = "$TestURI $Padding"
Try {
$Result = [System.Text.Encoding]::Default.GetString([System.Convert]::FromBase64String($TestURI))
}
Catch {
Write-Warning "Problem with input of $EventURL.`nTry entire url."
Return $null
}
$Result = $Result.split(" ")[0]
IF ([string]::IsNullOrWhiteSpace($Email)) {
Write-Warning "GAM calendar <Email> info event id $Result"
}
Else {
Write-Warning "GAM calendar $Email info event id $Result"
If ($Run) {
$File = $Email 'Event.txt'
GAM calendar $Email info event id $Result > $File
Write-Host $File
}
}
Return [string]$Result
}
The problem with my code is it will not account for the various ways the string comes in.
My code can can currently handle 2 out of 3 was.
Can't manage. (The following comes from google calendar diagnostics)
Event{eid=69uqr7fedmm5elmtout52hu0or_20211006T160000Z, <Blah blah}
Because it is already parsed for the most part
My code can handle.
https://calendar.google.com/calendar/u/0/r/eventedit/Njl1cXI3ZmVkbW01ZWxtdG91dDUyaHUwb3JfMjAyMTEwMDhUMTYwMDAwWiBkYW4uY2FzbWFzQHNvbm9zLmNvbQ
and
Njl1cXI3ZmVkbW01ZWxtdG91dDUyaHUwb3JfMjAyMTEwMDhUMTYwMDAwWiBkYW4uY2FzbWFzQHNvbm9zLmNvbQ
Any good ideas on how to find the event ID for all 3 examples ?
CodePudding user response:
You could use the -match
operator to test if the input string comes in the non-base64 format like this:
# $EventURL = 'Event{eid=69uqr7fedmm5elmtout52hu0or_20211006T160000Z, <Blah blah}'
if ($EventURL -match 'Event\{.*?\beid=(\w ).*?\}') {
# The Event{...} format was passed, grab the eid value from the capture group above
$Result = $Matches[1]
}
else {
# your existing code parsing the URL/base64 format goes here
}
if ([string]::IsNullOrWhiteSpace($Email)) {
Write-Warning "GAM calendar <Email> info event id $Result"
}
else {
Write-Warning "GAM calendar $Email info event id $Result"
if ($Run) {
$File = $Email 'Event.txt'
GAM calendar $Email info event id $Result > $File
Write-Host $File
}
}
return [string]$Result