我正在寻找一种使用 PowerShell 获取具有各自 Log Analytics 工作区的虚拟机列表(如果该虚拟机已连接到 Log Analytics 工作区)的方法。
第 1 列应为虚拟机名称,第 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.如果虚拟机都是windows虚拟机,则继续使用参数$extension_name = "MicrosoftMonitoringAgent"
的值。
3.我只是打印出虚拟机名称和连接的日志分析工作区名称。您可以相应地修改脚本以满足您的要求。
关于azure - 有没有办法使用 Powershell 获取哪个 VM 连接到哪个 Log Analytics 工作区的详细信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60092292/