Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sam-Martin/cffb35d1f593202525c3 to your computer and use it in GitHub Desktop.
Save Sam-Martin/cffb35d1f593202525c3 to your computer and use it in GitHub Desktop.
Sync ServiceNow to Leankit using PowerShell
Import-Module PSLeankit
Import-Module PSServiceNow
# Set-LeanKitAuth
# Set-ServiceNowAuth
# We need to know the boardID, we'll infer any other information
$boardID = [array](Find-LeanKitBoard) | ?{$_.Title -eq "Sam's Board"} | select -ExpandProperty id;
# Set defaults
$board = Get-LeankitBoard -boardID $boardID
$DropLaneID = $board.DefaultDropLaneId
$doneLaneID = $board.lanes | ?{$_.type -eq 3} | select -first 1 | select -ExpandProperty id
$CardTypeID = $board.CardTypes | ?{$_.name -eq "Task"} | select -ExpandProperty id;
#$UserGroup = Get-ServiceNowUserGroup -MatchExact @{'name'='Cloud Ops Architect Team '}
$User = Get-ServiceNowUser -MatchExact @{name="Sam Martin"}
# Get cards and incidents
$cards = $board.Lanes.cards + $board.Archive + $board.Backlog.Cards
#$incidents = Get-ServiceNowIncident -MatchExact @{'assignment_group'=$UserGroup.sys_id;'active'='true'} -Limit 999
$incidents = Get-ServiceNowIncident -MatchExact @{'assigned_to'=$user.sys_id;'active'='true'} -Limit 999
$PriorityMapping = @{
"1 - Critical" = 3
"2 - High" = 2
"3 - Medium" = 1
"4 - Low" = 0
}
$stateMapping = @{
default = $DropLaneID
Resolved = $doneLaneID
}
$CategoryMapping = @{
default = $board.CardTypes | ?{$_.name -eq "Task"}
<# Request = $board.CardTypes | ?{$_.name -eq "Task"}
Server = $board.CardTypes | ?{$_.name -eq "Server"}
Application = $board.CardTypes | ?{$_.name -eq "Application"}
Database = $board.CardTypes | ?{$_.name -eq "Database"}
Network = $board.CardTypes | ?{$_.name -eq "Network"}
Maintenance = $board.CardTypes | ?{$_.name -eq "Maintenance"}
Internal = $board.CardTypes | ?{$_.name -eq "Internal"}
"Sales Support" = $board.CardTypes | ?{$_.name -eq "Sales Support"}
Security = $board.CardTypes | ?{$_.name -eq "Security"} #>
}
# Compare the cards with the incidents, and create cards for any incidents which don't have them already
foreach($incident in $incidents){
$card = $($cards| ?{$_.ExternalCardID -eq $incident.number})
$cardType = if($CategoryMapping.($incident.category)){$CategoryMapping.($incident.category)}else{$CategoryMapping.default}
if($stateMapping.($incident.state)){
$laneID = $stateMapping.($incident.state)
}if($cardID){
$laneID = $card.LaneId
}else{
$laneID = $stateMapping.default
}
if(!$cardID){
$result = Add-LeanKitCard -BoardID $boardID `
-Title $incident.short_description `
-Description $incident.description `
-ExternalCardID $incident.number `
-Priority $PriorityMapping.($incident.priority) `
-CardTypeID $CardTypeID `
-LaneID $laneID `
-UserWipOverrideComment 'None'
}else{
$result = Update-LeanKitCard -BoardID $boardID `
-CardID $card.id `
-Title $incident.short_description `
-Description $incident.description `
-ExternalCardID $incident.number `
-Priority $PriorityMapping.($incident.priority) `
-CardTypeID $CardTypeID `
-LaneID $laneID `
-UserWipOverrideComment 'None'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment