Pipedrive.psm1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
$baseUrl = "https://api.pipedrive.com/v1"; <# .SYNOPSIS Connects to Pipedrive. .DESCRIPTION Credentials can be persisted using https://www.powershellgallery.com/packages/CredentialsManager/ by setting the parameter UseCredentialsManager to $True. o $Credential -eq $Null -and $UseCredentialsManager -eq $Null -> credentials are requested and are not persisted o $Credential -ne $Null -and $UseCredentialsManager -eq $Null -> passed credentials are used and not persisted o $Credential -ne $Null -and $UseCredentialsManager -ne $Null -> passed credentials are used and persisted o $Credential -eq $Null -and $UseCredentialsManager -ne $Null -> Persisted credentials are used .PARAMETER Credential Credential to access Pipedrive with. I not provided they get requested. .PARAMETER UseCredentialsManager If $True, credentials are read and written using the CredentialsManager. Only Password part, containing the Token, is used. #> function Connect-PD { [CmdletBinding()] Param( [PSCredential]$Credential, [Boolean]$UseCredentialsManager = $False ) #end param # check module prerequisites if($UseCredentialsManager) { $module = Get-Module -ListAvailable -Name "CredentialsManager"; if (!$module) { throw "Module 'CredentialsManager' needed. Please install executing 'Install-Module -Name CredentialsManager' as Administrator."; } } if($UseCredentialsManager -and $Credential -eq $Null) { $Credential = Read-Credential -ListAvailable | Where { $_.Environment -eq "Pipedrive" } } if(!$Credential) { $Credential = Get-Credential -Message "Please enter Token for Pipedrive as password (username can be anything)."; } if($UseCredentialsManager) { Write-Credential "Pipedrive" -Credential $Credential; } $script:token = $Credential.GetNetworkCredential().password ; } <# .SYNOPSIS Get Pipeline by Name from Pipedrive .DESCRIPTION .PARAMETER Name Name of Pipeline to get. if omitted all Pipelines are returned. #> function Get-PDPipeline { [CmdletBinding()] Param( [Parameter(Mandatory=$true)][string]$Name ) if(!$script:token) { Connect-PD -UseCredentialsManager $True; } $pipelines = Invoke-RestMethod -Uri "${baseUrl}/pipelines?api_token=${script:token}" -Method Get -ContentType "application/json; charset=utf-8"; foreach($pipeline in $pipelines.data) { if($pipeline.name -eq $Name) { return $pipeline; } } throw "No Pipeline having name ${Name} found." } <# .SYNOPSIS Get Stage from a Pipeline by Name. .DESCRIPTION .PARAMETER PipelineId Id of Pipeline the stage is in. .PARAMETER StageName Name of Stage to get. #> function Get-PDStage { [CmdletBinding()] Param( [Parameter(Mandatory=$true)][int]$PipelineId, [Parameter(Mandatory=$true)][string]$StageName ) if(!$script:token) { Connect-PD -UseCredentialsManager $True; } $stages = Invoke-RestMethod -Uri "${baseUrl}/stages?pipeline_id=${PipelineId}&api_token=${script:token}" -Method Get -ContentType "application/json; charset=utf-8"; foreach($stage in $stages.data) { if($stage.name -eq $StageName) { return $stage; } } throw "No Stage having name ${StageName} found." } <# .SYNOPSIS Get Deals from Pipedrive .DESCRIPTION .PARAMETER Term Search Term to look for #> function Get-PDDeal { [CmdletBinding()] Param( [Parameter(Mandatory=$true)][string]$Term ) if(!$script:token) { Connect-PD -UseCredentialsManager $True; } $parameters = @{ term=$Term; api_token=$script:token; } $uri = New-HttpQueryString "${baseUrl}/deals/find" $parameters return $(Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json; charset=utf-8"); } <# .SYNOPSIS Adds a deal to a Pipeline .DESCRIPTION .PARAMETER Title Title of deal .PARAMETER OrganizationId Id of Organization to assign Deal to .PARAMETER PersonId Id of Person to assign Deal to .PARAMETER StageId Id of Stage to assign Deal to. If omitted, the deal will be placed in the first stage of the pipeline. .PARAMETER Value Value of the deal. If omitted, value will be set to 0. .PARAMETER ExpectedCloseDate ExpectedCloseDate to assign to deal. #> function Add-PDDeal { [CmdletBinding()] Param( [Parameter(Mandatory=$true)][string]$Title, [int]$OrganizationId, [int]$PersonId, [int]$StageId, [float]$Value = 0, [datetime]$ExpectedCloseDate, [string][ValidateSet('open','won','lost','deleted')]$Status ) #end param if(!$script:token) { Connect-PD -UseCredentialsManager $True; } $body = @{ title=$Title; } if($StageId) { $body.Add("stage_id", $StageId) } if($OrganizationId) { $body.Add("org_id", $OrganizationId) } if($PersonId) { $body.Add("person_id", $PersonId) } if($Value) { $body.Add("value", $Value) } if($ExpectedCloseDate) { $body.Add("expected_close_date", $ExpectedCloseDate.ToString('yyyy-MM-dd')) } if($Status) { $body.Add("status", $Status) } $bodyAsJson = ConvertTo-JSON($body); return $(Invoke-RestMethod -Uri "${baseUrl}/deals?api_token=${script:token}" -Method Post -Body $bodyAsJson -ContentType "application/json; charset=utf-8"); } <# .SYNOPSIS Edit a deal .DESCRIPTION .PARAMETER DealId Id of Deal to edit .PARAMETER Title Title to set in Deal .PARAMETER StageId Id of Stage to assign Deal to. .PARAMETER Value Value to assign to deal. .PARAMETER ExpectedCloseDate ExpectedCloseDate to assign to deal. .PARAMETER Status Status the deal gets set to. Can be set to open, won, lost, deleted. #> function Edit-PDDeal { [CmdletBinding()] Param( [Parameter(Mandatory=$true)][int]$DealId, [string]$Title, [int]$StageId, [float]$Value, [datetime]$ExpectedCloseDate, [string][ValidateSet('open','won','lost','deleted')]$Status ) #end param if(!$script:token) { Connect-PD -UseCredentialsManager $True; } $body = @{}; if($Title) { $body.Add("title", $Title) } if($StageId) { $body.Add("stage_id", $StageId) } if($Value) { $body.Add("value", $Value) } if($ExpectedCloseDate) { $body.Add("expected_close_date", $ExpectedCloseDate.ToString('yyyy-MM-dd')) } if($Status) { $body.Add("status", $Status) } $bodyAsJson = ConvertTo-JSON( $body ); return $(Invoke-RestMethod -Uri "${baseUrl}/deals/${DealId}?api_token=${script:token}" -Method Put -Body $bodyAsJson -ContentType "application/json; charset=utf-8"); } <# .SYNOPSIS Get Organizations from Pipedrive .DESCRIPTION .PARAMETER Term Search Term to look for #> function Get-PDOrganization { [CmdletBinding()] Param( [Parameter(Mandatory=$true)][string]$Term ) if(!$script:token) { Connect-PD -UseCredentialsManager $True; } return $(Invoke-RestMethod -Uri "${baseUrl}/organizations/find?term=${Term}&api_token=${script:token}" -Method Get -ContentType "application/json; charset=utf-8"); } <# .SYNOPSIS Adds an Organization .DESCRIPTION .PARAMETER Name Name of Organization #> function Add-PDOrganization { [CmdletBinding()] Param( [Parameter(Mandatory=$true)][string]$Name ) #end param if(!$script:token) { Connect-PD -UseCredentialsManager $True; } $bodyAsJson = ConvertTo-JSON( @{ name=$Name; }); return $(Invoke-RestMethod -Uri "${baseUrl}/organizations?api_token=${script:token}" -Method Post -Body $bodyAsJson -ContentType "application/json; charset=utf-8"); } <# .SYNOPSIS Gets a Person .DESCRIPTION .PARAMETER Term Search term to look for .PARAMETER OrganizationName Name of the Organization. Can be omitted to search all persons. #> function Get-PDPerson { [CmdletBinding()] Param( [Parameter(Mandatory=$true)][string]$Term, [string]$OrganizationName ) #end param if(!$script:token) { Connect-PD -UseCredentialsManager $True; } $parameters = @{ term=$Term; api_token=$script:token; } if($OrganizationName) { $organization = Get-PDOrganization -Term $OrganizationName if($organization.data.count -eq 0) { throw "No Organization having Name ${OrganizationName} found." } $parameters.Add("org_id", $organization.data.id) } $uri = New-HttpQueryString "${baseUrl}/persons/find" $parameters return $(Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json; charset=utf-8"); } <# .SYNOPSIS Adds a Person .DESCRIPTION .PARAMETER Name Name of Person .PARAMETER OrganizationId Id of the Organization. If omitted, the person is assigned to no organization. #> function Add-PDPerson { [CmdletBinding()] Param( [Parameter(Mandatory=$true)][string]$Name, [int]$OrganizationId ) #end param if(!$script:token) { Connect-PD -UseCredentialsManager $True; } $body = @{ name=${Name} }; if($OrganizationId) { $body.Add("org_id", $OrganizationId) } $bodyAsJson = ConvertTo-JSON($body); return $(Invoke-RestMethod -Uri "${baseUrl}/persons?api_token=${script:token}" -Method Post -Body $bodyAsJson -ContentType "application/json; charset=utf-8"); } ## Private Functions function New-HttpQueryString { [CmdletBinding()] param ( [Parameter(Mandatory = $true)][String]$Uri, [Parameter(Mandatory = $true)][Hashtable]$QueryParameter ) # Add System.Web Add-Type -AssemblyName System.Web # Create a http name value collection from an empty string $nvCollection = [System.Web.HttpUtility]::ParseQueryString([String]::Empty) foreach ($key in $QueryParameter.Keys) { $nvCollection.Add($key, $QueryParameter.$key) } # Build the uri $uriRequest = [System.UriBuilder]$uri $uriRequest.Query = $nvCollection.ToString() return $uriRequest.Uri.OriginalString } |