简体   繁体   English

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

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

I'm having trouble into converting a ResourceID into a String in PowerShell. 我在PowerShell中无法将ResourceID转换为String。

I have a list that have every virtual interface information from an Azure Subscription. 我有一个列表,其中包含来自Azure订阅的每个虚拟接口信息。

$virtualInterfaceArray = @(Get-AzureRmNetworkInterface)

Then I put the value of an Index of that list into a variable, so I cant get the exact information I want. 然后,我将该列表的索引值放入变量中,因此无法获得所需的确切信息。 For example: 例如:

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

If I print $hostVirtualInterfaceVM it returns: 如果我打印$ hostVirtualInterfaceVM,它将返回:

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

I know that I can't use the .Split() method, because it is not a String. 我知道我不能使用.Split()方法,因为它不是String。 So I try to contert it using the .ToString() method: 所以我尝试使用.ToString()方法阻止它:

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

But then, when I print the variable, it returns: 但是,当我打印变量时,它返回:

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

I need this conversation so I can use the .Split() method. 我需要这次对话,因此可以使用.Split()方法。

If you want to get the Virtual Machine's name associated with that NIC. 如果要获取与该NIC关联的虚拟机的名称。 Please have a try to use the following code. 请尝试使用以下代码。

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