您的位置:首页 > 运维架构 > Shell

powershell部署Azure云虚拟机的命令流程

2017-09-16 15:31 363 查看
公司需要对业务做托管,使用Azure云上的windows服务器,用程序创建脚本远程管理,根据业务完成情况执行powershell脚本创建并开启虚拟机做后台worker,结合我在网上搜索的和azure.cn上的文档,执行下面的一套命令可以正常创建虚拟机。

我使用的powershell的majar是3,

有一个问题,就是这套脚本只能在powershell窗口下运行,使用cmd运行该脚本会有变量报错,因为cmd不支持powershell的高级功能。

一、登录Azure的相关操作:





0、Add-AzureRmAccount -Environment azurechinacloud //增加账号信息
1、Login-AzureRmAccount -Environment "AzureChinaCloud" //登陆,否则会提示没有映射
2、Get-AzurePublishSettingsFile -Environment "AzureChinaCloud"   //登陆azure中国服务
3、Enable-PSRemoting   //支持远程连接,默认选是
4、Get-AzureSubscription //查看订阅
5、Get-AzureStorageAccount //查看存储账户
6、Set-AzureSubscription -SubscriptionName '<SubscriptionName>' -CurrentStorageAccount '<StorageAccount>'  //订阅名称和存储账户
7、Set-ExecutionPolicy Unrestricted//更改脚本运行权限


二、创建资源组和可用性集:

# Variables for common values
$resourceGroup = "YangGangRS"
$location = "chinaeast"
$vmName = "YangGangVM"
# Create a resource group
New-AzureRmResourceGroup -Name $resourceGroup -Location $location
# Create a high availible set
New-AzureRmAvailabilitySet -Location ChinaEast -Name YangGangHA -ResourceGroupName $resourceGroup -Managed -PlatformFaultDomainCount 2 -PlatformUpdateDomainCount 2


三、在资源组下创建网络组相关资源

 

# Create a subnet configuration
$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name YangGangSubNet -AddressPrefix 192.168.1.0/24
# Create a virtual network
$vnet = New-AzureRmVirtualNetwork -ResourceGroupName $resourceGroup -Location $location -Name YangGangVN -AddressPrefix 192.168.0.0/16 -Subnet $subnetConfig
# Create a public IP address and specify a DNS name
$pip = New-AzureRmPublicIpAddress -ResourceGroupName $resourceGroup -Location $location -Name "mypublicdns$(Get-Random)" -AllocationMethod Static -IdleTimeoutInMinutes 4
# Create an inbound network security group rule for port 3389
$nsgRuleSSH = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleRDP -Protocol Tcp -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389 -Access Allow
# Create a network security group
$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName $resourceGroup -Location $location -Name YangGangNSG -SecurityRules $nsgRuleSSH


四、创建虚拟机相关资源

# Create user object //下面第一条是会弹出输入用户名密码的窗口的,我用脚本执行的时候需要一次性写入,所以没有使用它
$cred = Get-Credential -Message "Enter a username and password for the virtual machine."
$uname="YGAdmin"
$pwd=ConvertTo-SecureString "Passw0rd1234" -AsPlainText -Force;
$cred=New-Object System.Management.Automation.PSCredential($uname,$pwd);
# Create a virtual network card and associate with public IP address and NS
$nic = New-AzureRmNetworkInterface -Name YangGangVMNic -ResourceGroupName $resourceGroup -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id
# 获取可用性集
$availabilitySet = Get-AzureRmAvailabilitySet -ResourceGroupName $resourceGroup -Name YangGangHA
# Create a virtual machine configuration
$vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize Standard_D2 -Ava
aeeb
ilabilitySetId $availabilitySet.Id | Set-AzureRmVMOperatingSystem -Windows -ComputerName $vmName -Credential $cred | Set-AzureRmVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2012-R2-Datacenter-zhcn -Version latest | Add-AzureRmVMNetworkInterface -Id $nic.Id
# Create a virtual machine
New-AzureRmVM -ResourceGroupName $resourceGroup -Location $location -VM $vmConfig

五、创建VM镜像

