Pipedrive.psm1
$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 } |