Automation

 View Only
  • 1.  IP Pools, Creating with PowerCLI

    Posted Jan 10, 2014 04:51 PM

    So I've been digging through documentation and such, and I cannot figure out how to script creating an IP Pool (with proper settings) for my Datacenters using PowerCLI.  Anyone have any insight into this?



  • 2.  RE: IP Pools, Creating with PowerCLI

    Broadcom Employee
    Posted Jan 10, 2014 05:20 PM

    There is no dedicated cmdlet for this but I have done it with Get-View before, here is the sample code I used at the time, it needs adjusting to work but you get the point :smileywink:

    $dc = New-Object VMware.Vim.ManagedObjectReference

    $dc.type = "Datacenter"

    $dc.Value = "datacenter-2"

    $pool = New-Object VMware.Vim.IpPool

    $pool.name = "MyIPPool"

    $pool.ipv4Config = New-Object VMware.Vim.IpPoolIpPoolConfigInfo

    $pool.ipv4Config.subnetAddress = "192.168.1.0"

    $pool.ipv4Config.netmask = "255.255.255.0"

    $pool.ipv4Config.gateway = "192.168.1.1"

    $pool.ipv4Config.range = "192.168.1.10#1"

    $pool.ipv4Config.dns = New-Object System.String[] (1)

    $pool.ipv4Config.dns[0] = ""

    $pool.ipv4Config.dhcpServerAvailable = $false

    $pool.ipv4Config.ipPoolEnabled = $true

    $pool.ipv6Config = New-Object VMware.Vim.IpPoolIpPoolConfigInfo

    $pool.ipv6Config.subnetAddress = ""

    $pool.ipv6Config.netmask = "ffff:ffff:ffff:ffff:ffff:ffff::"

    $pool.ipv6Config.gateway = ""

    $pool.ipv6Config.dns = New-Object System.String[] (1)

    $pool.ipv6Config.dns[0] = ""

    $pool.ipv6Config.dhcpServerAvailable = $false

    $pool.ipv6Config.ipPoolEnabled = $false

    $pool.dnsDomain = ""

    $pool.dnsSearchPath = ""

    $pool.hostPrefix = ""

    $pool.httpProxy = ""

    $pool.networkAssociation = New-Object VMware.Vim.IpPoolAssociation[] (1)

    $pool.networkAssociation[0] = New-Object VMware.Vim.IpPoolAssociation

    $pool.networkAssociation[0].network = New-Object VMware.Vim.ManagedObjectReference

    $pool.networkAssociation[0].network.type = "DistributedVirtualPortgroup"

    $pool.networkAssociation[0].network.Value = "dvportgroup-178"

    $pool.networkAssociation[0].networkName = ""

    $PoolManager = Get-View -Id 'IpPoolManager-IpPoolManager'

    $PoolManager.CreateIpPool($dc, $pool)



  • 3.  RE: IP Pools, Creating with PowerCLI

    Posted Jan 10, 2014 06:35 PM

    Very helpful!  I am getting "Exception calling "CreateIpPool" with "2" argument(s)", so need to figure that out but you are pointing me in the right direction so thank you for that!



  • 4.  RE: IP Pools, Creating with PowerCLI
    Best Answer

    Broadcom Employee
    Posted Jan 10, 2014 07:03 PM

    A function would be nice if i had the time but the below is more portable, just change the name of the DC to yours on the below code:

        $dcSelection = Get-Datacenter MyDatacenter
        $dc = $dcSelection.ExtensionData.MoRef
        $pool = New-Object VMware.Vim.IpPool 
        $pool.name = "MyIPPool1" 
        $pool.ipv4Config = New-Object VMware.Vim.IpPoolIpPoolConfigInfo 
        $pool.ipv4Config.subnetAddress = "192.168.1.0" 
        $pool.ipv4Config.netmask = "255.255.255.0" 
        $pool.ipv4Config.gateway = "192.168.1.1" 
        $pool.ipv4Config.range = "192.168.1.10#1" 
        $pool.ipv4Config.dns = New-Object System.String[] (1) 
        $pool.ipv4Config.dns[0] = "" 
        $pool.ipv4Config.dhcpServerAvailable = $false 
        $pool.ipv4Config.ipPoolEnabled = $true 
        $pool.ipv6Config = New-Object VMware.Vim.IpPoolIpPoolConfigInfo 
        $pool.ipv6Config.subnetAddress = "" 
        $pool.ipv6Config.netmask = "ffff:ffff:ffff:ffff:ffff:ffff::" 
        $pool.ipv6Config.gateway = "" 
        $pool.ipv6Config.dns = New-Object System.String[] (1) 
        $pool.ipv6Config.dns[0] = "" 
        $pool.ipv6Config.dhcpServerAvailable = $false 
        $pool.ipv6Config.ipPoolEnabled = $false 
        $pool.dnsDomain = "" 
        $pool.dnsSearchPath = "" 
        $pool.hostPrefix = "" 
        $pool.httpProxy = "" 
        $pool.networkAssociation = New-Object VMware.Vim.IpPoolAssociation[] (1) 
        $pool.networkAssociation[0] = New-Object VMware.Vim.IpPoolAssociation 
        $pool.networkAssociation[0].network = New-Object VMware.Vim.ManagedObjectReference 
        $pool.networkAssociation[0].network.type = "DistributedVirtualPortgroup" 
        $pool.networkAssociation[0].network.Value = "dvportgroup-178" 
        $pool.networkAssociation[0].networkName = "" 
        $PoolManager = Get-View -Id 'IpPoolManager-IpPoolManager' 
        $PoolManager.CreateIpPool($dc, $pool) 



  • 5.  RE: IP Pools, Creating with PowerCLI

    Posted Jan 10, 2014 07:18 PM

    Unfortunately no change :smileysad:



  • 6.  RE: IP Pools, Creating with PowerCLI

    Broadcom Employee
    Posted Jan 10, 2014 07:34 PM

    Whats the full error and version of VC?



  • 7.  RE: IP Pools, Creating with PowerCLI

    Posted Jan 10, 2014 09:35 PM

    Well, I had a typo and have it working now.  Interestingly enough, this is built into the vCloud PowerCLI Commandlet's, so not sure if one can use that on regular vSphere or not, but here's the function I came up with based on your code.

    function New-IPPool

    {

    <#

      .SYNOPSIS

      Describe the function here

      .DESCRIPTION

      Describe the function in more detail

      .EXAMPLE

      Give an example of how to use it

      .EXAMPLE

      Give another example of how to use it

      .PARAMETER computername

      The computer name to query. Just one.

      .PARAMETER logname

      The name of a file to write failed computer names to. Defaults to errors.txt.

    #>

      [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='High')]

      param

      (

      [Parameter(Mandatory=$true,

      ValueFromPipeline=$true,

      ValueFromPipelineByPropertyName=$true,

      HelpMessage='The name of the Datacenter the IP Pool will live in')]

      [string]$dataCenter,

      [Parameter(Mandatory=$true,

      ValueFromPipeline=$true,

      ValueFromPipelineByPropertyName=$true,

      HelpMessage='The name of the new IP Pool')]

      [string]$poolName,

      [Parameter(Mandatory=$true,

      ValueFromPipeline=$true,

      ValueFromPipelineByPropertyName=$true,

      HelpMessage='The Subnet Mask of the network')]

      [string]$poolSubnet,

      [Parameter(Mandatory=$true,

      ValueFromPipeline=$true,

      ValueFromPipelineByPropertyName=$true,

      HelpMessage='The Network Address of the new IP Pool')]

      [string]$poolNetwork,

      [Parameter(Mandatory=$true,

      ValueFromPipeline=$true,

      ValueFromPipelineByPropertyName=$true,

      HelpMessage='The gateway of the new IP Pool')]

      [string]$poolGateway,

      [Parameter(Mandatory=$true,

      ValueFromPipeline=$true,

      ValueFromPipelineByPropertyName=$true,

      HelpMessage='The start address of the new IP Pool')]

      [string]$poolRangeStart,

      [Parameter(Mandatory=$true,

      ValueFromPipeline=$true,

      ValueFromPipelineByPropertyName=$true,

      HelpMessage='The size of the IP range of the new IP Pool')]

      [string]$poolRangeNum,

      [Parameter(Mandatory=$true,

      ValueFromPipeline=$true,

      ValueFromPipelineByPropertyName=$true,

      HelpMessage='The Portgroup Association of the new IP Pool')]

      [string]$poolAssociatedPortGroup

      )

      begin

      {

      }

      process

      {

      $dcSelection = Get-Datacenter -Name $dataCenter

      $dc = $dcSelection.ExtensionData.MoRef

      $pool = New-Object Vmware.Vim.IpPool

      $pool.Name = $poolName

      $pool.ipv4Config = New-Object vmware.Vim.IpPoolIpPoolConfigInfo

      $pool.ipv4Config.subnetAddress = $poolNetwork

      $pool.ipv4Config.netmask = $poolSubnet

      $pool.ipv4Config.gateway = $poolGateway

      $pool.ipv4Config.range = $poolRangeStart + "#" + $poolRangeNum

      $pool.ipv4Config.dhcpServerAvailable = $false

      $pool.ipv4Config.ipPoolEnabled = $true 

      $pool.networkAssociation = New-Object VMware.Vim.IpPoolAssociation[] (1) 

      $pool.networkAssociation[0] = New-Object VMware.Vim.IpPoolAssociation 

      $pool.networkAssociation[0].network = New-Object VMware.Vim.ManagedObjectReference 

      $pool.networkAssociation[0].network.type = "DistributedVirtualPortgroup" 

      $pool.networkAssociation[0].network.Value = $poolAssociatedPortGroup 

      $pool.networkAssociation[0].networkName = "" 

      $PoolManager = Get-View -Id 'IpPoolManager-IpPoolManager' 

      $PoolManager.CreateIpPool($dc, $pool) 

      }

    }



  • 8.  RE: IP Pools, Creating with PowerCLI

    Broadcom Employee
    Posted Jan 10, 2014 10:44 PM

    Very nice.



  • 9.  RE: IP Pools, Creating with PowerCLI

    Posted Mar 06, 2018 02:03 PM

    I'm working with a vSphere Essentials license, and I'm trying to figure out how to modify his code to associate a virtualportgroup 'TestPG' with a network protocol profile 'NPP3'. The code is met to associate a DistributedVirtualPortgroup; however, I'm trying to associate a VirtualPortGroup. I've tried a few variations of this and the code completes without error, but the association never happens. I am able to do this manually.

    Here is the snippet from your code.

        $pool.networkAssociation = New-Object VMware.Vim.IpPoolAssociation[] (1)   

        $pool.networkAssociation[0] = New-Object VMware.Vim.IpPoolAssociation   

        $pool.networkAssociation[0].network = New-Object VMware.Vim.ManagedObjectReference   

        $pool.networkAssociation[0].network.type = "DistributedVirtualPortgroup"   

        $pool.networkAssociation[0].network.Value = "dvportgroup-178"   

        $pool.networkAssociation[0].networkName = ""   

        $PoolManager = Get-View -Id 'IpPoolManager-IpPoolManager'   

        $PoolManager.CreateIpPool($dc, $pool)

    Below is how I've modified the code.

    $pool.networkAssociation = New-Object VMware.Vim.IpPoolAssociation[] (1)   

        $pool.networkAssociation[0] = New-Object VMware.Vim.IpPoolAssociation   

        $pool.networkAssociation[0].network = New-Object VMware.Vim.ManagedObjectReference   

        $pool.networkAssociation[0].network.type = "VirtualPortgroup"   

        $pool.networkAssociation[0].network.Value = " "   

        $pool.networkAssociation[0].networkName = "TestPG"   

        $PoolManager = Get-View -Id 'IpPoolManager-IpPoolManager'   

        $PoolManager.CreateIpPool($dc, $pool)