Public/nic.ps1
|
function Get-CSNic { <# .SYNOPSIS Lists the NICs of a virtual machine. .DESCRIPTION Wraps listNics. Accepts -VM (a name or a piped VM object) or -VirtualMachineId. Each NIC's secondary IPs are in its 'secondaryip' property; pipe those to Remove-CSNicIp to release them. .PARAMETER VirtualMachineId The ID of the VM whose NICs to list .PARAMETER VM A VM name or VM object. Binds from the pipeline. .PARAMETER NicId Only return this NIC .PARAMETER NetworkId Only return the VM's NIC on this network .PARAMETER ForDisplay Filter by the display flag (root admin only) .PARAMETER Keyword Filter by keyword .PARAMETER Page Page number of results to return .PARAMETER PageSize Number of results per page .EXAMPLE Get-CSNic -VM 'web-01' Lists web-01's NICs. .EXAMPLE Get-CSVM -Keyword 'web-' | Get-CSNic | Select-Object virtualmachineid, networkname, ipaddress, macaddress Shows the addresses of every NIC on the web VMs. .EXAMPLE (Get-CSNic -VM 'web-01' -NetworkId net-uuid).secondaryip Lists the secondary IPs on web-01's NIC in one network. #> [CmdletBinding()] param( [Parameter(Mandatory = $false)] [string]$VirtualMachineId, [Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [Alias('Name')][object]$VM, [string]$NicId, [string]$NetworkId, [bool]$ForDisplay, [string]$Keyword, [int]$Page, [int]$PageSize ) process { $resolvedId = Resolve-CSVMTagId -VirtualMachineId $VirtualMachineId -VM $VM $apiParams = @{ virtualmachineid = $resolvedId } Add-CSOptionalParameter -ApiParameters $apiParams -BoundParameters $PSBoundParameters -Map ([ordered]@{ NicId = 'nicid'; NetworkId = 'networkid'; ForDisplay = 'fordisplay'; Keyword = 'keyword' Page = 'page'; PageSize = 'pagesize' }) ConvertFrom-CSResponse -Response (Invoke-CSApiRequest -Command 'listNics' -Parameters $apiParams) -Command 'listNics' } } function Add-CSNicIp { <# .SYNOPSIS Adds a secondary IP address to a NIC. .DESCRIPTION Wraps addIpToNic. Leave out -IpAddress to have CloudStack pick a free address in the NIC's network. This is an asynchronous job; use -Wait to get the new secondary IP back (its id is what Remove-CSNicIp takes). Accepts NIC objects from Get-CSNic on the pipeline. .PARAMETER NicId The NIC to add the address to (binds from a piped NIC's id) .PARAMETER IpAddress The address to add. Allocated automatically if omitted. .PARAMETER Wait Wait for the async job to finish and return the new secondary IP .EXAMPLE Add-CSNicIp -NicId nic-uuid -IpAddress 10.1.1.50 -Wait Adds a specific secondary IP to a NIC. .EXAMPLE Get-CSNic -VM 'lb-01' -NetworkId net-uuid | Add-CSNicIp -Wait Adds an automatically chosen secondary IP to lb-01's NIC in one network. #> [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')] param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [Alias('Id')] [string]$NicId, [string]$IpAddress, [switch]$Wait ) process { $apiParams = @{ nicid = $NicId } if ($PSBoundParameters.ContainsKey('IpAddress')) { $apiParams['ipaddress'] = $IpAddress } $description = if ($IpAddress) { "Add secondary IP $IpAddress" } else { 'Add secondary IP' } if ($PSCmdlet.ShouldProcess("NIC $NicId", $description)) { Invoke-CSAsyncApiRequest -Command 'addIpToNic' -Parameters $apiParams -Wait:$Wait } } } function Remove-CSNicIp { <# .SYNOPSIS Removes a secondary IP address from a NIC. .DESCRIPTION Wraps removeIpFromNic. -Id is the ID of the secondary IP itself, not of the NIC. Accepts secondary IP objects on the pipeline: those in a NIC's 'secondaryip' property, or those returned by Add-CSNicIp -Wait. This is an asynchronous job; use -Wait to block until it finishes. .PARAMETER Id The ID of the secondary IP to remove (binds from a piped secondary IP's id) .PARAMETER Wait Wait for the async job to finish and return its result .EXAMPLE Remove-CSNicIp -Id secondary-ip-uuid Removes one secondary IP after prompting for confirmation. .EXAMPLE (Get-CSNic -VM 'lb-01').secondaryip | Where-Object ipaddress -eq '10.1.1.50' | Remove-CSNicIp -Confirm:$false Removes a secondary IP from lb-01 by address. #> [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')] param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [string]$Id, [switch]$Wait ) process { if ($PSCmdlet.ShouldProcess("secondary IP $Id", 'Remove from NIC')) { Invoke-CSAsyncApiRequest -Command 'removeIpFromNic' -Parameters @{ id = $Id } -Wait:$Wait } } } function Set-CSNicIp { <# .SYNOPSIS Changes the primary IP address of a VM's NIC. .DESCRIPTION Wraps updateVmNicIp. Leave out -IpAddress to have CloudStack pick a new free address. The VM usually has to be stopped, and the guest OS has to pick up the new address (DHCP or a config change). This is an asynchronous job; use -Wait to get the updated VM back. Accepts NIC objects from Get-CSNic on the pipeline. .PARAMETER NicId The NIC to change (binds from a piped NIC's id) .PARAMETER IpAddress The new address. Never bound from the pipeline, because a piped NIC's own ipaddress is its current address. Allocated automatically if omitted. .PARAMETER Wait Wait for the async job to finish and return the updated VM .EXAMPLE Set-CSNicIp -NicId nic-uuid -IpAddress 10.1.1.20 -Wait Gives a NIC a new primary address. .EXAMPLE Get-CSNic -VM 'db-01' -NetworkId net-uuid | Set-CSNicIp -IpAddress 10.1.1.20 Changes db-01's address in one network. #> [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')] param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [Alias('Id')] [string]$NicId, [string]$IpAddress, [switch]$Wait ) process { $apiParams = @{ nicid = $NicId } if ($PSBoundParameters.ContainsKey('IpAddress')) { $apiParams['ipaddress'] = $IpAddress } $description = if ($IpAddress) { "Change primary IP to $IpAddress" } else { 'Change primary IP' } if ($PSCmdlet.ShouldProcess("NIC $NicId", $description)) { Invoke-CSAsyncApiRequest -Command 'updateVmNicIp' -Parameters $apiParams -Wait:$Wait } } } |