[Azure]使用Powershell輸出經典模式下全部雲服務的VIP

遠行的風發表於2017-01-08

本文介紹如何將經典模式中當前訂閱下的所有云服務與其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";





相關文章