Guest User

Untitled

a guest
Dec 19th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. # Create variables to store the location and resource group names.
  2. $location = "<location azure stack>"
  3. $ResourceGroupName = "RG-IMG-LNX"
  4.  
  5. New-AzureRmResourceGroup `
  6. -Name $ResourceGroupName `
  7. -Location $location
  8.  
  9. # Create variables to store the storage account name and the storage account SKU information
  10. $StorageAccountName = "imgvmlnxstg"
  11. $SkuName = "Standard_LRS"
  12.  
  13. # Create a new storage account
  14. $StorageAccount = New-AzureRMStorageAccount `
  15. -Location $location `
  16. -ResourceGroupName $ResourceGroupName `
  17. -Type $SkuName `
  18. -Name $StorageAccountName
  19.  
  20. Set-AzureRmCurrentStorageAccount `
  21. -StorageAccountName $storageAccountName `
  22. -ResourceGroupName $resourceGroupName
  23.  
  24. # Create a storage container to store the virtual machine image
  25. $containerName = 'osdisks'
  26. $container = New-AzureStorageContainer `
  27. -Name $containerName `
  28. -Permission Blob
  29.  
  30. # Create a subnet configuration
  31. $subnetConfig = New-AzureRmVirtualNetworkSubnetConfig `
  32. -Name InternalSubnet `
  33. -AddressPrefix 192.168.1.0/24
  34.  
  35. # Create a virtual network
  36. $vnet = New-AzureRmVirtualNetwork `
  37. -ResourceGroupName $ResourceGroupName `
  38. -Location $location `
  39. -Name IMG-LNX-Vnet `
  40. -AddressPrefix 192.168.0.0/16 `
  41. -Subnet $subnetConfig
  42.  
  43. # Create a public IP address and specify a DNS name
  44. $pip = New-AzureRmPublicIpAddress `
  45. -ResourceGroupName $ResourceGroupName `
  46. -Location $location `
  47. -AllocationMethod Static `
  48. -IdleTimeoutInMinutes 4 `
  49. -Name "ippublic$(Get-Random)"
  50.  
  51. # Create an inbound network security group rule for port 22
  52. $nsgRuleSSH = New-AzureRmNetworkSecurityRuleConfig -Name IMG-LNX-RuleRDP -Protocol Tcp `
  53. -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
  54. -DestinationPortRange 22 -Access Allow
  55.  
  56. # Create an inbound network security group rule for port 80
  57. $nsgRuleWeb = New-AzureRmNetworkSecurityRuleConfig -Name IMG-LNX-RuleWWW -Protocol Tcp `
  58. -Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
  59. -DestinationPortRange 80 -Access Allow
  60.  
  61. # Create a network security group
  62. $nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName $ResourceGroupName -Location $location `
  63. -Name IMG-LNX-NSG -SecurityRules $nsgRuleSSH,$nsgRuleWeb
  64.  
  65. # Create a virtual network card and associate it with public IP address and NSG
  66. $nic = New-AzureRmNetworkInterface `
  67. -Name IMG-WS-NIC `
  68. -ResourceGroupName $ResourceGroupName `
  69. -Location $location `
  70. -SubnetId $vnet.Subnets[0].Id `
  71. -PublicIpAddressId $pip.Id `
  72. -NetworkSecurityGroupId $nsg.Id
  73.  
  74. # Define a credential object.
  75. $UserName='demouser'
  76. $securePassword = 'Password@123'| ConvertTo-SecureString -Force -AsPlainText
  77. $cred = New-Object System.Management.Automation.PSCredential ($UserName, $securePassword)
  78.  
  79. # Create the virtual machine configuration object
  80. $VmName = "Ubuntu16"
  81. $VmSize = "Standard_D2_v2"
  82. $VirtualMachine = New-AzureRmVMConfig `
  83. -VMName $VmName `
  84. -VMSize $VmSize
  85.  
  86. $VirtualMachine = Set-AzureRmVMOperatingSystem `
  87. -VM $VirtualMachine `
  88. -Linux `
  89. -ComputerName "Ubuntu16" `
  90. -Credential $cred
  91.  
  92. $urlOfImageVhd = "<url vhd file in container blob>"
  93.  
  94. #Create the OS disk URI
  95.  
  96. $osDiskName = "OsDisk"
  97. $osDiskUri = '{0}vhds/{1}-{2}.vhd' -f `
  98. $StorageAccount.PrimaryEndpoints.Blob.ToString(),`
  99. $vmName.ToLower(), `
  100. $osDiskName
  101.  
  102. # Sets the operating system disk properties on a virtual machine.
  103. $VirtualMachine = Set-AzureRmVMOSDisk `
  104. -VM $VirtualMachine `
  105. -Name $osDiskName `
  106. -VhdUri $OsDiskUri `
  107. -CreateOption FromImage -SourceImageUri $urlOfImageVhd -Linux | Add-AzureRmVMNetworkInterface -Id $nic.Id
  108.  
  109. # Configure SSH Keys
  110. #$sshPublicKey = Get-Content "$env:USERPROFILE\.ssh\id_rsa.pub"
  111.  
  112. # Adds the SSH Key to the virtual machine
  113. #Add-AzureRmVMSshPublicKey -VM $VirtualMachine `
  114. # -KeyData $sshPublicKey `
  115. # -Path "/home/azureuser/.ssh/authorized_keys"
  116.  
  117. #Create the virtual machine.
  118. New-AzureRmVM `
  119. -ResourceGroupName $ResourceGroupName `
  120. -Location $location `
  121. -VM $VirtualMachine
Add Comment
Please, Sign In to add comment