Powershell batch add VM endpoints

Source: Internet
Author: User
Tags myvm

We can use powershell scripts to add VM endpoints in batches. Please refer to the following solutions.

Preparations-PowershellConnectionChina azure

 

1. Download and install Windows azure powershell: http://www.windowsazure.cn/zh-cn/downloads/#cmd-line-tools from the official website

2. Run as Administrator after installation. Right-click the powershell icon and choose "Run as administrator;

3. Run the get-azurepublishsettingsfile-Environment "azurechinacloud" command to download the publishing configuration file of your windows azure subcategory on the open page;

4. Run import-azurepublishsettingsfile in powershell to "Publish the local storage path of the configuration file ";

Solution 1: add custom endpoints in batches

Note: In this example, three ports are added for a virtual machine:

Port name

Protocol

Public port

Private Port

Myport1

TCP

5001

5001

Myport2

TCP

5002

5002

Myport3

UDP

5003

5003

In this example, the cloud service name and Virtual Machine name are both johnsonlinux. To add more ports, add the port configuration to $ newvmendpoints according to the corresponding format. Format: ("Port name", "protocol", "public port", "Private Port ")

$serviceName = "JohnsonLinux"$name = "JohnsonLinux"$newVmEndpoints = ("MyPort1","tcp",5001,5001) ,("MyPort2","tcp",5002,5002) ,("MyPort3","udp",5003,5003)$myVm = Get-AzureVM -ServiceName $serviceName -Name $nameforeach ($endpointConfig in $newVmEndpoints){$myVm | Add-AzureEndpoint -Name $endpointConfig[0] -Protocol $endpointConfig[1] -PublicPort $endpointConfig[2] -LocalPort $endpointConfig[3]}$myVm | Update-AzureVM

Solution 2: Add endpoints in a range in batches

 

Below are some parameter descriptions in the script. Please replace them accordingly.

$ Servicename-name of the cloud service to which the VM belongs

$ Name-VM name

$ Portfrom-Start port number

$ Portto-end port number

$ Protocal-protocol name

In the following example:

1. We have added TCP endpoints 1-150, a total.

2. The values of public ports and private ports are the same.

3. the endpoint name is in the format of protocol name + port number.

4. If a port has been added, the script skips the port.

5. At the same time, we found that currently we can only open up to 150 ports.

 

 

$serviceName = "JohnsonLinux"$name = "JohnsonLinux"$protocol = "tcp"$portFrom = 1$portTo = 150$myVm = Get-AzureVM -ServiceName $serviceName -Name $name $existingPublicPorts = New-Object System.Collections.ArrayList$existingLocalPorts = New-Object System.Collections.ArrayListforeach($endpoint in $myVm | Get-AzureEndpoint){    if($protocal.Equals($endpoint.Protocol))    {        $existingPublicPorts.Add($endpoint.Port)        $existingLocalPorts.Add($endpoint.LocalPort)    }}for($index = $portFrom; $index -le $portTo; $index++){    if(!$existingPublicPorts.Contains($index) -and !$existingLocalPorts.Contains($index))    {        $portName = $protocol + $index        $myVm | Add-AzureEndpoint -Name $portName -Protocol $protocol -PublicPort $index -LocalPort $index    }}$myVm | Update-AzureVM

 

The following are some results after the script is run:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.