VMware Cloud Community
mcsaki
Contributor
Contributor
Jump to solution

Orphaned VM's, getting the folder Size from path, VMX

Hi everyone,

I am running the latest script in this thread: (I think ver 4): Find unregistered virtual machines

I want to run some metrics on the servers the script runs on. Essentially I want to add the resources these orphaned VM's take up in a csv file. The script needs to note down the size of the folder where the VMX is found. I'm not sure how to do that with this script.

Any help is appreciated!

Thanks,

Mike

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Ok, try running with this updated version.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

32 Replies
LucD
Leadership
Leadership
Jump to solution

Since you have the Path and the VMXname, you could use the VimDatastore PSProvider to just list the content of the Vm's folder.

That returns the size of each file in there.

Something like this for example

$vmxPath = "[DS1] sample/sample.vmx"

$dsName,$Path = $vmxPath.Split(' ')

$dsName = $dsName.Trim('[]')

$ds = Get-Datastore -Name $dsName

$Path = $Path.Split('/')[0]

New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root '\' > $null

$size = Get-ChildItem -Path "DS:/$($Path)" | Measure-Object -Property Length -Sum | select -ExpandProperty Sum

Write-Host "Total size $($size) bytes"

Remove-PSDrive -Name DS


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

mcsaki
Contributor
Contributor
Jump to solution

Thanks a lot for the reply. I'm very new to PowerCLI. Does this method work dynamically? it looks like I have to manually specify the path:

$vmxPath = "[DS1] sample/sample.vmx"

How would I integrate this with the script from the other forum?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No need to do that.

Just run through the $report array, and add the FolderSize property.

In $newReport will be the entries with the FolderSize.

Just replace the last line of the script with this code.

$newReport = $report | %{

    $dsName,$Path = $_.vmxPath.Split(' ')

    $dsName = $dsName.Trim('[]')

    $ds = Get-Datastore -Name $dsName

    $Path = $Path.Split('/')[0]

   

    New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root '\' > $null

    $size = Get-ChildItem -Path "DS:/$($Path)" | Measure-Object -Property Length -Sum | select -ExpandProperty Sum

    $_ | Add-Member -Name FolderSize -Value $size -MemberType NoteProperty

    Remove-PSDrive -Name DS

    $_

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

mcsaki
Contributor
Contributor
Jump to solution

Hi LucD,

I added the lines of code right under the last line $report += $row however I got the following error:

Thanks for the help!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is not what I meant.

Replace this line

$report | Export-Csv "C:\Unregistered-VMX.csv" -NoTypeInformation

with this

$newReport = $report | %{

    $dsName,$Path = $_.vmxPath.Split(' ')

    $dsName = $dsName.Trim('[]')

    $ds = Get-Datastore -Name $dsName

    $Path = $Path.Split('/')[0]

   

    New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root '\' > $null

    $size = Get-ChildItem -Path "DS:/$($Path)" | Measure-Object -Property Length -Sum | select -ExpandProperty Sum

    $_ | Add-Member -Name FolderSize -Value $size -MemberType NoteProperty

    Remove-PSDrive -Name DS

    $_

}

$newReport | Export-Csv "C:\Unregistered-VMX.csv" -NoTypeInformation


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
mcsaki
Contributor
Contributor
Jump to solution

Hi LucD,

I replaced the last line, however now I am getting a different error:

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you attach script.ps1?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
mcsaki
Contributor
Contributor
Jump to solution

here you go

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Works perfectly for me. See the following result from a small test setup with one orphaned VMX.

At the end I just displayed the content of $newReport.

FolderSize.png

Do you get VMX files reported on the first part of the script?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
mcsaki
Contributor
Contributor
Jump to solution

No I didn't I kept getting that error every time it tried to write the new file. Does your script look the same as mine?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I used the script you attached


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
mcsaki
Contributor
Contributor
Jump to solution

I'm getting the VMX files shown, but not the rest. The new code doesn't run until the script is done and then it gives me errors for each vmx file found.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, which versions are you using?

Do a $PSVersionTable to get the PowerShell version, then do a Get-PowerCLIVersion to get the PowerCLI version.

Please share the output that is generated.

Another quick test you can do.

Check in the output if there is a Length property for each returned object.

$dsName = 'OneOfYourDatastores'

$ds = Get-Datastore -Name $dsName

New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root '\' > $null

Get-ChildItem -Path "DS:/" -Filter *.vmx -Recurse | Select *

Remove-PSDrive -Name DS


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
mcsaki
Contributor
Contributor
Jump to solution

Hi LucD,

I ran your last bit of code and I was able to get the length and all other fields from a specific datastore, so I guess something isn't working properly with the actual script.

Here is the versions and the output i got from the code

Reply
0 Kudos
mcsaki
Contributor
Contributor
Jump to solution

For some reason the attachment didn't work so here's a new upload

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

When you run the script, do you get the messages about Datacenter, CLuster, Datastore...?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
mcsaki
Contributor
Contributor
Jump to solution

The original script works fine, I get the datacenter, cluster and datastore and it exports to a csv file. I just ran the code you gave me 2 posts before and it works. here's what it shows:

Still cant get the original script to work with the length.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do all these have the ItemType as Container?

On a Container there is indeed no Length property.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

What type are the datastores the scripts are running against?

Are they VMFS, NFS...


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos