Reply

New Powershell module, Posh-IBWAPI

[ Edited ]
Authority
Posts: 31
13808     0

Hey all-

 

I realize there are a variety of Powershell related WAPI projects out there. But I wasn't completely happy with any of them. So I made my own called Posh-IBWAPI.

 

https://github.com/rmbolger/Posh-IBWAPI
https://www.powershellgallery.com/packages/Posh-IBWAPI

 

Notable features include:

  • Automatic paging for large GET result sets (Requires WAPI version 1.5+)
  • Error details in the body of HTTP 400 responses are exposed instead of being swallowed by Invoke-RestMethod.
  • Pipeline support so you can do things like pass the results from Get-IBObject directly to Remove-IBObject.
  • Optionally ignore certificate validation errors.
  • Save common connection parameters per-session with Set-IBWAPIConfig so you don't need to pass them to every function call.

My problem with most of the existing projects is that they're trying to create individual wrappers for all of the supported WAPI object types. Those projects never seem to cover 100% of the supported objects and usually have difficulty dealing with differences between WAPI versions or even try to enforce a particular version. Posh-IBWAPI tries to be way more generic and just provide a thin layer on top of Invoke-RestMethod so you don't have to futz with the tedium of "doing REST" and just focus on making the WAPI do what you want.

 

So instead of commands like Get-Record and Get-Network, you just have:

Get-IBObject -type record:host
Get-IBObject -type network

This makes the module way more compatible across WAPI versions and usually means no code change is necessary when new objects or features are introduced.

 

The primary commands that most tasks will make use of are Get-IBObjectNew-IBObjectSet-IBObjectRemove-IBObject, and Invoke-IBFunction. There's a quick start in the readme and I also ported the "The definitive list of REST examples" post to use the module and put it in the Github repo wiki.

https://github.com/rmbolger/Posh-IBWAPI/wiki/The-definitive-list-of-REST-examples

 

There are two main things I'm planning on working on next. One is a command dedicated to querying the schema so you can hypothetically spend less time in the full HTML docs when figuring out how to query/manipulate an object. The other is a set of functions dedicated to uploading and downloading files via the fileop object. It's doable in the current version via Invoke-IBFunction, but it's tedious and I feel like it would be nice to abstract away that tediousness a bit more.

 

Comments, criticism, issues? Feel free to reply here or create a Github issue.

 

Thanks for your time!

 

*Edit: Fixed links. Thanks, FHecker!

Re: New Powershell module, Posh-IBWAPI

Adviser
Posts: 132
13808     0

Thanks for creating this. I'm going to try it out for a PowerShell project I'm working on. However, note that you included the wrong links. Presumably you meant to link to the following:

 

https://github.com/rmbolger/Posh-IBWAPI

https://www.powershellgallery.com/packages/Posh-IBWAPI/1.0

Re: New Powershell module, Posh-IBWAPI

Authority
Posts: 31
13808     0

Well that's rather embarrassing. Those are indeed the right links. Hopefully I can edit the original post.

Re: New Powershell module, Posh-IBWAPI

[ Edited ]
Authority
Posts: 31
13808     0

I just released version 1.2 of the module which now has the first version of schema querying via Get-IBSchema. The schema querying also allowed me to add a -ReturnAllFields parameter to Get-IBObject which makes it way easier browse the complete object model for a particular object.

 

https://github.com/rmbolger/Posh-IBWAPI

https://www.powershellgallery.com/packages/Posh-IBWAPI/1.2

 

*Edit: I forgot to mention that I wrote a little guide on using Get-IBSchema to demonstrate what it's capable of. You can check it out here:

https://github.com/rmbolger/Posh-IBWAPI/wiki/Guide-to-Get-IBSchema

 

Re: New Powershell module, Posh-IBWAPI

Authority
Posts: 31
13808     0

Version 1.2.2 is now available and officially supports the recently GA'd PowerShell Core 6.0. So you can use the module across all supported PowerShell Core platforms including Windows, MacOS, and Linux.

 

https://github.com/rmbolger/Posh-IBWAPI

https://www.powershellgallery.com/packages/Posh-IBWAPI

Re: New Powershell module, Posh-IBWAPI

Authority
Posts: 31
13808     0

Version 1.3 is now available and supports persistent configs with Save-IBWAPIConfig and Remove-IBWAPIConfig. This allows you to save your config sets from Set-IBWAPIConfig to disk in your profile such that they automatically get loaded in your next PowerShell session.

 

https://github.com/rmbolger/Posh-IBWAPI

https://www.powershellgallery.com/packages/Posh-IBWAPI

Highlighted

Re: New Powershell module, Posh-IBWAPI

ranmasao12
Techie
Posts: 4
13808     0

Awesome work.  I have a question.  I am trying to get a set-ibobject to work on adding a value to the list of values on an extensible attribute.   I figured out that I cannot simply add a new value that i have to replace the list but I keep getting an error so I am curious about the formatting of the template.   

 

This is what I have:

 

set-ibobject -ObjectRef extensibleattributedef/b25lLmV4dGVuc2libGVfYXR0cmlidXRlc19kZWYkLlNpdGVjb2RlSmiley Frustrateditecode -Template @{list_values=@{@{value=emc}, @{value=Undefined}}}

 

For some reason it really doesnt like the formatting.  can you help me figure out what I am missing here?

 

I am using the example on the git hub as a guide.

 

Set-IBObject -Template @{extattrs=@{Site=@{value='New York'}}}

 

Re: New Powershell module, Posh-IBWAPI

Authority
Posts: 31
13809     0

You were actually really close. The example on GitHub was a bit misleading because it's adding/setting an extesible attribute value to another object rather than modifying the extensible attribute definition itself.

 

For "List" type extensible attributes, the list_values format it's looking for is an array of @{value='string'} pairs.

 

So instead of 

@{list_values=@{ @{value='emc'}, @{value='Undefined'} } }

You instead want this where the second "@(" is a regular parenthesis rather than a curly brace.

@{list_values=@( @{value='emc'}, @{value='Undefined'} ) }

If you're just adding a value to the existing list, you can also make it easier on yourself by just modifying the original object definition that gets returned. For example:

 

# get the current attribute definition and its list_values field
$listdef = Get-IBObject -type extensibleattributedef -Filters 'name=MyList' -fields list_values

# add a new value
$listdef.list_values += @{value='NewItem'}

# set the updated definition
$listdef | Set-IBObject

Re: New Powershell module, Posh-IBWAPI

ranmasao12
Techie
Posts: 4
13809     0

Thank you for the quick response!    I love your work here.   It is going to allow me to simply a lot of scripts.

 

Re: New Powershell module, Posh-IBWAPI

Authority
Posts: 31
13809     0

No problem. Simplifying access to the WAPI is pretty much the whole goal of the module. So yay! I added this example to the REST examples wiki page as well.

Re: New Powershell module, Posh-IBWAPI

scampos
Techie
Posts: 2
13809     0

Hi - 

 

Im trying to get all record types (host,ptr,a ) within a certain IP subnet (10.25.5.x). Im tyring to use Get-IBobject, but not working out. Need help please.. 

Re: New Powershell module, Posh-IBWAPI

Authority
Posts: 31
13809     0

Is the subnet defined as a network or network container object in IPAM? What have you tried already? Or are you just not sure where to start querying the API for that info?

Re: New Powershell module, Posh-IBWAPI

Authority
Posts: 31
13809     0

So assuming your subnet is an actual "network" object defined in IPAM, you can start with something like this:

 

$usedAddresses = Get-IBObject -type ipv4address -filter 'network=10.0.0.0/24' | ?{ $_.objects }

The $usedAddresses variable will contain a list of IPs in that network that have at least one record type associated with it. The record object references are stored in the "objects" property of each entry.

 

You could do something like this to then query each individual record.

 

$usedObjects = $usedAddresses | select -expand objects | get-ibobject

But the results get messy because your resulting output contains multiple different types of objects with different base properties. A records have ipv4addr and name, PTR have ptrdname, Host have ipv4addrs and name. So depending on what you're trying to do with the data, there may be a better option.

Re: New Powershell module, Posh-IBWAPI

[ Edited ]
scampos
Techie
Posts: 2
13809     0

Hi, thanks for getting back to me..

That worked GREAT! Thank you!!! 

Re: New Powershell module, Posh-IBWAPI

fboyd
Techie
Posts: 2
13809     0

Good morning,

 

I just found this PowerShell module and hope to use it for updating existing server records in our InfoBlox. We are preparing to rename our 140+ VMware ESXi hosts as part of an upgrade to the latest VMware version and change them from DHCP addressing to static.  The InfoBlox managed DHCP scope will remain for some period of time as we work through the upgrade, so I need to make sure the addresses assigned to the hosts don't get issued from the scope once we move to static. I've created a script that updates all our host firmware versions and installs the new version of ESXi with the new host name and the static IP. Since the script pulls the list of servers to upgrade from a CSV, I'd like to also perform the InfoBlox updates from within the script. I'm new to InfoBlox and our network team does everything through the web front-end and can't provide any real assistance with scripting.

 

Since we only have a production InfoBlox, I don't want to try to learn through experimentation and screw something up. Could you provide a noob some guidance on coding these changes?

 

