presents by IT Consulting 109

Azure – PowerShellでセキュリティ規則の追加

概要

AWSをメインで利用しているエンジニアがAzure環境の構築をPowerShellで進める連載。
リソースグループの作成から仮想マシンを起動するまでを連載していく予定です。

第4回は、前回作成した「ネットワークセキュリティグループ」に、AWSで言ううころのセキュリティグループのインバウンドに類似する「受信セキュリティ規則」、アウトバウンドに類似する「送信セキュリティ規則」を追加します。

番外編

  • 第10回 PowerShellでストレージアカウントの作成
  • 第11回 PowerShellで仮想マシン用VHDイメージのアップロード
  • 第12回 PowerShellで仮想マシンをVHDイメージから作成
  • 第13回 PowerShellでディスクの拡張

手順

前提:事前にネットワークセキュリティグループが作成されていること。

# リソースグループ名を設定
$private:resourceGroupName = "rg001"

# ロケーションを設定
$private:location = "japaneast"

# タグを設定
$tags = @{"環境名"="ITC109"}

# パブリック用ネットワークセキュリティグループ
$script:nwSgPublicName = $private:resourceGroupName + "nwsg-public"  

# プライベート用ネットワークセキュリティグループ
$script:nwSgPrivateName = $private:resourceGroupName + "nwsg-private" 

※パブリック用ネットワークセキュリティグループの受信セキュリティ規則に、RDP用の接続ルールを設定するサンプル

##################################################
Write-Host "■■【受信セキュリティ】の設定処理 ■■"
##################################################
$script:gip = "xxx.xxx.xxx.xxx"

# パブリック用ネットワークセキュリティグループに追加
[Microsoft.Azure.Commands.Network.Models.PSSecurityRule[]]$private:rules = @()
$private:rules += New-AzNetworkSecurityRuleConfig -Name "AllowRDP" -Description "RDS接続" -Protocol "TCP" -SourcePortRange "*" -DestinationPortRange "3389" -SourceAddressPrefix $script:gip -DestinationAddressPrefix "*" -Access "Allow" -Priority "100" -Direction "Inbound"

$script:nsg = New-AzNetworkSecurityGroup -Location $private:location -ResourceGroupName $private:resourceGroupName -Name $script:nwSgPublicName -Tag $tags -ErrorAction Stop
$script:nsg.SecurityRules.AddRange($private:rules)

Write-Host "ネットワークセキュリティグループ の 受信セキュリティ規則 に ルール を設定中です。"
$retnsg = Set-AzNetworkSecurityGroup -NetworkSecurityGroup $script:nsg
Write-Host "ネットワークセキュリティグループ の 受信セキュリティ規則 に ルール を設定しました。"
Write-Host ""

##################################################

New-AzNetworkSecurityRuleConfigでネットワークセキュリティグループにセキュリティ規則を追加。

New-AzNetworkSecurityRuleConfig -Name "AllowRDP" -Description "RDS接続" -Protocol "TCP" -SourcePortRange "*" -DestinationPortRange "3389" -SourceAddressPrefix $script:gip -DestinationAddressPrefix "*" -Access "Allow" -Priority "100" -Direction "Inbound"

-Nameで「名前」を設定。

-Name "AllowRDP"

-Descriptionで「説明」を設定。

-Description "RDS接続"

-Protocolで「プロトコル」を設定。

-Protocol "TCP"

-SourcePortRangeで「ソースポート範囲」を設定。

-SourcePortRange "*"  

-DestinationPortRangeで「宛先ポート範囲」を設定。

-DestinationPortRange "3389"

-SourceAddressPrefixで「ソースIPアドレス/CIDR範囲」を設定。

-SourceAddressPrefix $script:gip

-DestinationAddressPrefixで「宛先」を設定。
※Anyで設定する際は「*」

-DestinationAddressPrefix "*"  

-Accessで「アクション」を設定。
※許可[Allow]、拒否[deny]

-Access "Allow" 

-Priorityで「優先度」を設定。

-Priority "100"  

-Directionでセキュリティ規則の登録先を設定。
※受信セキュリティ規則[Inbound]、送信セキュリティ規則[Outbound]

-Direction "Inbound"

補足

複数のルールを一括で追加する為に

[Microsoft.Azure.Commands.Network.Models.PSSecurityRule[]]$private:rules = @()

として配列でルール用の変数を作成し

$private:rules += New-AzNetworkSecurityRuleConfig ~~~

$private:rulesに複数のルールを追加して

$script:nsg.SecurityRules.AddRange($private:rules)
Set-AzNetworkSecurityGroup -NetworkSecurityGroup $script:nsg

Set-AzNetworkSecurityGroupで設定。

この記事を書いた人
名前:TRUE's。 千葉県育ち、神奈川県在住のIT系フリーエンジニア。 IT系のナレッジサイトを不定期で更新中。 フォトグラファー兼エンジニアとして日々勤しんでいる。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です