简体   繁体   中英

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.

I have a list that have every virtual interface information from an Azure Subscription.

$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:

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. So I try to contert it using the .ToString() method:

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.

If you want to get the Virtual Machine's name associated with that 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)}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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