Thank you in advance,

Frank

Re: New Powershell module, Posh-IBWAPI

Authority
Posts: 31
13809     0

If you weren't scripting, what would your network team do in the GUI for this? If I have a better understanding of exactly what you're trying to do, I can likely suggest some scripted methods to do that.

Re: New Powershell module, Posh-IBWAPI

fboyd
Techie
Posts: 2
13809     0

Thank you for such a quick reply, I really appreciate it!

I did a skype session with one of the guys yesterday afternoon and we found inconsistencies in how records (objects) appear to have been created. In some cases there was a 'Host' object for a server along with 'A' and 'PTR' records. In other cases there was no 'Host' object, multiple 'A' records, and a 'PTR' record. A couple even included 'TXT' records (odd). What he suggested I do is create or update a 'Host' object with the new name for the server and delete any of the other record types that I find.

So I would go to IPAM view, search for the network, browse the network for the server's IP, open it, and then edit the entries. Once I made the edits for the list of servers being processed by the installation script.

I hope that answers your question.

Re: New Powershell module, Posh-IBWAPI

Authority
Posts: 31
13809     0

So searching by IP is really similar to the code I posted earlier for someone else. But instead of specifying a network range, you can just specify the actual IP in the filter parameter. This is the equivalent of finding the IP in IPAM and clicking it to see the related objects like Host, A, PTR, etc.

 

$ipObjects = @(Get-IBObject ipv4address -filter 'ip_address=192.168.0.3' | select -expand objects | Get-IBObject)

 

At this point, the $ipObjects variable has an array of object details in it for each type of record associated with that IP. Deleting or modifying them is easy once you've decided what to do. But based on your description, the tricky part may end up being the code that decides what to do based on what you got back.

 

The easiest way to check the type of each one is parsing the object type from the "_ref" property. For example, to get the type of the first item in the array, you might do something like this:

 

# grab the _ref text up to the first '/' character
$obj1Type = $ipObjects[0].'_ref'.Substring(0,$ipObjects[0].'_ref'.IndexOf('/')

 

Alternatively, you'll probably end up making decisions based on whether or not a host record already exists. So you might do something like this:

 

if ($ipObjects | ?{ $_.'_ref' -like 'record:host/*' }) {
    # a host exists, do something appropriate
}

 

So let's say you've done all your decision making logic and it's time to make the changes.

 

Here's how you'd delete the first record in the results.

 

$ipObjects[0] | Remove-IBObject

 

Here's how you might modify an existing host record that wasn't enabled for DNS:

 

# remove the ipv4addrs property from our local copy
$ipObjects[0].PSObject.Properties.Remove('ipv4addrs')

# update the name to contain a name + zone
$ipObjects[0].name = "$($ipObjects[0].name).myzone.com"

# set the appropriate DNS view<
$ipObjects[0].view = 'Internal'

# send the changes back to Infoblox
$ipObjects[0] | Set-IBObject

 

Here's how you might create a new host record using data from an existing A record

 

$newHost = @{
    name = $ipObjects[0].name
    view = 'Internal'
    ipv4addrs = @( @{ ipv4addr = $ipObjects[0].ipv4addr } )
}

New-IBObject record:host -IBObject $newHost

 

 

Re: New Powershell module, Posh-IBWAPI

Authority
Posts: 31
13809     0

Also, I just released version 2.0.0 of the module this morning. There are some breaking changes if you have scripts written against version 1.x. So be sure to check the changelog for details. Some of the new features include:

 

  • New functions for uploading/downloading files
  • Automatic session handling
  • Named connection profiles so you can have multiple sets of credentials saved for the same host

https://github.com/rmbolger/Posh-IBWAPI

https://www.powershellgallery.com/packages/Posh-IBWAPI

Re: New Powershell module, Posh-IBWAPI

pumaro
Techie
Posts: 1
13809     0

Is there a way to find the creation time/date for records?

Re: New Powershell module, Posh-IBWAPI

Authority
Posts: 31
13809     0

It depends on the record type.

 

For most of the standard DNS record types (A, AAAA, PTR, CNAME, etc.) yes. The WAPI exposes a "creation_time" field returned as epoch seconds (a.k.a. Unix time).

 

So you could do something like this:

 

Get-IBObject record:a -fields creation_time -base | Select name,ipv4addr,@{L='Creation Time';E={[DateTimeOffset]::FromUnixTimeSeconds($_.creation_time)}}

But it looks like none of the synthetic record types (Host, Alias, DTC, RPZ, etc) expose that same creation_time field. And it doesn't look they have any equivalent fields with a different name.

Showing results for 
Search instead for 
Do you mean 

Recommended for You