I am trying to set an IP pool to NSX-T with the following function code
Function Set-NSXTIPPool {
<#
.Synopsis
Creates an IP Pool
.DESCRIPTION
Creates a IP Pool with a number of required parameters. Supported IP formats include 192.168.1.1, 192.168.1.1-192.168.1.100, 192.168.0.0/24
.EXAMPLE
Set-NSXTIPPool -display_name "Pool Name" -allocation_start "192.168.1.2" -allocation_end "192.168.1.100" -cidr "192.168.1.0/24"
.EXAMPLE
Set-NSXTIPPool -display_name "Test Pool Name" -allocation_start "192.168.1.2" -allocation_end "192.168.1.100" -cidr "192.168.1.0/24" -dns_nameservers "192.168.1.1" -gateway_ip "192.168.1.1" -dns_suffix "evil corp"
#>
[CmdletBinding(SupportsShouldProcess=$true,
ConfirmImpact='High')]
# Paramameter Set variants will be needed Multicast & Broadcast Traffic Types as well as VM & Logical Port Types
Param (
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$display_name,
[parameter(Mandatory=$false)]
[string]$description,
[parameter(Mandatory=$false)]
[string]$dns_nameservers,
[parameter(Mandatory=$false)]
[string]$dns_suffix,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$allocation_start,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$allocation_end,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$cidr,
[parameter(Mandatory=$false)]
[string]$gateway_ip
)
Begin
{
if (-not $global:DefaultNsxtServers.isconnected)
{
try
{
Connect-NsxtServer -Menu -ErrorAction Stop
}
catch
{
throw "Could not connect to an NSX-T Manager, please try again"
}
}
$NSXTIPPoolService = Get-NsxtService -Name "com.vmware.nsx.pools.ip_pools"
# Classes unused - part of early testing
class allocation_ranges {
[string]$start
[string]$end
#$self
}
class subnets {
[array]$allocation_ranges = [allocation_ranges]::new()
[array]$dns_nameservers
[string]$dns_suffix
[string]$cidr
[string]$gateway_ip
#hidden $self
}
class ip_pool {
[string]$display_name
[string]$description
[string]$resource_type = 'IpPool'
[long]$revision = '0'
[array]$subnets = [subnets]::new()
hidden $pool_usage
hidden [array]$tags
# hidden $self
hidden $links
}
}
Process
{
$sample_ip_pool = $NSXTIPPoolService.help.create.ip_pool.Create()
$sample_ip_pool.subnets = @($NSXTIPPoolService.help.create.ip_pool.subnets.Create())
$sample_ip_pool.subnets = @($NSXTIPPoolService.help.create.ip_pool.subnets.Element.Create())
$sample_ip_pool.subnets[0].allocation_ranges = @($NSXTIPPoolService.help.create.ip_pool.subnets.Element.allocation_ranges.create())
$sample_ip_pool.subnets[0].allocation_ranges = @($NSXTIPPoolService.help.create.ip_pool.subnets.Element.allocation_ranges.element.create())
#Remove buggy self object
$ip_pool = $sample_ip_pool | select -Property * -ExcludeProperty self
$ip_pool.subnets[0] = $sample_ip_pool.subnets[0] | select -Property * -ExcludeProperty self
$ip_pool.subnets[0].allocation_ranges[0] = $sample_ip_pool.subnets[0].allocation_ranges[0] | select -Property * -ExcludeProperty self
# Assign objects
$ip_pool.display_name = $display_name
$ip_pool.description = $description
$ip_pool.resource_type = "IpPool"
$ip_pool.subnets[0].dns_nameservers = @($dns_nameservers)
$ip_pool.subnets[0].dns_suffix = $dns_suffix
$ip_pool.subnets[0].allocation_ranges[0].start = $allocation_start
$ip_pool.subnets[0].allocation_ranges[0].end = $allocation_end
$ip_pool.subnets[0].cidr = $cidr
$ip_pool.subnets[0].gateway_ip = $gateway_ip
$ip_pool.revision = 0
$ip_pool.tags = @()
try
{
# Should process
if ($pscmdlet.ShouldProcess($ip_pool.display_name, "Create IP Pool"))
{
$NSXTIPPoolService.create($ip_pool)
}
}
catch
{
$Error[0].Exception.ServerError.data
# more error data found in the NSX-T Manager /var/log/vmware/nsx-manager.log file; grep POOL-MGMT
throw
}
}
}
I try to execute the function with the following syntax
Set-NSXTIPPool -display_name "Test Pool Name" -allocation_start "192.168.31.2" -allocation_end "192.168.31.100" -cidr "192.168.31.0/24" -dns_nameservers "192.168.30.2" -gateway_ip "192.168.31.1" -dns_suffix "tataoui.com" -Confirm:$false
here is the error I am getting.
Object reference not set to an instance of an object.
At line:124 char:17
$NSXTIPPoolService.create($ip_pool)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To debug this, I add the following line and pipe the what support to be the dataset it is execution with out as json
$ip_pool | ConvertTo-Json | Set-Content -Path "$($ENV:Temp)\IPPooljsontemplate.json"
From what I can tell, one of the parameter is not being set, which I am assuming this is the root of the error
Why don't you use/look at the Set-NSXTIPPool function in the NSXT module?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I am using the code from the site you directing me to 😉 I just copy only the set NSXTIP function for testing.
I know, and I also know you already opened an Issue there.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I load in the entire NSXT modules to my code and ran the following and still getting the same error. Just want to make sure I am not doing a simple errors, syntax/usage
Set-NSXTIPPool -display_name "Test Pool Name" -allocation_start "192.168.31.20" -allocation_end "192.168.31.30" -cidr "192.168.31.0/24" -dns_nameservers "192.168.30.2" -gateway_ip "192.168.31.1" -dns_suffix "tataoui.com" -description "Test Pool description" -Confirm:$false
Object reference not set to an instance of an object.
At C:\Users\cdominic\Documents\WindowsPowerShell\Modules\NSXT\NSXT.psm1:1641 char:17
+ $NSXTIPPoolService.create($ip_pool)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], CisException
+ FullyQualifiedErrorId : VMware.VimAutomation.Cis.Core.Types.V1.CisException
PS C:\Users\cdominic> Set-NSXTIPPool
If you look at the API Reference for IP Pools, you'll notice that allocation_ranges needs to be an array.
That is obviously not the case, as you can see in your JSON dump.
Btw, dns_namservers should also hold an array.
This might be an issue with the help.create method, not sure and I can't test right now.
You could create the $ip_pool structure without using the help.create method.
It is in the end nothing more than a hash table with a number of properties, some of which are arrays.
And the layout is documented in that API Reference page
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Luc, as always thank you for the insight. Turns out there are 2 issues. First, I am aware the Pool range array is not being created based on my JSON output. I end up using a more manual method and it seems to work
$ipPoolService = Get-NsxtService -Name "com.vmware.nsx.pools.ip_pools"
$ipPoolSpec = $ipPoolService.help.create.ip_pool.Create()
$tagSpec = $ipPoolService.help.create.ip_pool.tags.Element.Create()
$subNetSpec = $ipPoolService.help.create.ip_pool.subnets.Element.Create()
$allocationRangeSpec = $ipPoolService.help.create.ip_pool.subnets.Element.allocation_ranges.Element.Create()
$ipPoolSpec.display_name = $NSXT_IP_Pool_Name
$ipPoolSpec.description = $NSXT_IP_Pool_Description
$allocationRangeSpec.start = $NSXT_IP_Pool_Start
$allocationRangeSpec.end = $NSXT_IP_Pool_End
$addResult = $subNetSpec.allocation_ranges.Add($allocationRangeSpec)
$subNetSpec.cidr = $NSXT_IP_Pool_CIDR
$subNetSpec.gateway_ip = $NSXTP_IP_Pool_Gateway # Non routable
$subNetSpec.dns_nameservers = @($NSXT_IP_Pool_DNS1, $NSXT_IP_Pool_DNS2) # new
$subNetSpec.dns_suffix = $NSXT_IP_Pool_DNSSuffix
$addResult = $ipPoolSpec.subnets.Add($subNetSpec)
My-Logger "Creating IP Pool - $NSXT_IP_Pool_Name ..."
$ipPool = $ipPoolService.create($ipPoolSpec)
2. with NSX-T, there are two operation mode, Policy and Manager, turns out I have to switch mode (as this was created by API) to manager in order to see this entries
Thank you again