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
Ok, try running with this updated version.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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?
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
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
Can you attach script.ps1?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
Do you get VMX files reported on the first part of the script?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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?
I used the script you attached
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
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
When you run the script, do you get the messages about Datacenter, CLuster, Datastore...?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
What type are the datastores the scripts are running against?
Are they VMFS, NFS...
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference