有没有办法使用 Powershell 获取哪个 VM 连接到哪个 Log Analytics 工作区的详细信息?
Is there a way to get details of which VM connected to which Log Analytics Workspace using Powershell?
我正在寻找一种使用 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.In 脚本,出于测试目的,我硬编码了虚拟机名称/资源组名称。如果您有更多的虚拟机,请相应地修改脚本。改变很容易。
2.If VM都是windows vm,那就继续使用参数$extension_name = "MicrosoftMonitoringAgent"
.
的值
3.I 只需打印出 VM 名称和连接的 Log analytics 工作区名称。您可以相应地修改脚本以满足您的要求。
我正在寻找一种使用 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.In 脚本,出于测试目的,我硬编码了虚拟机名称/资源组名称。如果您有更多的虚拟机,请相应地修改脚本。改变很容易。
2.If VM都是windows vm,那就继续使用参数$extension_name = "MicrosoftMonitoringAgent"
.
3.I 只需打印出 VM 名称和连接的 Log analytics 工作区名称。您可以相应地修改脚本以满足您的要求。