次の方法で共有


PowerShell を使用して Azure VM 上に SQL Server を作成する

適用対象: Azure SQL Managed Instance

この PowerShell スクリプトの例では、Azure に Windows SQL Server 仮想マシン (VM) を作成します。

Azure Cloud Shell を使用する

Azure では、ブラウザーを介して使用できる対話型のシェル環境、Azure Cloud Shell がホストされています。 Cloud Shell で Bash または PowerShell を使用して、Azure サービスを操作できます。 ローカル環境に何もインストールしなくても、Cloud Shell にプレインストールされているコマンドを使用して、この記事のコードを実行できます。

Azure Cloud Shell を開始するには、以下のようにします。

オプション 例とリンク
コード ブロックの右上隅にある [使ってみる] を選択します。 [使ってみる] を選択しても、コードは Cloud Shell に自動的にコピーされません。 Azure Cloud Shell の [使ってみる] の例を示すスクリーンショット。
https://shell.azure.com に移動するか、[Cloud Shell を起動する] ボタンを選択して、ブラウザーで Cloud Shell を開きます。 新しいウィンドウで Cloud Shell を起動する方法を示すスクリーンショット。
Azure portal の右上にあるメニュー バーの [Cloud Shell] ボタンを選択します。 Azure portal の [Cloud Shell] ボタンを示すスクリーンショット

Azure Cloud Shell でこの記事のコードを実行するには:

  1. Cloud Shell を開始します。

  2. [コピー] ボタンを選択して、コード ブロックをコードにコピーします。

  3. Windows と Linux では Ctrl+Shift+V キーを選択し、macOS では Cmd+Shift+V キーを選択して、コードを Cloud Shell セッションに貼り付けます。

  4. Enter キーを選択して、コードを実行します。

PowerShell をインストールしてローカルで使用する場合、このチュートリアルでは Azure PowerShell 1.4.0 以降が必要になります。 アップグレードする必要がある場合は、Azure PowerShell モジュールのインストールに関するページを参照してください。 PowerShell をローカルで実行している場合、Connect-AzAccount を実行して Azure との接続を作成することも必要です。

変数の設定

$SubscriptionId = "<Enter Subscription ID>"
$Location = "<Enter Location>"
$ResourceGroupName = "<Enter Resource Group Name>"
$userName = "<Enter User Name for the virtual machine"

$StorageName = "sqlvm" + "storage"
$StorageSku = "Premium_LRS"

$InterfaceName = $ResourceGroupName + "ServerInterface"
$NsgName = $ResourceGroupName + "nsg"
$TCPIPAllocationMethod = "Dynamic"
$VNetName = $ResourceGroupName + "VNet"
$SubnetName = "Default"
$VNetAddressPrefix = "10.0.0.0/16"
$VNetSubnetAddressPrefix = "10.0.0.0/24"
$DomainName = $ResourceGroupName

$VMName = $ResourceGroupName + "VM"
$ComputerName = $ResourceGroupName + "Server"
$VMSize = "Standard_DS13"
$OSDiskName = $VMName + "OSDisk"

$OfferName = "SQL2022-WS2022"
$PublisherName = "MicrosoftSQLServer"
$Version = "latest"
$Sku = "SQLDEV-GEN2"
$License = 'PAYG'

