获取 VM 名称和详细信息 按 HTTP(80) 端点过滤
Get VM Name and Details Filter By HTTP(80) Endpoint
我在同一个 Microsoft Azure 云服务上有几个虚拟机,我只想使用 Powershell 命令获取端点为 HTTP(端口 80TCP)的实例 (VM) 名称。
这就是我的小代码!
获取 AzureVM -ServiceName |获取 AzureEndpoint | Where-Object {$_.Port -eq 80}
谢谢!!!
调用 Get-AzureEndpoint
后,您不再拥有 VM 对象,而是拥有具有不同属性的 Endpoint 对象。然后过滤掉所需的端点,但现在您缺少 VM 的属性。
可能的解决方案:遍历每个 VM 的所有端点
$VMs = Get-AzureVM 'myservicename'
foreach ($VM in $VMs) {
# check if the current VM has an endpoint with port 80
$HttpEndpoint = Get-AzureEndpoint -VM $VM | where { $_.Port -eq 80 }
if ($HttpEndpoint) {
$VM.Name
}
}
这是假设端点不包含它们所属的 VM 的名称。否则你当然可以做
... where { $_.Port -eq 80 } | select InstanceName # or whatever the name of the property with the VM name is
跟进:
要查询其他端口:
where { $_.Port -eq 80 -or $_.Port -eq 443 }
或:
where { 80, 443 -contains $_.Port }
或使用 PowerShell 3 及更高版本:
where { $_.Port -in 80, 443 }
我在同一个 Microsoft Azure 云服务上有几个虚拟机,我只想使用 Powershell 命令获取端点为 HTTP(端口 80TCP)的实例 (VM) 名称。
这就是我的小代码! 获取 AzureVM -ServiceName |获取 AzureEndpoint | Where-Object {$_.Port -eq 80}
谢谢!!!
调用 Get-AzureEndpoint
后,您不再拥有 VM 对象,而是拥有具有不同属性的 Endpoint 对象。然后过滤掉所需的端点,但现在您缺少 VM 的属性。
可能的解决方案:遍历每个 VM 的所有端点
$VMs = Get-AzureVM 'myservicename'
foreach ($VM in $VMs) {
# check if the current VM has an endpoint with port 80
$HttpEndpoint = Get-AzureEndpoint -VM $VM | where { $_.Port -eq 80 }
if ($HttpEndpoint) {
$VM.Name
}
}
这是假设端点不包含它们所属的 VM 的名称。否则你当然可以做
... where { $_.Port -eq 80 } | select InstanceName # or whatever the name of the property with the VM name is
跟进: 要查询其他端口:
where { $_.Port -eq 80 -or $_.Port -eq 443 }
或:
where { 80, 443 -contains $_.Port }
或使用 PowerShell 3 及更高版本:
where { $_.Port -in 80, 443 }