本文介绍如何将经典模式中当前订阅下的所有云服务与其VIP的对应关系输出,一共有三种不同的方法:
通过Deployment的VirtualIPs属性来获取(推荐方法):
$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";