Last active
April 3, 2018 02:05
-
-
Save mtsukamoto/199d3581dc8b3b66c6dd64ecba803b4c to your computer and use it in GitHub Desktop.
サブスクリプション内の仮想ネットワーク、サブネット、仮想マシンのIPアドレス構成のサマリを作成
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get-AzureVnetSummary.ps1 | |
# ---- | |
# Makio Tsukamoto <[email protected]> | |
# Initial : 2018.03.16 | |
# Latest : 2018.04.03 | |
# 引数の処理 | |
Param( | |
[string]$SubscriptionName, # 情報取得対象のサブスクリプション名、デフォルトはカレントコンテキストのサブスクリプション | |
[string]$OutputFile, # 出力ファイル名、デフォルトは"AzureVnetSummary_サブスクリプション名_年月日-時分秒.json" | |
[switch]$StdOut, # ファイルではなく標準出力に出力させるためのスイッチ | |
[switch]$RawData # 生データを追加出力させるためのスイッチ | |
) | |
$ErrorActionPreference = "Stop" | |
# 出力用関数定義 | |
function Write-Summary { | |
param( | |
[Parameter(ValueFromPipeline=$true,Mandatory=$true)][string]$string, | |
[switch]$NewFile | |
) | |
if ($StdOut) { | |
$string | Write-Output | |
} else { | |
if ($NewFile) { | |
$string | Out-File -filepath $OutputFile -Encoding UTF8 | |
} else { | |
$string | Out-File -filepath $OutputFile -Encoding UTF8 -Append | |
} | |
} | |
} | |
# JSONインデント成型 | |
# see also https://gist.github.com/y-kitami/e798451c7992c36c6fc411cec81b4f10/721424f735211ac5e678b4024b60beaffbdab3c8 | |
function ConvertTo-PrettyJson { | |
param ( | |
[parameter(Mandatory,ValueFromPipeline=$true)][string]$Json, | |
[ValidateLength(1, 1)][string]$IndentChar = ' ', | |
[ValidateRange(1, 20)][int]$IndentWidth = 4 | |
) | |
$indent = $IndentChar * $IndentWidth | |
$indentCount = 0 | |
$lines = @(); | |
foreach ($line in ($Json -split "`r`n")) { | |
if ($line -match '^\s*([\}\]].*)') { | |
$indentCount-- | |
$lines[-1] += " " + $Matches[1].Trim() | |
} else { | |
$lines += ($indent * $indentCount) + $line.Trim() | |
} | |
if ($line -match '[\{\[]\s*$') { | |
$indentCount++ | |
} | |
} | |
$lines -join "`r`n" | |
} | |
# ログイン | |
# Login-AzureRMAccount | |
try{ | |
if ($SubscriptionName) { | |
"[{0}] サブスクリプションを $SubscriptionName に変更します。" -f (Get-Date -Format "yyyy/MM/dd HH:mm:ss") | Write-Output | |
$result = Set-AzureRmContext -SubscriptionName $SubscriptionName | |
} | |
"[{0}] コンテキストを取得します。" -f (Get-Date -Format "yyyy/MM/dd HH:mm:ss") | Write-Output | |
$context = Get-AzureRmContext | |
} catch { | |
Write-Output $Error[0] | |
exit 1 | |
} | |
# 出力ファイル名の決定 | |
if ($StdOut) { | |
"[{0}] 情報は 標準出力 に出力されます。" -f (Get-Date -Format "yyyy/MM/dd HH:mm:ss") | Write-Output | |
} else { | |
if (!($OutputFile)) { | |
$OutputFile = "AzureVnetSummary_{0}_{1}.json" -f $context.Subscription.SubscriptionName, (Get-Date -Format "yyyyMMdd-HHmmss") | |
} | |
"[{0}] 情報は '{1}' に出力されます。" -f ((Get-Date -Format "yyyy/MM/dd HH:mm:ss"),$OutputFile) | Write-Output | |
} | |
# コンテキスト情報の出力 | |
$info = [ordered]@{ | |
'Context' = [ordered]@{ | |
'Account' = $context.Account.Id; | |
'AccountType' = $context.Account.AccountType; | |
'Environment' = $context.Environment.Name; | |
'Subscription' = $context.Subscription.SubscriptionName; | |
'Tenant' = $context.Tenant.Domain; | |
}; | |
} | |
# リソース情報取得 | |
try{ | |
"[{0}] リソースグループを取得します。" -f (Get-Date -Format "yyyy/MM/dd HH:mm:ss") | Write-Output | |
$rgs = Get-AzureRmResourceGroup | |
"[{0}] 仮想マシンを取得します。" -f (Get-Date -Format "yyyy/MM/dd HH:mm:ss") | Write-Output | |
$vms = Get-AzureRmVM | |
"[{0}] 仮想ネットワークを取得します。" -f (Get-Date -Format "yyyy/MM/dd HH:mm:ss") | Write-Output | |
$vnets = Get-AzureRmVirtualNetwork | |
"[{0}] ネットワークインターフェースを取得します。" -f (Get-Date -Format "yyyy/MM/dd HH:mm:ss") | Write-Output | |
$nics = Get-AzureRmNetworkInterface | |
"[{0}] パブリックIPを取得します。" -f (Get-Date -Format "yyyy/MM/dd HH:mm:ss") | Write-Output | |
$pips = Get-AzureRmPublicIpAddress | |
} catch { | |
Write-Output $Error[0] | |
exit 1 | |
} | |
"[{0}] サブネット情報を整理します。" -f (Get-Date -Format "yyyy/MM/dd HH:mm:ss") | Write-Output | |
$subnets = [ordered]@{} | |
foreach ($vnet in $vnets) { | |
foreach ($subnet in $vnet.Subnets) { | |
$subnets[$subnet.Id] = [ordered]@{ | |
'Subnet' = $subnet; | |
'VirtualNetwork' = $vnet; | |
} | |
} | |
} | |
"[{0}] IP設定情報を整理します。" -f (Get-Date -Format "yyyy/MM/dd HH:mm:ss") | Write-Output | |
$ips = [ordered]@{} | |
foreach ($nic in $nics) { | |
foreach ($ip in $nic.IpConfigurations) { | |
$ips[$ip.Id] = [ordered]@{ | |
'IpConfig' = $ip; | |
'NetworkInterface' = $nic; | |
} | |
} | |
} | |
# サマリ(Vnet)の生成 | |
"[{0}] サマリ(Vnet)を生成します。" -f (Get-Date -Format "yyyy/MM/dd HH:mm:ss") | Write-Output | |
$vnetinfo = [ordered]@{} | |
foreach ($rg in $rgs) { | |
$labelRg = "[RG] {0} ({1})" -f ($rg.ResourceGroupName,$rg.Location) | |
$vnetinfo[$labelRg] = [ordered]@{} | |
foreach ($vnet in ($vnets | where {$_.ResourceGroupName -eq $rg.ResourceGroupName})) { | |
$labelVnet = "[Vnet] {0} ({1})" -f ($vnet.Name,($vnet.AddressSpace.AddressPrefixes -join ", ")) | |
$vnetinfo[$labelRg][$labelVnet] = [ordered]@{} | |
foreach ($subnet in $vnet.Subnets) { | |
$labelSubnet = "[Subnet] {0} ({1})" -f ($subnet.Name,$subnet.AddressPrefix) | |
$labelIps = [ordered]@{} | |
$vnetinfo[$labelRg][$labelVnet][$labelSubnet] = [ordered]@{} | |
foreach ($id in $subnet.IpConfigurations.Id) { | |
$nic = $ips[$id].NetworkInterface | |
$ip = $ips[$id].IpConfig | |
$labelIp = "{0}({1})" -f ($ip.PrivateIpAddress,$ip.PrivateIpAllocationMethod) | |
$valueIp = "" | |
if ($nic.VirtualMachine) { | |
$vm = ($vms | where {$_.id -eq $nic.VirtualMachine.Id}) | |
$valueIp = "[VM] {0}" -f ($vm.Name) | |
} else { | |
$valueIp = "[NIC] {0}" -f ($nic.Name) | |
} | |
if ($ip.PublicIpAddress) { | |
$pip = ($pips | where {$_.id -eq $ip.PublicIpAddress.Id}) | |
$valueIp += ", [PIP] {0}({1})" -f ($pip.IpAddress, $pip.PublicIpAllocationMethod) | |
} | |
$vnetinfo[$labelRg][$labelVnet][$labelSubnet][$labelIp] = $valueIp | |
} | |
} | |
} | |
} | |
$info['VirtualNetwork'] = $vnetinfo | |
# サマリ(VM)の生成 | |
"[{0}] サマリ(VM)を生成します。" -f (Get-Date -Format "yyyy/MM/dd HH:mm:ss") | Write-Output | |
$vminfo = [ordered]@{} | |
foreach ($rg in $rgs) { | |
$labelRg = "[RG] {0} ({1})" -f ($rg.ResourceGroupName,$rg.Location) | |
$vminfo[$labelRg] = [ordered]@{} | |
foreach ($vm in ($vms | where {$_.ResourceGroupName -eq $rg.ResourceGroupName})) { | |
$vnet = $subnets[$nic.IpConfigurations[0].Subnet.Id].VirtualNetwork | |
$labelVm = "[VM] {0} ([vnet] {1})" -f ($vm.Name, $vnet.Name) | |
$vminfo[$labelRg][$labelVm] = [ordered]@{} | |
foreach ($id in $vm.NetworkInterfaceIDs) { | |
$nic = ($nics | where {$_.id -eq $id}) | |
foreach ($ip in $nic.IpConfigurations) { | |
$labelIp = "{0}({1})" -f ($ip.PrivateIpAddress,$ip.PrivateIpAllocationMethod) | |
$subnet = $subnets[$ip.Subnet.Id].Subnet | |
$valueIp = "[Subnet] {0}, [NIC] {1}" -f ($subnet.Name, $nic.Name) | |
if ($ip.PublicIpAddress) { | |
$pip = ($pips | where {$_.id -eq $ip.PublicIpAddress.Id}) | |
$valueIp += ", [PIP] {0}({1})" -f ($pip.IpAddress, $pip.PublicIpAllocationMethod) | |
} | |
$vminfo[$labelRg][$labelVm][$labelIp] = $valueIp | |
} | |
} | |
} | |
} | |
$info['VirtualMachine'] = $vminfo | |
# リソース情報出力 | |
if ($RawData) { | |
"[{0}] リソース情報(RawData)を出力対象に追加します。" -f (Get-Date -Format "yyyy/MM/dd HH:mm:ss") | Write-Output | |
$info['RawData'] = [ordered]@{ | |
'ResourceGroups' = $rgs; | |
'VirtualMachines' = $vms; | |
'VirtualNetworkss' = $vnets; | |
'NetworkInterfaces' = $nics; | |
} | |
} | |
# 出力 | |
if ($StdOut) { | |
"[{0}] 情報を 標準出力 に出力します。" -f (Get-Date -Format "yyyy/MM/dd HH:mm:ss") | Write-Output | |
} else { | |
"[{0}] 情報を '{1}' に出力します。" -f (Get-Date -Format "yyyy/MM/dd HH:mm:ss"), $OutputFile | Write-Output | |
} | |
$info | ConvertTo-Json -Depth 10 | ConvertTo-PrettyJson | Write-Summary -NewFile | |
"[{0}] 出力されました。" -f (Get-Date -Format "yyyy/MM/dd HH:mm:ss") | Write-Output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
出力JSONのインデント修正など。