[英]How to retrieve the name and path of VM's through powercli
我正在使用以下腳本來檢索虛擬機的名稱和路徑 ,並且在PATH中獲取了我不想要的全長路徑,我只需要在輸出中的資源后面顯示的路徑
這是我的代碼:
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
如果infront文本和包含“ Resources”的文本多余,則可以使用簡單的正則表達式替換它,然后再從函數輸出。
從$path
到$path -replace "^.*?Resources/"
這樣就可以替換函數內部的相似行(返回屬性的位置)。 我們采取一切從字符串直至並包括第一次出現的“資源/”。
另一個可能是編輯get-path
的輸出,該輸出與函數名稱保持一致
Get-VM | Select Name,
@{N="Path";E={(Get-Path -Object $_) -replace "^.*?Resources/"`}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.