1

am using the below script to retrieve the NAME and PATH of the VM's and in the PATH am getting the full length path which i dont want it, i just need the path which is displaying after the Resources in the output

here is my code:

 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 $_}}

Output:

Name:

AWServer 3.1
CCW%.1.1_DEMO
DEMO-EA
MUSE_DEMO
UV_CARDIO
UView-Web_Cardio-2015-v2

PATH:

> 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
2
  • 1
    Are you looking for another property or changing the output of Get-Path? Commented Jun 22, 2015 at 11:36
  • In the Path: output i want to see only the data which is showing after the Resoures and i dont knwo how to do that. Commented Jun 22, 2015 at 12:07

1 Answer 1

1

If the text infront and including "Resources" is redundant then using a simple regex we can replacing it before it is output from your function.

From $path to $path -replace "^.*?Resources/"

So that would replace the similar line inside your function ( Where you return the property). We take everything from the string up to and including the first occurrence of "Resources/".

Another was would be to edit the output from get-path which keeps with the name of the function

Get-VM | Select Name,
    @{N="Path";E={(Get-Path -Object $_) -replace "^.*?Resources/"`}}
Sign up to request clarification or add additional context in comments.

14 Comments

when i use the ' $parent = (Get-View $parent.Parent) -replace "^.*?Resources/" ' i was unable to get the path output
@Izaz that is my fault. I edited the wrong property. See my edit.
when i use the $path -replace "^.*?Resources/" am getting the same NAME output in the PATH output. I want to get the output which is displaying after Resources in the PATH
Which line did you replace. That should have worked?
This $path = $parent.Name + "/" + $path To This $path = $path -replace "^.*?Resources/"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.