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

时间:2018-07-05 16:44:09

标签: string powershell azure

在PowerShell中将ResourceID转换为字符串时遇到麻烦。

我有一个列表,其中包含来自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()方法。

1 个答案:

答案 0 :(得分:0)

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

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button type="button">Click Me</button>