Last active
November 17, 2020 03:16
-
-
Save davejlong/885ccd4a099b2e92b75a89726b2bc217 to your computer and use it in GitHub Desktop.
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
Import-Module -Name PSAtera | |
Import-Module -Name PnP.PowerShell | |
$CustomFieldLabel = "Scheduled For" | |
$SharePointSite = "https://contoso.sharepoint.com/sites/Contoso" | |
$List = "Calendar" | |
# I don't know enough about the PnP Module to connect to | |
# SharePoint better with MFA enabled | |
Write-Host "Connecting to SharePoint Online..." -NoNewline | |
Connect-PnpOnline -Url $SharePointSite -PnPManagementShell -LaunchBrowser | |
Write-Host "✔" -ForegroundColor Green | |
Write-Host "Getting Open and Pending Tickets..." -NoNewline | |
$Tickets = Get-AteraTicketsFiltered -Open -Pending | |
Write-Host "✔" -ForegroundColor Green | |
Write-Host "Getting events already on calendar..." -NoNewline | |
$PnPQuery = @' | |
<View> | |
<ViewFields> | |
<FieldRef Name="Title"/> | |
</ViewFields> | |
<Query> | |
<Where> | |
<Geq> | |
<FieldRef Name="EventDate"/> | |
<Value type="DateTime"><Today /></Value> | |
</Geq> | |
</Where> | |
</Query> | |
</View> | |
'@ | |
$ListItems = Get-PnPListItem -List $List -Query $PnPQuery | |
Write-Host "✔" -ForegroundColor Green | |
# Loop through all open and pending tickets to look for ones that are scheduled | |
Write-Host "Processing tickets..." | |
$Tickets | ForEach-Object { | |
$TicketID = $_.TicketID | |
$CustomValue = Get-AteraCustomValue -ObjectType "Ticket" -ObjectID $_.TicketID -FieldName $CustomFieldLabel | |
if($null -ne $CustomValue.ValueAsDateTime -and (Get-Date $CustomValue.ValueAsDateTime) -ge (Get-Date)) { | |
Write-Host "Processing $($_.TicketID) ($($_.TicketTitle))" | |
$CalendarTicket = @{ | |
Title = "[$($_.TicketID)] $($_.TicketTitle)"; | |
EventDate = Get-Date $CustomValue.ValueAsDateTime; | |
EndDate = Get-Date $CustomValue.ValueAsDateTime; | |
fAllDayEvent = $true; | |
Location = "https://app.atera.com/Admin#/ticket/$($_.TicketID)"; | |
} | |
$Item = $ListItems | Where-Object { $_.FieldValues.Title -match "^\[$($TicketID)\]" } | |
Write-Host "Search for ticket $($_.TicketID): $Item" | |
if ($null -eq $Item) { | |
Write-Host "Adding new calendar entry for $($_.TicketID)..." -NoNewline | |
Add-PnpListItem -List $List -Values $CalendarTicket | |
Write-Host "✔" -ForegroundColor Green | |
} else { | |
Write-Host "Updating existing calendar entry for $($_.TicketID)..." -NoNewline | |
Set-PnpListItem -List $List -Identity $Item.Id -Values $CalendarTicket | |
Write-Host "✔" -ForegroundColor Green | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment