使用PowerShell和MS Live凭据访问Azure VM
问题描述:
我想使用PowerShell更改Azure VM的大小。原因是:我使用机器进行开发。我需要A2尺寸,每天4小时。当我不开发时,虚拟机的所有者要求将机器的大小切换到A0。我可以使用我的MS Live帐户访问Azure订阅。现在我通过Azure Portal手动更改大小。我想用PowerShell自动执行此任务。该脚本应该将大小设置为A2,等待4小时并将其设置回A0。我只想在开始我的开发之前双击脚本并忘记问题。使用PowerShell和MS Live凭据访问Azure VM
我有一般程序的理解如下:
- 润进口AzurePublishSettings
- 运行SELECT-AzureSubscription
- 获取VM以Get-AzureVM
- 运行设置,AzureVMSize 对象
- Update-AzureVM
我无法获得发布配置文件,因为我没有拥有该机器。有没有使用MS Live帐户进行身份验证的方法?
答
跳过导入Azure发布并改为执行Add-AzureAccount。这将弹出用户界面进行身份验证与您的MS Live帐户。
一旦做到这一点,你可以使用Select-AzureSubscription
经典的部署,你需要这样的:
# authenticate if no account is already added to the powershell session
if (!(Get-AzureAccount)){ Add-AzureAccount }
# Get the vm object out of azure
$vm = get-azurevm | where name -eq "name of the vm"
# Now all you need is to is update the VM with its new size:
$vm | Set-AzureVMSize -InstanceSize Medium | Update-AzureVM
如果VM通过资源管理器(RM模型)部署
if (!(Get-AzureRMContext)){ Add-AzureRmAccount }
Select-AzureRmSubscription -SubscriptionId "{subscriptionId}"
$vm = Get-AzureRmVm | where name -eq "{vmName}"
$vm.HardwareProfile.vmSize = "Medium"
Update-AzureRmVM -VM $vm
btw。 Medium是API在API中调用的内容。
你能提供一个代码示例吗? –
无论如何,谢谢! –
没问题,当我今天晚些时候在我的笔记本电脑附近时,我会尝试并将一些代码放在一起... – larsro