[Azure]使用Powershell输出经典模式下全部云服务的VIP

60 篇文章 0 订阅
59 篇文章 0 订阅

本文介绍如何将经典模式中当前订阅下的所有云服务与其VIP的对应关系输出,一共有三种不同的方法:

通过DeploymentVirtualIPs属性来获取(推荐方法):

$services = Get-AzureService;
$VerbosePreference = "SilentlyContinue";
$dnsResult = New-Object 'System.Collections.Generic.Dictionary[string,string]';
foreach($Service in $services)
{
    $Deployment = Get-AzureDeployment -ServiceName $Service.ServiceName;
    if ($Deployment.VirtualIPs.Count -eq 0)
    {
        $dnsResult[$Service.ServiceName] = "Not found";
        continue;
    }
    foreach($VirtualIP in $Deployment.VirtualIPs)
    {
        if($VirtualIP.IsDnsProgrammed -eq $true)
        {
            $ipAddress = $VirtualIP.Address;
            if ($ipAddress -ne $NULL)
            {
                $dnsResult[$Service.ServiceName] = $ipAddress;
            } else {
                $dnsResult[$Service.ServiceName] = "Not found";
            }
        }
    }
    
}
$column1 = @{expression="Key"; width=30;label="CloudService"; alignment="left"}
$column2 = @{expression="Value"; width=20;label="VIP"; alignment="right"}
$dnsResult | Format-Table $column1, $column2
$VerbosePreference = "Continue";


通过NSLookup的方式来解析:

$services = Get-AzureService;
$VerbosePreference = "SilentlyContinue";
$dnsResult = New-Object 'System.Collections.Generic.Dictionary[string,string]';
foreach($service in $services)
{
    $dns = $service.ServiceName + ".chinacloudapp.cn";
    $ipAddress = Resolve-DnsName -Name $dns -ErrorAction Ignore;
    if ($ipAddress -ne $NULL)
    {
        $dnsResult[$service.ServiceName] = $ipAddress.Address;
    } else {
        $dnsResult[$service.ServiceName] = "Not found";
    }
}
$column1 = @{expression="Key"; width=30;label="CloudService"; alignment="left"}
$column2 = @{expression="Value"; width=20;label="VIP"; alignment="right"}
$dnsResult | Format-Table $column1, $column2
$VerbosePreference = "Continue"; 


使用Endpoint的方式:

$VerbosePreference = "SilentlyContinue";
$services = Get-Azureservice;
$dnsResult = New-Object 'System.Collections.Generic.Dictionary[string,string]';
foreach($service in $services)
{
    $vms = Get-AzureVM -ServiceName $service.ServiceName -WarningAction Ignore;
    if ($vms -ne $NULL)
    {
        $checked = $FALSE;
        foreach($vm in $vms)
        {
            $checked = $FALSE;
            $endpoints = Get-AzureEndpoint -VM $vm.VM;
            foreach($endpoint in $endpoints)
            {
                if ($endpoint.InternalLoadBalancerName -eq $NULL -and $endpoint.Vip -ne $NULL)
                {
                    $dnsResult[$service.ServiceName] = $endpoint.Vip;
                    $checked = $TRUE;
                    break;
                }
            }
        }
        if ($checked -ne $TRUE) {
            $dnsResult[$service.ServiceName] = "Not found";
        }
    } else {
        $dnsResult[$service.ServiceName] = "Not found";
    }
}
$column1 = @{expression="Key"; width=30;label="CloudService"; alignment="left"}
$column2 = @{expression="Value"; width=20;label="VIP"; alignment="right"}
$dnsResult | Format-Table $column1, $column2
$VerbosePreference = "Continue";




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值