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

来源:互联网 发布:linux 查看用户 编辑:程序博客网 时间:2024/06/05 07:49

公司需要对业务做托管,使用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 groupNew-AzureRmResourceGroup -Name $resourceGroup -Location $location# Create a high availible setNew-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 -AvailabilitySetId $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 machineNew-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.IdNew-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" //获取资源组的公网IPGet-AzureRmPublicIpAddress -ResourceGroupName myResourceGroup | Select IpAddress //远程连接mstsc /v:<publicIpAddress> 





 
阅读全文
1 0
原创粉丝点击