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
Izaz
  • 85
  • 1
  • 2
  • 10
  • 1
    Are you looking for another property or changing the output of `Get-Path`? – Matt Jun 22 '15 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. – Izaz Jun 22 '15 at 12:07

1 Answers1

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/"`}}
Matt
  • 45,022
  • 8
  • 78
  • 119
  • when i use the ' $parent = (Get-View $parent.Parent) -replace "^.*?Resources/" ' i was unable to get the path output – Izaz Jun 22 '15 at 12:08
  • @Izaz that is my fault. I edited the wrong property. See my edit. – Matt Jun 22 '15 at 12:09
  • 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 – Izaz Jun 22 '15 at 12:18
  • Which line did you replace. That should have worked? – Matt Jun 22 '15 at 12:24
  • This `$path = $parent.Name + "/" + $path` To This `$path = $path -replace "^.*?Resources/"` – Izaz Jun 22 '15 at 12:25
  • ok that explains it. wrong line to edit. I updated just in case of that anyway. Use the last thing I did in my update. – Matt Jun 22 '15 at 12:30
  • Matt, How to export the output to a excel file? – Izaz Jun 23 '15 at 08:10
  • CSV would just be a matter of piping to `import-csv` – Matt Jun 23 '15 at 10:20
  • i want to export my output to a excel file but the command which you mention it will just import the data in to the cmd prompt. What exactly i want is to do is that how the output has to be transfered to a excel file. – Izaz Jun 23 '15 at 11:11
  • @Izaz yeah.. it's too early for me. I meant to say `Export-CSV` is the easy way to export data to excel. Else look at this: http://stackoverflow.com/questions/17688468/how-to-export-a-csv-to-excel-using-powershell – Matt Jun 23 '15 at 11:30
  • Thanks Matt i just did like this and it works like a charm `Get-VM | Select Name, @{N="Path";E={(Get-Path -Object $_) -replace "^.*?Resources"}} | Export-Csv C:\izaz\test.csv` – Izaz Jun 23 '15 at 11:48
  • in my CSV file am getting a line on the top of my data as " **#TYPE Selected.VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl** " how to het rid of it – Izaz Jun 29 '15 at 08:23
  • @Matt- Thanks Matt it works like a charm `Get-VM | Select Name, @{N="Path";E={(Get-Path -Object $_) -replace "^.*?Resources"}} | Export-csv C:\izaz\test.xls -NoTypeInformation` – Izaz Jun 29 '15 at 11:24
  • How to remove the `%5c` from the Path Output and replace it with `/` Output: _ha-folder-root/ha-datacenter/host/tempcardio.centricity.info/Resources/vbnrt735w6%5c/sdfasd34564/AWServer_ – Izaz Jul 16 '15 at 10:38