1、Stop-AzureRmVM -ResourceGroupName 'YangGangRS' -Name 'YangGangVM' -Force//确保VM已经解除分配
2、Set-AzureRmVm -ResourceGroupName 'YangGangRS' -Name 'YangGangVM' -Generalized //设置虚拟机状态为通用化
3、$vm = Get-AzureRmVM -Name 'YangGangVM' -ResourceGroupName 'YangGangRS' //获取虚拟机
4、$image = New-AzureRmImageConfig -Location 'China East' -SourceVirtualMachineId $vm.ID  //创建映像配置
5、New-AzureRmImage -Image $image -ImageName ‘YangGang-image’-ResourceGroupName 'YangGangRS'//创建映像


六、使用自定义镜像在资源组中创建虚拟机

为了使脚本每一次都可以不用登陆直接运行,使用Save-AzureRmContext -Profile (Add-AzureRmAccount) -Path C:\myprofile.json命令提取出登陆信息,脚本直接加载改文件就相当于登陆了。下面是使用我自己创建的镜像创建虚拟机的流程,修改网卡名称和虚拟机名称便可以再次创建新的虚拟机。

Import-AzureRmContext -Path c:/myprofile.json;
$vmName = "YangGangVM1"
$Nic = "YangGangVM1Nic"
$vmImage = "YangGang-image"
$location = "chinaeast"
$resourceGroup = "YangGangRS"
$uname="YGAdmin"
$pwd=ConvertTo-SecureString "Passw0rd1234" -AsPlainText -Force;
$cred=New-Object System.Management.Automation.PSCredential($uname,$pwd);
$nic = New-AzureRmNetworkInterface -Name $Nic -ResourceGroupName $resourceGroup -Location $location -SubnetId $vnet.Subnets[0].Id -NetworkSecurityGroupId $nsg.Id
$availabilitySet = Get-AzureRmAvailabilitySet -ResourceGroupName $resourceGroup -Name YangGangHA
$vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize Standard_D2 -AvailabilitySetId $availabilitySet.Id | Set-AzureRmVMOperatingSystem -Windows -ComputerName $vmName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
$image = Get-AzureRmImage -ImageName $vmImage -ResourceGroupName $resourceGroup
$vmConfig = Set-AzureRmVMSourceImage -VM $vmConfig -Id $image.Id
$vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $nic.Id
New-AzureRmVM -ResourceGroupName $resourceGroup -Location $location -VM $vmConfig

七、其他常用命令

//删除虚拟机
Remove-AzureRmVM -ResourceGroupName 'YangGangRS' -Name 'YangGangVM'
//开启虚拟机
Start-AzureRmVM -ResourceGroupName YangGangRS -Name YangGangVM
//停止虚拟机并解除分配
Stop-AzureRmVM -ResourceGroupName YangGangRS -Name "YangGangVM" -Force
//查看电源信息
Get-AzureRmVM ` -ResourceGroupName myResourceGroup ` -Name myVM ` -Status | Select @{n="Status"; e={$_.Statuses[1].Code}}
//获取所有镜像名称
$images = Find-AzureRMResource -ResourceType Microsoft.Compute/images
$images.name
//查看镜像信息:使用Azure自己的镜像时使用
Get-AzureRmVMImagePublisher -Location "ChinaEast"
Get-AzureRmVMImageOffer -Location "ChinaEast" -PublisherName "MicrosoftWindowsServer"
Get-AzureRmVMImageSku -Location "ChinaEast" -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer"
//删除镜像
$images.nameRemove-AzureRmImage -ImageName myOldImage -ResourceGroupName YangGangRS
//检索资源组
Get-AzureRmResourceGroup -ResourceGroupName 'YangGangAV'
//删除资源组
Remove-AzureRmResourceGroup -Name 'YangGangRS'
//创建存储账户
New-AzureRmStorageAccount -ResourceGroupName 'YangGangAV' -AccountName 'YangGang' -Type "Standard_LRS" -Location "ChinaEast"
//获取资源组的公网IP
Get-AzureRmPublicIpAddress -ResourceGroupName myResourceGroup | Select IpAddress
//远程连接
mstsc /v:<publicIpAddress>



















                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: