title | description | services | documentationcenter | author | manager | editor | ms.assetid | ms.service | ms.workload | pms.tgt_pltfrm | ms.devlang | ms.topic | ms.date | ms.author | ms.custom |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Create a Windows virtual machine by using PowerShell in Azure Stack | Microsoft Docs |
Create a Windows virtual machine with PowerShell in Azure Stack. |
azure-stack |
SnehaGunda |
byronr |
azure-stack |
na |
na |
na |
quickstart |
09/25/2017 |
sngun |
mvc |
Applies to: Azure Stack integrated systems
This guide details using PowerShell to create a Windows Server 2016 virtual machine in Azure Stack. You can run the steps described in this article either from the Azure Stack Development Kit, or from a Windows-based external client if you are connected through VPN.
-
Make sure that your Azure Stack operator has added the “Windows Server 2016” image to the Azure Stack marketplace.
-
Azure Stack requires a specific version of Azure PowerShell to create and manage the resources. If you don't have PowerShell configured for Azure Stack, follow the steps to install and configure PowerShell.
A resource group is a logical container into which Azure Stack resources are deployed and managed. From your development kit or the Azure Stack integrated system, run the following code block to create a resource group. We have assigned values for all variables in this document, you can use them as is or assign a different value.
# Create variables to store the location and resource group names.
$location = "local"
$ResourceGroupName = "myResourceGroup"
New-AzureRmResourceGroup `
-Name $ResourceGroupName `
-Location $location
Create a storage account, and a storage container to store the Windows Server 2016 image.
# Create variables to store the storage account name and the storage account SKU information
$StorageAccountName = "mystorageaccount"
$SkuName = "Standard_LRS"
# Create a new storage account
$StorageAccount = New-AzureRMStorageAccount `
-Location $location `
-ResourceGroupName $ResourceGroupName `
-Type $SkuName `
-Name $StorageAccountName
Set-AzureRmCurrentStorageAccount `
-StorageAccountName $storageAccountName `
-ResourceGroupName $resourceGroupName
# Create a storage container to store the virtual machine image
$containerName = 'osdisks'
$container = New-AzureStorageContainer `
-Name $containerName `
-Permission Blob
Create a virtual network, subnet, and a public IP address. These resources are used to provide network connectivity to the virtual machine.
# Create a subnet configuration
$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig `
-Name mySubnet `
-AddressPrefix 192.168.1.0/24
# Create a virtual network
$vnet = New-AzureRmVirtualNetwork `
-ResourceGroupName $ResourceGroupName `
-Location $location `
-Name MyVnet `
-AddressPrefix 192.168.0.0/16 `
-Subnet $subnetConfig
# Create a public IP address and specify a DNS name
$pip = New-AzureRmPublicIpAddress `
-ResourceGroupName $ResourceGroupName `
-Location $location `
-AllocationMethod Static `
-IdleTimeoutInMinutes 4 `
-Name "mypublicdns$(Get-Random)"
The network security group secures the virtual machine by using inbound and outbound rules. Lets create an inbound rule for port 3389 to allow incoming Remote Desktop connections and an inbound rule for port 80 to allow incoming web traffic.
# Create an inbound network security group rule for port 3389
$nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig `
-Name myNetworkSecurityGroupRuleRDP `
-Protocol Tcp `
-Direction Inbound `
-Priority 1000 `
-SourceAddressPrefix * `
-SourcePortRange * `
-DestinationAddressPrefix * `
-DestinationPortRange 3389 `
-Access Allow
# Create an inbound network security group rule for port 80
$nsgRuleWeb = New-AzureRmNetworkSecurityRuleConfig `
-Name myNetworkSecurityGroupRuleWWW `
-Protocol Tcp `
-Direction Inbound `
-Priority 1001 `
-SourceAddressPrefix * `
-SourcePortRange * `
-DestinationAddressPrefix * `
-DestinationPortRange 80 `
-Access Allow
# Create a network security group
$nsg = New-AzureRmNetworkSecurityGroup `
-ResourceGroupName $ResourceGroupName `
-Location $location `
-Name myNetworkSecurityGroup `
-SecurityRules $nsgRuleRDP,$nsgRuleWeb
The network card connects the virtual machine to a subnet, network security group, and public IP address.
# Create a virtual network card and associate it with public IP address and NSG
$nic = New-AzureRmNetworkInterface `
-Name myNic `
-ResourceGroupName $ResourceGroupName `
-Location $location `
-SubnetId $vnet.Subnets[0].Id `
-PublicIpAddressId $pip.Id `
-NetworkSecurityGroupId $nsg.Id
Create a virtual machine configuration. The configuration includes the settings that are used when deploying the virtual machine such as a virtual machine image, size, credentials.
# Define a credential object to store the username and password for the virtual machine
$UserName='demouser'
$Password='Password@123'| ConvertTo-SecureString -Force -AsPlainText
$Credential=New-Object PSCredential($UserName,$Password)
# Create the virtual machine configuration object
$VmName = "VirtualMachinelatest"
$VmSize = "Standard_A1"
$VirtualMachine = New-AzureRmVMConfig `
-VMName $VmName `
-VMSize $VmSize
$VirtualMachine = Set-AzureRmVMOperatingSystem `
-VM $VirtualMachine `
-Windows `
-ComputerName "MainComputer" `
-Credential $Credential
$VirtualMachine = Set-AzureRmVMSourceImage `
-VM $VirtualMachine `
-PublisherName "MicrosoftWindowsServer" `
-Offer "WindowsServer" `
-Skus "2016-Datacenter" `
-Version "latest"
$osDiskName = "OsDisk"
$osDiskUri = '{0}vhds/{1}-{2}.vhd' -f `
$StorageAccount.PrimaryEndpoints.Blob.ToString(),`
$vmName.ToLower(), `
$osDiskName
# Sets the operating system disk properties on a virtual machine.
$VirtualMachine = Set-AzureRmVMOSDisk `
-VM $VirtualMachine `
-Name $osDiskName `
-VhdUri $OsDiskUri `
-CreateOption FromImage | `
Add-AzureRmVMNetworkInterface -Id $nic.Id
#Create the virtual machine.
New-AzureRmVM `
-ResourceGroupName $ResourceGroupName `
-Location $location `
-VM $VirtualMachine
To remote into the virtual machine that you created in the previous step, you need its public IP address. Run the following command to get the public IP address of the virtual machine:
Get-AzureRmPublicIpAddress `
-ResourceGroupName $ResourceGroupName | Select IpAddress
Use the following command to create a Remote Desktop session with the virtual machine. Replace the IP address with the publicIPAddress of your virtual machine. When prompted, enter the username and password that you used when creating the virtual machine.
mstsc /v <publicIpAddress>
Now that you have logged in to the Azure VM, you can use a single line of PowerShell to install IIS and enable the local firewall rule to allow web traffic. Open a PowerShell prompt and run the following command:
Install-WindowsFeature -name Web-Server -IncludeManagementTools
With IIS installed and port 80 now open on your VM from the Internet, you can use a web browser of your choice to view the default IIS welcome page. Be sure to use the publicIpAddress you documented above to visit the default page.
When no longer needed, use the following command to remove the resource group that contains the virtual machine and its related resources:
Remove-AzureRmResourceGroup `
-Name $ResourceGroupName
In this quick start, you’ve deployed a simple Windows virtual machine. To learn more about Azure Stack virtual machines, continue to Considerations for Virtual Machines in Azure Stack.