Dynamic/Set-AtwsContract.ps1
#Requires -Version 4.0 <# .COPYRIGHT Copyright (c) Office Center Hønefoss AS. All rights reserved. Based on code from Jan Egil Ring (Crayon). Licensed under the MIT license. See https://github.com/officecenter/Autotask/blob/master/LICENSE.md for license information. #> Function Set-AtwsContract { <# .SYNOPSIS This function sets parameters on the Contract specified by the -InputObject parameter or pipeline through the use of the Autotask Web Services API. Any property of the Contract that is not marked as READ ONLY by Autotask can be speficied with a parameter. You can specify multiple paramters. .DESCRIPTION This function one or more objects of type [Autotask.Contract] as input. You can pipe the objects to the function or pass them using the -InputObject parameter. You specify the property you want to set and the value you want to set it to using parameters. The function modifies all objects and updates the online data through the Autotask Web Services API. The function supports all properties of an [Autotask.Contract] that can be updated through the Web Services API. The function uses PowerShell parameter validation and supports IntelliSense for selecting picklist values. Entities that have fields that refer to the base entity of this CmdLet: AccountToDo BillingItem Contract ContractBlock ContractCost ContractExclusionAllocationCode ContractExclusionRole ContractFactor ContractMilestone ContractNote ContractRate ContractRetainer ContractRoleCost ContractService ContractServiceAdjustment ContractServiceBundle ContractServiceBundleAdjustment ContractServiceBundleUnit ContractServiceUnit ContractTicketPurchase InstalledProduct Project PurchaseOrderItem Ticket TimeEntry .INPUTS [Autotask.Contract[]]. This function takes one or more objects as input. Pipeline is supported. .OUTPUTS Nothing or [Autotask.Contract]. This function optionally returns the updated objects if you use the -PassThru parameter. .EXAMPLE Set-AtwsContract -InputObject $Contract [-ParameterName] [Parameter value] Passes one or more [Autotask.Contract] object(s) as a variable to the function and sets the property by name 'ParameterName' on ALL the objects before they are passed to the Autotask Web Service API and updated. .EXAMPLE $Contract | Set-AtwsContract -ParameterName <Parameter value> Same as the first example, but now the objects are passed to the funtion through the pipeline, not passed as a parameter. The end result is identical. .EXAMPLE Get-AtwsContract -Id 0 | Set-AtwsContract -ParameterName <Parameter value> Gets the instance with Id 0 directly from the Web Services API, modifies a parameter and updates Autotask. This approach works with all valid parameters for the Get function. .EXAMPLE Get-AtwsContract -Id 0,4,8 | Set-AtwsContract -ParameterName <Parameter value> Gets multiple instances by Id, modifies them all and updates Autotask. .EXAMPLE $Result = Get-AtwsContract -Id 0,4,8 | Set-AtwsContract -ParameterName <Parameter value> -PassThru Gets multiple instances by Id, modifies them all, updates Autotask and returns the updated objects. .LINK New-AtwsContract .LINK Get-AtwsContract #> [CmdLetBinding(DefaultParameterSetName='InputObject', ConfirmImpact='Low')] Param ( # An object that will be modified by any parameters and updated in Autotask [Parameter( ParameterSetName = 'Input_Object', ValueFromPipeline = $true )] [ValidateNotNullOrEmpty()] [Autotask.Contract[]] $InputObject, # The object.ids of objects that should be modified by any parameters and updated in Autotask [Parameter( ParameterSetName = 'By_Id' )] [ValidateNotNullOrEmpty()] [Int[]] $Id, # Return any updated objects through the pipeline [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( ParameterSetName = 'By_parameters' )] [Switch] $PassThru, # User defined fields already setup i Autotask [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( ParameterSetName = 'By_parameters' )] [Alias('UDF')] [Autotask.UserDefinedField[]] $UserDefinedFields, # Billing Preference [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [Int] $BillingPreference, # Contract Compilance [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [boolean] $Compliance, # Contract Contact [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [ValidateLength(1,250)] [string] $ContactName, # Category [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [Int] $ContractCategory, # Contract Name [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( Mandatory = $true, ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [Alias('Name')] [ValidateNotNullOrEmpty()] [ValidateLength(1,100)] [string] $ContractName, # Contract Number [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [ValidateLength(1,50)] [string] $ContractNumber, # Default Contract [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [boolean] $IsDefaultContract, # Description [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [ValidateLength(1,2000)] [string] $Description, # End Date [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( Mandatory = $true, ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [ValidateNotNullOrEmpty()] [datetime] $EndDate, # Estimated Cost [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [double] $EstimatedCost, # Estimated Hours [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [double] $EstimatedHours, # Estimated Revenue [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [double] $EstimatedRevenue, # Contract Overage Billing Rate [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [double] $OverageBillingRate, # Start Date [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( Mandatory = $true, ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [ValidateNotNullOrEmpty()] [datetime] $StartDate, # Status [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( Mandatory = $true, ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [ValidateNotNullOrEmpty()] [Int] $Status, # Service Level Agreement ID [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [Int] $ServiceLevelAgreementID, # Contract Setup Fee [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [double] $SetupFee, # purchase_order_number [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [ValidateLength(1,50)] [string] $PurchaseOrderNumber, # Time Reporting Requires Start and Stop Times [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( Mandatory = $true, ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [ValidateNotNullOrEmpty()] [Int] $TimeReportingRequiresStartAndStopTimes, # opportunity_id [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [Int] $OpportunityID, # Renewed Contract Id [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [long] $RenewedContractID, # Contract Setup Fee Allocation Code ID [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [long] $SetupFeeAllocationCodeID, # Contact ID [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [Int] $ContactID, # Business Division Subdivision ID [Parameter( ParameterSetName = 'Input_Object' )] [Parameter( ParameterSetName = 'By_parameters' )] [Parameter( ParameterSetName = 'By_Id' )] [Int] $BusinessDivisionSubdivisionID ) Begin { $EntityName = 'Contract' # Enable modern -Debug behavior If ($PSCmdlet.MyInvocation.BoundParameters['Debug'].IsPresent) {$DebugPreference = 'Continue'} Write-Debug ('{0}: Begin of function' -F $MyInvocation.MyCommand.Name) # Set up TimeZone offset handling If (-not($script:ESToffset)) { $Now = Get-Date $ESTzone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time") $ESTtime = [System.TimeZoneInfo]::ConvertTimeFromUtc($Now.ToUniversalTime(), $ESTzone) $script:ESToffset = (New-TimeSpan -Start $ESTtime -End $Now).TotalHours } # Collect fresh copies of InputObject if passed any IDs If ($Id.Count -gt 0 -and $Id.Count -le 200) { $Filter = 'Id -eq {0}' -F ($Id -join ' -or Id -eq ') $InputObject = Get-AtwsData -Entity $EntityName -Filter $Filter } ElseIf ($Id.Count -gt 200) { Throw [ApplicationException] 'Too many objects, the module can process a maximum of 200 objects when using the Id parameter.' } } Process { $Fields = Get-AtwsFieldInfo -Entity $EntityName Foreach ($Parameter in $PSBoundParameters.GetEnumerator()) { $Field = $Fields | Where-Object {$_.Name -eq $Parameter.Key} If ($Field -or $Parameter.Key -eq 'UserDefinedFields') { If ($Field.IsPickList) { $PickListValue = $Field.PickListValues | Where-Object {$_.Label -eq $Parameter.Value} $Value = $PickListValue.Value } ElseIf ($Field.Type -eq 'datetime') { $TimePresent = $Parameter.Value.Hour -gt 0 -or $Parameter.Value.Minute -gt 0 -or $Parameter.Value.Second -gt 0 -or $Parameter.Value.Millisecond -gt 0 If ($Field.Name -like "*DateTime" -or $TimePresent) { # Yes, you really have to ADD the difference $Value = $Parameter.Value.AddHours($script:ESToffset) } } Else { $Value = $Parameter.Value } Foreach ($Object in $InputObject) { $Object.$($Parameter.Key) = $Value } } } $ModifiedObjects = Set-AtwsData -Entity $InputObject # The API documentation explicitly states that you can only use the objects returned # by the .create() function to get the new objects ID. # so to return objects with accurately represents what has been created we have to # get them again by id $NewObjectFilter = 'id -eq {0}' -F ($ModifiedObjects.Id -join ' -or id -eq ') $ModifiedObjects = Get-AtwsData -Entity $EntityName -Filter $NewObjectFilter } End { Write-Debug ('{0}: End of function' -F $MyInvocation.MyCommand.Name) If ($PassThru.IsPresent) { # Datetimeparameters $DateTimeParams = $Fields.Where({$_.Type -eq 'datetime'}).Name # Expand UDFs by default Foreach ($Item in $ModifiedObjects) { # Any userdefined fields? If ($Item.UserDefinedFields.Count -gt 0) { # Expand User defined fields for easy filtering of collections and readability Foreach ($UDF in $Item.UserDefinedFields) { # Make names you HAVE TO escape... $UDFName = '#{0}' -F $UDF.Name Add-Member -InputObject $Item -MemberType NoteProperty -Name $UDFName -Value $UDF.Value } } # Adjust TimeZone on all DateTime properties Foreach ($DateTimeParam in $DateTimeParams) { # Get the datetime value $ParameterValue = $Item.$DateTimeParam # Skip if parameter is empty If (-not ($ParameterValue)) { Continue } $TimePresent = $ParameterValue.Hour -gt 0 -or $ParameterValue.Minute -gt 0 -or $ParameterValue.Second -gt 0 -or $ParameterValue.Millisecond -gt 0 # If this is a DATE it should not be touched If ($DateTimeParam -like "*DateTime" -or $TimePresent) { # This is DATETIME # We need to adjust the timezone difference # Yes, you really have to ADD the difference $ParameterValue = $ParameterValue.AddHours($script:ESToffset) # Store the value back to the object (not the API!) $Item.$DateTimeParam = $ParameterValue } } } Return $ModifiedObjects } } } |