VMware Cloud Community
bkieca1972
Contributor
Contributor

Powercli script question for exporting Data

I am trying to export data from my 6.5 vcenter with the embedded database. I keep getting an error. This works in my 6.0 vcenter with no issues. Script is below the error messages

Any assistance would be very much appreciated.

ERRORS

Cannot find an overload for
"Round" and the argument count: "2".

At
C:\Users\wkieca\Desktop\Docs\2019-Trueup\Scripts\Inventory-spvcenter11.ps1:40
char:5

+   
$VMInfo.DatastoreFree = [Math]::Round($ds.FreeSpaceGB,2)

+   
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    +
CategoryInfo          :
NotSpecified: (:) [], MethodException

    +
FullyQualifiedErrorId : MethodCountCouldNotFindBest

Method invocation failed because
[System.Object[]] does not contain a method named 'op_Subtraction'.

At
C:\Users\wkieca\Desktop\Docs\2019-Trueup\Scripts\Inventory-spvcenter11.ps1:41
char:5

+   
$VMInfo.DatastoreUsed = [Math]::Round($ds.CapacityGb - $ds.FreeSp ...

+   
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    +
CategoryInfo          :
InvalidOperation: (op_Subtraction:String) [], RuntimeException

    +
FullyQualifiedErrorId : MethodNotFound

Cannot find an overload for
"Round" and the argument count: "2".

At C:\Users\wkieca\Desktop\Docs\2019-Trueup\Scripts\Inventory-spvcenter11.ps1:40
char:5

+   
$VMInfo.DatastoreFree = [Math]::Round($ds.FreeSpaceGB,2)

+   
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    +
CategoryInfo          :
NotSpecified: (:) [], MethodException

    +
FullyQualifiedErrorId : MethodCountCouldNotFindBest

Method invocation failed because
[System.Object[]] does not contain a method named 'op_Subtraction'.

At
C:\Users\wkieca\Desktop\Docs\2019-Trueup\Scripts\Inventory-spvcenter11.ps1:41
char:5

+   
$VMInfo.DatastoreUsed = [Math]::Round($ds.CapacityGb - $ds.FreeSp ...

+   
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    +
CategoryInfo          :
InvalidOperation: (op_Subtraction:String) [], RuntimeException

    + FullyQualifiedErrorId
: MethodNotFound

-------------------------------------------------------------------------------------------------------

filter Get-FolderPath
{

    $_ | Get-View | % {

      
$row = "" | select Name, Path

      
$row.Name
= $_.Name

      
$current =
Get-View $_.Parent

      
# $path = $_.Name # Uncomment out this line if
you do want the VM Name to appear at the end of the path

      
$path = ""

      
do {

          
$parent =
$current

          
if($parent.Name -ne "vm"){$path
= $parent.Name + "\" +
$path}

          
$current =
Get-View $current.Parent

      
} while ($current.Parent -ne $null)

      
$row.Path
= $path

      
$row

    }

}

$VCServerName =
"spvcenter11.reyesholdings.com"

$VC =
Connect-VIServer $VCServerName
-user RH\x-bkieca
-password

$VMFolder =
"RFS"

$ExportFilePath =
"C:\Users\wkieca\Desktop\Docs\2019-Trueup\Export-spvcenter11.csv"

$Report =
@()

$VMs =
Get-VM

$Datastores =
Get-Datastore

$VMHosts =
Get-VMHost |
select Name, Parent

ForEach ($VM
in $VMs)
{

    $VMView = $VM | Get-View

    $ds = $Datastores | where {$_.ID -match ($vmview.Datastore
| Select -First 1).Value}

    $VMInfo = {} | Select VMName,Powerstate,OS,Folder,IPAddress,ToolsStatus,ToolsVersion,Host,Cluster,Datastore,DatastoreFree,DatastoreUsed,NumCPU,MemMb,DiskGb, DiskFree, DiskUsed, BusinessUnit

    $VMInfo.VMName = $vm.name

    $VMInfo.Powerstate
= $vm.Powerstate

    $VMInfo.OS = $vm.Guest.OSFullName

    $VMInfo.Folder = ($vm | Get-Folderpath).Path

    $VMInfo.IPAddress
= $vm.Guest.IPAddress[0]

    $VMInfo.ToolsStatus
= $VMView.Guest.ToolsStatus

    $VMInfo.ToolsVersion
= $VMView.Guest.ToolsVersion

    $VMInfo.Host = $vm.VMHost.Name

    $VMInfo.Cluster
= $vm.VMhost.Parent.Name

    $VMInfo.Datastore
= $ds.Name

    $VMInfo.DatastoreFree
= [Math]::Round($ds.FreeSpaceGB,2)

    $VMInfo.DatastoreUsed
= [Math]::Round($ds.CapacityGb - $ds.FreeSpaceGB,2)

    $VMInfo.NumCPU = $vm.NumCPU

    $VMInfo.MemMb = [Math]::Round(($vm.MemoryMB),2)

    $VMInfo.DiskGb = [Math]::Round((($vm.Guest.Disks | Measure-Object -Property
CapacityGB -Sum).Sum),2)

    $VMInfo.DiskFree
= [Math]::Round((($vm.Guest.Disks | Measure-Object -Property
FreeSpaceGB -Sum).Sum),2)

    $VMInfo.DiskUsed
= $VMInfo.DiskGb - $VMInfo.DiskFree

    $VMInfo.BusinessUnit
= $vm.CustomFields.Item("Business Unit")

    $Report += $VMInfo

}

$Report =
$Report |
Sort-Object VMName

IF ($Report
-ne "")
{

    $report | Export-Csv $ExportFilePath
-NoTypeInformation

}

$VC =
Disconnect-VIServer -Confirm:$False

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership

You seem to have more than 1 object in variable $ds


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

Reply
0 Kudos
bkieca1972
Contributor
Contributor

How can I fix this?

$ds = $Datastores | where {$_.ID -match ($vmview.Datastore | Select -First 1).Value}

Reply
0 Kudos
bkieca1972
Contributor
Contributor

The script runs in my 6.0 environment but not my 6.5

Reply
0 Kudos
LucD
Leadership
Leadership

You might try replacing line

$ds = $Datastores | where {$_.ID -match ($vmview.Datastore | Select -First 1).Value}

with

$ds = Get-View -Id $vm.ExtensionData.Datastore[0] | Get-VIObjectByVIView


Are you by any chance connected to multiple vCenters?

Check what is in $global:defaultviservers

There might be strange double match.
Can you run the following and share the result?

Get-VM | Select Name, @{N = 'Values'; E = {($_.ExtensionData.Datastore | select -First 1).Value}}

Get-Datastore | select Name, Id


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

Reply
0 Kudos
bkieca1972
Contributor
Contributor

We have 3 centers that are in the same that are in the in lynched mode. When I am running the script I am only connect to one at a time in powercli.

SDVCENTER11

SPVCENTER11

SRVCENTER11

The output is too large to post here. Are you looking for duplicate names?

I am trying to run the script with your change. I will let you know if it works.

Thanks,

Reply
0 Kudos
LucD
Leadership
Leadership

No, not the complete output.
I was trying to see if some of the datastores had Ids that would match multiple Values.


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

Reply
0 Kudos
bkieca1972
Contributor
Contributor

looks like your change allowed the script to run. I am checking the output now.

Thanks again

Reply
0 Kudos