我正在寻找一种方法来使用 PowerShell 获取具有各自 Log Analytics 工作区(如果该 VM 连接到 Log Analytics 工作区)的 VM 列表。
第 1 列应该是 VM 名称,第 2 列应该是对应的 LA 工作区名称。
我坚持下去,任何建议都会有所帮助。
谢谢。
我正在寻找一种方法来使用 PowerShell 获取具有各自 Log Analytics 工作区(如果该 VM 连接到 Log Analytics 工作区)的 VM 列表。
第 1 列应该是 VM 名称,第 2 列应该是对应的 LA 工作区名称。
我坚持下去,任何建议都会有所帮助。
谢谢。
您可以尝试以下脚本:
#get all the Log Analytics Workspace
$all_workspace = Get-AzOperationalInsightsWorkspace
#here, I hard-code a vm name for testing purpose. If you have more VMs, you can modify the code below using loop.
$myvm_name = "vm name"
$myvm_resourceGroup= "resource group name of the vm"
#for windows vm, the value is fixed as below
$extension_name = "MicrosoftMonitoringAgent"
$myvm = Get-AzVMExtension -ResourceGroupName $myvm_resourceGroup -VMName $myvm_name -Name $extension_name
$workspace_id = ($myvm.PublicSettings | ConvertFrom-Json).workspaceId
#$workspace_id
foreach($w in $all_workspace)
{
if($w.CustomerId.Guid -eq $workspace_id)
{
#here, I just print out the vm name and the connected Log Analytics workspace name
Write-Output "the vm: $($myvm_name) writes log to Log Analytics workspace named: $($w.name)"
}
}
测试结果:
脚本注释:
1.在脚本中,出于测试目的,我硬编码了虚拟机名称/资源组名称。如果您有更多虚拟机,请相应地修改脚本。很容易做出改变。
2.如果VM都是windows vm,则继续使用参数的值$extension_name = "MicrosoftMonitoringAgent"。
3.我只是打印出 VM 名称和连接的 Log Analytics 工作区名称。您可以相应地修改脚本以满足您的要求。