Last active
September 29, 2021 23:27
-
-
Save theinventor/f765c14ee5cda25a0e94df00dc61ffae to your computer and use it in GitHub Desktop.
A sample powershell script with some functions to work with the SyncroMSP HTTP REST API
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 $env:SyncroModule | |
########################################################################################### | |
# First, some functions, these are just declarations that get used lower down the script. # | |
########################################################################################### | |
function Query-Syncro-Tickets ($SyncroSubdomain,$SyncroAPIKey,$Query) { | |
$uri = "https://$SyncroSubdomain.syncromsp.com/api/v1/tickets?api_key=$SyncroAPIKey&query=$Query" | |
$response = Invoke-RestMethod -Uri $uri | |
$response | |
} | |
function Query-Syncro-Assets ($SyncroSubdomain,$SyncroAPIKey,$Query) { | |
$uri = "https://$SyncroSubdomain.syncromsp.com/api/v1/customer_assets?api_key=$SyncroAPIKey&query=$Query" | |
$response = Invoke-RestMethod -Uri $uri | |
$response | |
} | |
function Create-Syncro-Ticket ($SyncroSubdomain,$SyncroAPIKey,$Subject, $CustomerID, $IssueType,$Status,$AssetName,$Created) { | |
IF($AssetName){ | |
$assets = Query-Syncro-Assets $SyncroSubdomain $SyncroAPIKey $AssetName | |
IF($assets.assets.count -gt 0){ | |
$AssetIds = @($assets.assets[0].id) | |
} ELSE { | |
$AssetIDs = $null | |
} | |
} ELSE { | |
$AssetIDs = $null | |
} | |
$NewTicket = @{ | |
customer_id=$CustomerID | |
subject=$Subject | |
problem_type=$IssueType | |
status="New" | |
asset_ids=$AssetIds | |
created_at=$Created | |
api_key=$SyncroAPIKey | |
} | |
$body = (ConvertTo-Json $NewTicket) | |
$url = "https://$SyncroSubdomain.syncromsp.com/api/v1/tickets" | |
$response = Invoke-RestMethod -Uri $url -Method Post -Body $body -ContentType 'application/json' | |
$response | |
} | |
function Create-Syncro-Ticket-Comment ($SyncroSubdomain,$SyncroAPIKey,$TicketID, $Subject, $Body, $Hidden, $DoNotEmail) { | |
$NewComment = @{ | |
ticket_id=$TicketID | |
subject=$Subject | |
body=$Body | |
hidden= if ($Hidden) {"1"} else {"0"} | |
do_not_email= if ($DoNotEmail) {"1"} else {"0"} | |
api_key=$SyncroAPIKey | |
} | |
$postBody = (ConvertTo-Json $NewComment) | |
$url = "https://$SyncroSubdomain.syncromsp.com/api/v1/tickets/$TicketID/comment" | |
$response = Invoke-RestMethod -Uri $url -Method Post -Body $postBody -ContentType 'application/json' | |
$response | |
} | |
function Create-Syncro-Ticket-TimerEntry ($SyncroSubdomain,$SyncroAPIKey,$TicketID, $StartTime, $DurationMinutes, $Notes, $UserID) { | |
#$StartTime needs to be formatted for a computer to read, the best format is "2018-02-14 15:30" | |
# You can use powershell with 'Get-Date -Format o' and that will work nicely. | |
# If you wanted to get "30 minutes ago and formatted" it works like this (Get-Date).AddMinutes(-30).toString("o") | |
$NewTimer = @{ | |
ticket_id=$TicketID | |
start_at=$StartTime | |
duration_minutes=$DurationMinutes | |
notes=$Notes | |
user_id= if ($UserID) {$UserID} else {$null} | |
api_key=$SyncroAPIKey | |
} | |
$postBody = (ConvertTo-Json $NewTimer) | |
$url = "https://$SyncroSubdomain.syncromsp.com/api/v1/tickets/$TicketID/timer_entry" | |
$response = Invoke-RestMethod -Uri $url -Method Post -Body $postBody -ContentType 'application/json' | |
$response | |
} | |
########################################################################################### | |
############################## END of Function area ####################################### | |
########################################################################################### | |
$SyncroSubdomain = "repairshopr" | |
$SyncroAPIKey = "YOUR_KEY_HERE_BE_CAREFUL_WITH_IT" | |
### Sample usage ### | |
#$result = Query-Syncro-Tickets $SyncroSubdomain $SyncroAPIKey "Virus detected" | |
#$ticket = $result.tickets[0] | |
#$ticketID = $ticket.id | |
#$assetresult = Query-Syncro-Assets $SyncroSubdomain $SyncroAPIKey $env:computername | |
#$customerID = $assetresult.assets[0].customer_id | |
#$newticket = create-syncro-ticket $syncrosubdomain $syncroapikey "Test subject 1" $customerID "Hardware" "New" $env:computername | |
#$newcomment = Create-Syncro-ticket-comment $syncrosubdomain $syncroapikey $newticket.ticket.id "Contacted" "Just a cool comment from powershell!" $False $False | |
#$newtimerentry = create-syncro-ticket-timerentry $syncrosubdomain $syncroapikey $newticket.ticket.id (Get-Date).AddMinutes(-30).toString("o") 30 "Removed a virus" $null | |
### End sample usage ### | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment