繁体   English   中英

如何在PowerShell中将ResourceId变量转换为字符串?

[英]How do I convert a ResourceId variable into a string in PowerShell?

我在PowerShell中无法将ResourceID转换为String。

我有一个列表,其中包含来自Azure订阅的每个虚拟接口信息。

$virtualInterfaceArray = @(Get-AzureRmNetworkInterface)

然后,我将该列表的索引值放入变量中,因此无法获得所需的确切信息。 例如:

$hostVirtualInterfaceIndex = $virtualInterfaceArray[5]
$hostVirtualInterfaceVM = hostVirtualInterfaceIndex.VirtualMachine

如果我打印$ hostVirtualInterfaceVM,它将返回:

PS C:\Users\marcelo.salvatori> $hostVirtualInterfaceVM
Id                                                                                                                                 
--                                                                                                                                 
/subscriptions/301238dsf0sdfj/resourceGroups/rg-test/providers/Microsoft.Compute/virtualMachines/vmtest

我知道我不能使用.Split()方法,因为它不是String。 所以我尝试使用.ToString()方法阻止它:

PS C:\Users\marcelo.salvatori> $hostVirtualInterfaceVMString = hostVirtualInterfaceVM.ToString()

但是,当我打印变量时,它返回:

PS C:\Users\marcelo.salvatori> $hostVirtualInterfaceVMString
Microsoft.Azure.Commands.Network.Models.PSResourceId

我需要这次对话,因此可以使用.Split()方法。

如果要获取与该NIC关联的虚拟机的名称。 请尝试使用以下代码。

Get-AzureRmNetworkInterface |  Select-Object -Property Name, @{Name="VMName";Expression = {$_.VirtualMachine.Id.tostring().substring($_.VirtualMachine.Id.tostring().lastindexof('/')+1)}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM