簡體   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