Private/Get-AtwsDefinition.ps1
Function Get-AtwsDefinition { Begin { $EntityName = '#EntityName' $Prefix = '#Prefix' # Lookup Verbose, WhatIf and other preferences from calling context Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState Write-Verbose ('{0}: Begin of function' -F $MyInvocation.MyCommand.Name) } Process { If ($PSCmdlet.ParameterSetName -eq 'Get_all') { $Filter = @('id', '-ge', 0)} ElseIf (-not ($Filter)) { Write-Verbose ('{0}: Query based on parameters, parsing' -F $MyInvocation.MyCommand.Name) $Fields = Get-AtwsFieldInfo -Entity $EntityName -Connection $Prefix Foreach ($Parameter in $PSBoundParameters.GetEnumerator()) { $Field = $Fields | Where-Object {$_.Name -eq $Parameter.Key} If ($Field -or $Parameter.Key -eq 'UserDefinedField') { If ($Parameter.Value.Count -gt 1) { $Filter += '-begin' } Foreach ($ParameterValue in $Parameter.Value) { $Operator = '-or' $ParameterName = $Parameter.Key If ($Field.IsPickList) { If ($Field.PickListParentValueField) { $ParentField = $Fields.Where{$_.Name -eq $Field.PickListParentValueField} $ParentLabel = $PSBoundParameters.$($ParentField.Name) $ParentValue = $ParentField.PickListValues | Where-Object {$_.Label -eq $ParentLabel} $PickListValue = $Field.PickListValues | Where-Object {$_.Label -eq $ParameterValue -and $_.ParentValue -eq $ParentValue.Value} } Else { $PickListValue = $Field.PickListValues | Where-Object {$_.Label -eq $ParameterValue} } $Value = $PickListValue.Value } ElseIf ($ParameterName -eq 'UserDefinedField') { $Filter += '-udf' $ParameterName = $ParameterValue.Name $Value = $ParameterValue.Value } Else { $Value = $ParameterValue } $Filter += $ParameterName If ($Parameter.Key -in $NotEquals) { $Filter += '-ne' $Operator = '-and' } ElseIf ($Parameter.Key -in $GreaterThan) { $Filter += '-gt'} ElseIf ($Parameter.Key -in $GreaterThanOrEquals) { $Filter += '-ge'} ElseIf ($Parameter.Key -in $LessThan) { $Filter += '-lt'} ElseIf ($Parameter.Key -in $LessThanOrEquals) { $Filter += '-le'} ElseIf ($Parameter.Key -in $Like) { $Filter += '-like' $Value = $Value -replace '\*', '%' } ElseIf ($Parameter.Key -in $NotLike) { $Filter += '-notlike' $Value = $Value -replace '\*', '%' } ElseIf ($Parameter.Key -in $BeginsWith) { $Filter += '-beginswith'} ElseIf ($Parameter.Key -in $EndsWith) { $Filter += '-endswith'} ElseIf ($Parameter.Key -in $Contains) { $Filter += '-contains'} ElseIf ($Parameter.Key -in $IsThisDay) { $Filter += '-isthisday'} Else { $Filter += '-eq'} $Filter += $Value If ($Parameter.Value.Count -gt 1 -and $ParameterValue -ne $Parameter.Value[-1]) { $Filter += $Operator } ElseIf ($Parameter.Value.Count -gt 1) { $Filter += '-end' } } } } # IsNull and IsNotNull are special. They are the only operators that does not require a value to work If ($IsNull.Count -gt 0) { If ($Filter.Count -gt 0) { $Filter += '-and' } Foreach ($PropertyName in $IsNull) { $Filter += $PropertyName $Filter += '-isnull' } } If ($IsNotNull.Count -gt 0) { If ($Filter.Count -gt 0) { $Filter += '-and' } Foreach ($PropertyName in $IsNotNull) { $Filter += $PropertyName $Filter += '-isnotnull' } } } Else { Write-Verbose ('{0}: Passing -Filter raw to Get function' -F $MyInvocation.MyCommand.Name) } $Result = Get-AtwsData -Entity $EntityName -Filter $Filter -Connection $Prefix Write-Verbose ('{0}: Number of entities returned by base query: {1}' -F $MyInvocation.MyCommand.Name, $Result.Count) if ( ($Result) -and ($GetReferenceEntityById)) { Write-Verbose ('{0}: User has asked for external reference objects by {1}' -F $MyInvocation.MyCommand.Name, $GetReferenceEntityById) $Field = $Fields.Where({$_.Name -eq $GetReferenceEntityById}) $ResultValues = $Result | Where-Object {$null -ne $_.$GetReferenceEntityById} If ($ResultValues.Count -lt $Result.Count) { Write-Warning ('{0}: Only {1} of the {2}s in the primary query had a value in the property {3}.' -F $MyInvocation.MyCommand.Name, $ResultValues.Count, $EntityName, $GetReferenceEntityById) -WarningAction Continue } $Filter = 'id -eq {0}' -F $($ResultValues.$GetReferenceEntityById -join ' -or id -eq ') $ReferenceResult = Get-Atwsdata -Entity $Field.ReferenceEntityType -Filter $Filter -Connection $Prefix If ($ReferenceResult) { $Result = $ReferenceResult } } ElseIf ( ($Result) -and ($AddPickListLabel)) { Foreach ($Field in $Fields.Where{$_.IsPickList}) { $FieldName = '{0}Label' -F $Field.Name Foreach ($Item in $Result) { $Value = ($Field.PickListValues.Where{$_.Value -eq $Item.$($Field.Name)}).Label $Exists = Get-Member -InputObject $Item -MemberType NoteProperty -Name $FieldName If (-not ($Exists)) { Add-Member -InputObject $Item -MemberType NoteProperty -Name $FieldName -Value $Value } Else { $Item.$FieldName = $Value } } } } } End { Write-Verbose ('{0}: End of function' -F $MyInvocation.MyCommand.Name) If ($Result) { Return $Result } } } |