# Define a credential object
$SecurePassword = ConvertTo-SecureString '<strong password>' `
   -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential ($userName, $securePassword)

サンプル スクリプト



# Set subscription context
Connect-AzAccount
$subscriptionContextParams = @{
    SubscriptionId = $SubscriptionId
}
Set-AzContext @subscriptionContextParams

# Create a resource group
$resourceGroupParams = @{
    Name = $resourceGroupName
    Location = $Location
    Tag = @{Owner="SQLDocs-Samples"}
}
$resourceGroup = New-AzResourceGroup @resourceGroupParams



# Create storage account
$StorageAccount = New-AzStorageAccount -ResourceGroupName $ResourceGroupName `
   -Name $StorageName -SkuName $StorageSku `
   -Kind "Storage" -Location $Location

# Create a subnet configuration
$SubnetConfig = New-AzVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $VNetSubnetAddressPrefix

# Create a virtual network
$VNet = New-AzVirtualNetwork -Name $VNetName `
   -ResourceGroupName $ResourceGroupName -Location $Location `
   -AddressPrefix $VNetAddressPrefix -Subnet $SubnetConfig

# Create a public IP address
$PublicIp = New-AzPublicIpAddress -Name $InterfaceName `
-ResourceGroupName $ResourceGroupName -Location $Location `
-AllocationMethod $TCPIPAllocationMethod -DomainNameLabel $DomainName

# Create a network security group rule
$NsgRuleRDP = New-AzNetworkSecurityRuleConfig -Name "RDPRule" -Protocol Tcp `
-Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * `
-DestinationAddressPrefix * -DestinationPortRange 3389 -Access Allow

$NsgRuleSQL = New-AzNetworkSecurityRuleConfig -Name "MSSQLRule"  -Protocol Tcp `
-Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * `
-DestinationAddressPrefix * -DestinationPortRange 1433 -Access Allow

# Create a network security group
$Nsg = New-AzNetworkSecurityGroup -ResourceGroupName $ResourceGroupName `
-Location $Location -Name $NsgName `
-SecurityRules $NsgRuleRDP,$NsgRuleSQL

# Create a network interface
$Interface = New-AzNetworkInterface -Name $InterfaceName `
   -ResourceGroupName $ResourceGroupName -Location $Location `
   -SubnetId $VNet.Subnets[0].Id -PublicIpAddressId $PublicIp.Id `
   -NetworkSecurityGroupId $Nsg.Id

# Create a virtual machine configuration
$VMName = $ResourceGroupName + "VM"
$VMConfig = New-AzVMConfig -VMName $VMName -VMSize $VMSize |
    Set-AzVMOperatingSystem -Windows -ComputerName $VMName -Credential $Cred -ProvisionVMAgent -EnableAutoUpdate |
    Set-AzVMSourceImage -PublisherName $PublisherName -Offer $OfferName -Skus $Sku  -Version $Version |
    Add-AzVMNetworkInterface -Id $Interface.Id

# Create the VM
New-AzVM -ResourceGroupName $ResourceGroupName -Location $Location -VM $VMConfig


# Register the SQL IaaS Agent extension to your subscription
Register-AzResourceProvider -ProviderNamespace Microsoft.SqlVirtualMachine

# Register SQL Server VM with the extension
New-AzSqlVM -Name $VMName -ResourceGroupName $ResourceGroupName -Location $Location `
-LicenseType $License

デプロイのクリーンアップ

次のコマンドを使用して、リソース グループと、それに関連付けられているすべてのリソースを削除します。

# Clean up deployment 
# Stop-AzVM -Name $VMName -ResourceGroupName $ResourceGroupName
# Remove-AzResourceGroup -ResourceGroupName $ResourceGroupName

スクリプトの説明

この記事のスクリプトでは、次のコマンドを使用します。

コマンド Notes
Get-AzVMImageOffer すべての Azure VM イメージを一覧表示します。
Get-AzVMImageSku 特定のオファーの SKU を一覧表示します。
New-AzResourceGroup すべてのリソースを格納するリソース グループを作成します。
New-AzStorageAccount 新しい Azure Storage アカウントの作成を作成します。
New-AzVirtualNetworkSubnetConfig 新しいサブネットを作成して構成します。
New-AzVirtualNetwork 仮想ネットワークを作成します。
New-AzPublicIpAddress パブリック IP アドレスを作成します。
New-AzNetworkSecurityGroup 新しいセキュリティ グループを作成します。
New-AzNetworkInterface ネットワーク インターフェイスを作成します。
New-AzVMConfig 構成可能な仮想マシン オブジェクトを作成します。
Add-AzVMNetworkInterface 仮想マシンにネットワーク インターフェイスを追加します。
Register-AzResourceProvider リソース プロバイダーを登録します。
New-AzSqlVM SQL 仮想マシン リソースを作成または更新します。
Stop-AzVM Azure 仮想マシンを停止します。
Remove-AzResourceGroup Azure 内のリソース グループを削除します。

Azure PowerShell の詳細については、Azure PowerShell のドキュメントをご覧ください。