Private/Set-AtwsDefinition.ps1
Function Set-AtwsDefinition { begin { $entityName = '#EntityName' # Enable modern -Debug behavior if ($PSCmdlet.MyInvocation.BoundParameters['Debug'].IsPresent) { $DebugPreference = 'Continue' } else { # Respect configured preference $DebugPreference = $Script:Atws.Configuration.DebugPref } Write-Debug ('{0}: Begin of function' -F $MyInvocation.MyCommand.Name) if (!($PSCmdlet.MyInvocation.BoundParameters['Verbose'].IsPresent)) { # No local override of central preference. Load central preference $VerbosePreference = $Script:Atws.Configuration.VerbosePref } $ModifiedObjects = [Collections.ArrayList]::new() } process { # Collect fresh copies of InputObject if passed any IDs # Has to collect in batches, or we are going to get the # dreaded 'too nested SQL' error If ($Id.count -gt 0) { for ($i = 0; $i -lt $Id.count; $i += 200) { $j = $i + 199 if ($j -ge $Id.count) { $j = $Id.count - 1 } # Create a filter with the current batch $Filter = 'Id -eq {0}' -F ($Id[$i .. $j] -join ' -or Id -eq ') $InputObject += Get-AtwsData -Entity $entityName -Filter $Filter } # Remove the ID parameter so we do not try to set it on every object $null = $PSBoundParameters.Remove('id') } $caption = $MyInvocation.MyCommand.Name $verboseDescription = '{0}: About to modify {1} {2}(s). This action cannot be undone.' -F $caption, $InputObject.Count, $entityName $verboseWarning = '{0}: About to modify {1} {2}(s). This action cannot be undone. Do you want to continue?' -F $caption, $InputObject.Count, $entityName if ($PSCmdlet.ShouldProcess($verboseDescription, $verboseWarning, $caption)) { Write-Verbose $verboseDescription # Process parameters and update objects with their values $processObject = $InputObject | Update-AtwsObjectsWithParameters -BoundParameters $PSBoundParameters -EntityName $EntityName try { # If using pipeline this block (process) will run once pr item in the pipeline. make sure to return them all $Data = Set-AtwsData -Entity $processObject if ($Data.Count -gt 1) { [void]$ModifiedObjects.AddRange($Data) }else { [void]$ModifiedObjects.Add($Data) } } catch { write-host "ERROR: " -ForegroundColor Red -NoNewline write-host $_.Exception.Message write-host ("{0}: {1}" -f $_.CategoryInfo.Category,$_.CategoryInfo.Reason) -ForegroundColor Cyan $_.ScriptStackTrace -split '\n' | ForEach-Object { Write-host " | " -ForegroundColor Cyan -NoNewline Write-host $_ } } } } end { Write-Debug ('{0}: End of function, returning {1} {2}(s)' -F $MyInvocation.MyCommand.Name, $ModifiedObjects.count, $entityName) if ($PassThru.IsPresent) { Return [array]$ModifiedObjects } } } |