azure - 使用单个 PowerShell 命令在 Classic 和 ARM VM 上安装扩展

标签 azure azure-virtual-machine azure-powershell azure-oms

我有一个脚本,可以将 OMS 扩展安装到订阅中的所有 ARM VM。问题是我有仅包含 ARM VM 的订阅、仅包含经典 VM 的订阅以及同时包含两种类型 VM 的订阅。如何修改脚本以在所有条件下工作?脚本是:

#This script installs OMS Monitoring Agent to all VMs in the selected Subscription.
#Before running this script, the user must login to Azure account and select target subscription.
#Example:
#Login-AzureRmAccount
#Select-AzureRmSubscription 'SubscriptionName'

$WorkspaceID = 'Provide Workspace ID here'
$WorkspaceKey = 'Provide Workspace key here'
$VMs = Get-AzureRmVM

$VMs.where({$_.osprofile.windowsconfiguration}) | ForEach-Object {
    "Installing Microsoft.EnterpriseCloud.Monitoring.MicrosoftMonitoringAgent Extension: {0}" -f $_.id
    Set-AzureRmVMExtension -ResourceGroupName $_.ResourceGroupName -VMName $_.Name -Name omsAgent -Publisher 'Microsoft.EnterpriseCloud.Monitoring' `
    -ExtensionType 'MicrosoftMonitoringAgent' -AsJob -TypeHandlerVersion '1.0' -Location $_.Location -ForceRerun 'yesh' `
    -SettingString ( "{'workspaceId': '$WorkspaceID'}") `
    -ProtectedSettingString "{'workspaceKey': '$WorkspaceKey'}" | 
    Add-Member -Name VM -Value $_.Id -MemberType NoteProperty
}

最佳答案

由于您同时拥有经典虚拟机和 ARM VM,因此您拥有两种不同的部署模型,因此您使用的是两种不同的 PowerShell 模块。

换句话说,您需要为每个单独登录并使用单独的脚本。

在经典模型中,您需要运行以下 cmdlet 来登录和访问您的虚拟机:

Add-AzureAccount
Get-AzureVM | Set-AzureVMExtension `` 
    -Publisher 'Microsoft.EnterpriseCloud.Monitoring' `` 
    -ExtensionName 'MicrosoftMonitoringAgent' `` 
    -Version '1.*' `` 
    -PublicConfiguration "<workspace id>" `` 
    -PrivateConfiguration "<workspace key>" `` 

在搜索信息时我发现this脚本。它是一个使用两种部署模型从单个或多个订阅加载虚拟机的脚本。

关于azure - 使用单个 PowerShell 命令在 Classic 和 ARM VM 上安装扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49473480/

相关文章:

asp.net-mvc - 如何调试部署在 Azure 中的 ASPNET Core MVC 应用程序

azure - 如何删除没有VM实例(服务器)的 "Azure File Sync"服务器端点

powershell - Windows Azure Powershell 'you MUST specify a certificate'

azure - 无法理解 Azure 的角色 JSON

c# - 具有不同数据库、实体类型和 dbcontext 的通用存储库

azure - 无法从外部访问 Azure VM Scaleset IP 地址

linux - 重置 Azure 资源管理器 Ubuntu VM 密码

azure - 将现有 Azure VM(经典)添加到虚拟网络

Powershell显示存储帐户不存在

c# - 使用 C# 将 JSON 字符串直接添加到 Azure Blob 存储容器