string - 如何在 PowerShell 中将 ResourceId 变量转换为字符串?
问题描述
我无法在 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() 方法,因为它不是字符串。所以我尝试使用 .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)}}
推荐阅读
- r - 错误:.rs.askForPassword(\"输入密码:\") 中的错误:找不到函数 \".rs.askForPassword\"\n"
- sql - 更新分区第一行的值。sqlanywhere17
- java - 如何从java(或groovy)中的postgres-db读取bytea格式的二进制数据并保存为jpeg文件?
- angularjs-ng-repeat - 如何在angularJs嵌套的ng-repeat中获取祖父母的索引
- ios - 如何将 iOS 应用程序指向测试服务器?
- svg - 在jupyter笔记本中调整图像大小?
- ios - 我正在使用 react native 并且在使用 react-native-firebase APNS 令牌时为空
- java - 在 java 上使用 JMX 运行时的 Bash 变量替换
- keras - 是否可以为经过训练的神经网络打印数据集中所选图像的序列?
- javascript - 在发布数据上使用 JSON.parse 时,Netlify 函数出现意外令牌错误