我正在使用下面的脚本检索VM的名称和路径,在 path 中获取我不想要的完整路径,我只需要在输出中Resources之后显示的路径
这是我的代码:
function Get-Path{
param($Object)
$path = $object.Name
$parent = Get-View $Object.ExtensionData.ResourcePool
while($parent){
$path = $parent.Name + "/" + $path
if($parent.Parent){
$parent = Get-View $parent.Parent
}
else{$parent = $null}
}
$path
}
Get-VM | Select Name,
@{N="Path";E={Get-Path -Object $_}}
输出:
名称:
AWServer 3.1
CCW%.1.1_DEMO
DEMO-EA
MUSE_DEMO
UV_CARDIO
UView-Web_Cardio-2015-v2
路径:
> ha-folder-root/ha-datacenter/host/tempcardio.centricity.info/Resources/vbnrt735w6%5c/sdfasd34564/AWServer
> 3.1 ha-folder-root/ha-datacenter/host/tempcardio.centricity.info/Resources/CCW5.1.1_DEMO
> ha-folder-root/ha-datacenter/host/tempcardio.centricity.info/Resources/4564/DEMO-EA
> ha-folder-root/ha-datacenter/host/tempcardio.centricity.info/Resources/vbnrt735w6%5c/MUSE_DEMO
> ha-folder-root/ha-datacenter/host/tempcardio.centricity.info/Resources/asd/UV_CARDIO
> ha-folder-root/ha-datacenter/host/tempcardio.centricity.info/Resources/UView-Web_Cardio_2015_v2
发布于 2015-06-22 11:40:03
如果包含“参考资料”的文本是多余的,那么使用一个简单的正则表达式,我们可以在函数输出之前替换它。
从$path
到$path -replace "^.*?Resources/"
因此,这将替换函数中类似的行(其中返回属性)。我们从字符串到包含"Resources/“的第一次出现的所有内容。
另一个方法是编辑get-path
的输出,它与函数的名称保持一致。
Get-VM | Select Name,
@{N="Path";E={(Get-Path -Object $_) -replace "^.*?Resources/"`}}
https://stackoverflow.com/questions/30978257
复制相似问题