Public/Set-IBObject.ps1
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 |
function Set-IBObject { [CmdletBinding(SupportsShouldProcess)] param( [Parameter(ParameterSetName='ObjectOnly',Mandatory=$True,ValueFromPipeline=$True)] [PSObject[]]$IBObject, [Parameter(ParameterSetName='RefAndTemplate',Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)] [Alias('_ref','ref')] [string[]]$ObjectRef, [Parameter(ParameterSetName='RefAndTemplate',Mandatory=$True)] [PSObject]$TemplateObject, [Parameter(ParameterSetName='ObjectOnly')] [Parameter(ParameterSetName='RefAndTemplate')] [Alias('fields')] [string[]]$ReturnFields, [Parameter(ParameterSetName='ObjectOnly')] [Parameter(ParameterSetName='RefAndTemplate')] [Alias('base')] [switch]$ReturnBaseFields, [Parameter(ParameterSetName='ObjectOnly')] [Parameter(ParameterSetName='RefAndTemplate')] [Alias('host')] [string]$WAPIHost, [Parameter(ParameterSetName='ObjectOnly')] [Parameter(ParameterSetName='RefAndTemplate')] [Alias('version')] [string]$WAPIVersion, [Parameter(ParameterSetName='ObjectOnly')] [Parameter(ParameterSetName='RefAndTemplate')] [PSCredential]$Credential, [Parameter(ParameterSetName='ObjectOnly')] [Parameter(ParameterSetName='RefAndTemplate')] [Alias('session')] [Microsoft.PowerShell.Commands.WebRequestSession]$WebSession, [Parameter(ParameterSetName='ObjectOnly')] [Parameter(ParameterSetName='RefAndTemplate')] [switch]$IgnoreCertificateValidation ) Begin { # grab the variables we'll be using for our REST calls $opts = Initialize-CallVars @PSBoundParameters $APIBase = $script:APIBaseTemplate -f $opts.WAPIHost,$opts.WAPIVersion $opts.Remove('WAPIHost') | Out-Null $opts.Remove('WAPIVersion') | Out-Null $querystring = [String]::Empty # process the return fields if ($ReturnFields.Count -gt 0) { if ($ReturnBaseFields) { $querystring = "?_return_fields%2B=$($ReturnFields -join ',')" } else { $querystring = "?_return_fields=$($ReturnFields -join ',')" } } elseif ($ReturnBaseFields) { $querystring = "?_return_fields%2B" } } Process { switch ($PsCmdlet.ParameterSetName) { "ObjectOnly" { if (!$IBObject._ref) { throw "IBObject is missing '_ref' field." } # copy out the ObjectRef from the object $ObjectRef = $IBObject._ref # create the json body $bodyJson = $IBObject | ConvertTo-Json -Compress -Depth 5 $bodyJson = [Text.Encoding]::UTF8.GetBytes($bodyJson) Write-Verbose "JSON body:`n$($IBObject | ConvertTo-Json -Depth 5)" } "RefAndTemplate" { # create the json body $bodyJson = $TemplateObject | ConvertTo-Json -Compress -Depth 5 $bodyJson = [Text.Encoding]::UTF8.GetBytes($bodyJson) Write-Verbose "JSON body:`n$($TemplateObject | ConvertTo-Json -Depth 5)" } } $uri = "$APIBase$($ObjectRef)$($querystring)" if ($PsCmdlet.ShouldProcess($uri, 'PUT')) { Invoke-IBWAPI -Method Put -Uri $uri -Body $bodyJson @opts } } <# .SYNOPSIS Modify an object in Infoblox. .DESCRIPTION Modify an object by specifying its object reference and a PSObject with the fields to change. .PARAMETER IBObject An object with the fields to be modified. This must include a '_ref' with the object reference string to modify. All included fields will be modified even if they are empty. .PARAMETER ObjectRef Object reference string. This is usually found in the "_ref" field of returned objects. .PARAMETER TemplateObject An object with the fields to be modified. A '_ref' field in this object will be ignored. This is only usable with a separate -ObjectRef parameter. .PARAMETER ReturnFields The set of fields that should be returned in addition to the object reference. .PARAMETER ReturnBaseFields If specified, the standard fields for this object type will be returned in addition to the object reference and any additional fields specified by -ReturnFields. .PARAMETER WAPIHost The fully qualified DNS name or IP address of the Infoblox WAPI endpoint (usually the grid master). This parameter is required if not already set using Set-IBWAPIConfig. .PARAMETER WAPIVersion The version of the Infoblox WAPI to make calls against (e.g. '2.2'). .PARAMETER Credential Username and password for the Infoblox appliance. This parameter is required unless -WebSession is specified or was already set using Set-IBWAPIConfig. .PARAMETER WebSession A WebRequestSession object returned by Get-IBSession or set when using Invoke-IBWAPI with the -SessionVariable parameter. This parameter is required unless -Credential is specified or was already set using Set-IBWAPIConfig. .PARAMETER IgnoreCertificateValidation If set, SSL/TLS certificate validation will be disabled. Overrides value stored with Set-IBWAPIConfig. .OUTPUTS The object reference string of the modified item or a custom object if -ReturnFields or -ReturnBaseFields was used. .EXAMPLE $myhost = Get-IBObject -ObjectType 'record:host' -Filters 'name=myhost' -ReturnFields 'comment' PS C:\>$myhost.comment = 'new comment' PS C:\>Set-IBObject -ObjectRef $myhost._ref -IBObject $myhost Search for a host record called 'myhost', update the comment field, and save it. .EXAMPLE $toChange = Get-IBObject -type 'record:host' -Filters 'name~=oldname' -fields 'name' PS C:\>$toChange | %{ $_.name = $_.name.Replace('oldname','newname'); $_ } | Set-IBObject Find all hosts with 'oldname' in the name, change the references to 'newname', and send them through the pipeline to Set-IBObject for saving. .EXAMPLE $myhosts = Get-IBObject 'record:host' -Filters 'comment=web server' PS C:\>$myhosts | Set-IBObject -TemplateObject @{comment='db server'} Find all host records with comment 'web server' and change them to 'db server' with a manually created template .LINK Project: https://github.com/rmbolger/Posh-IBWAPI .LINK Get-IBObject #> } |