I have a piece of code which was working OK then I moved a piece of the code to a function in order to simplify the overall structure. I’m now getting a conversion error.
This is the code:
Function Collate_Data_Into_HTML()
{
Param(
$fCustomerVPGs,
$fActiveAlerts
)
# Loop through the all customer VPGs and obtain status info.
Write-Host "$LineHeader Getting Status info for all VPGs." -ForegroundColor Yellow
$foutputArray_VPGInfo = @()
$fCustomerVPGs | % {
$fVPG = $_.VPGName
$fVMsCount = $_.VMsCount
$fActualRPO = $_.ActualRPO
$fLastTest = $_.LastTest
$fiStatus = $_.Status
$fiSubStatus = $_.SubStatus
[double]$fProvisionedStorageInMB = 0
$fProvisionedStorageInMB = $_.ProvisionedStorageInMB
$fProvisionedStorageInGB = $fProvisionedStorageInMB /1kb
$fProvisionedStorageInGB = [math]::Round($fProvisionedStorageInGB,1)
I get the following error at the line “$fProvisionedStorageInMB = $_.ProvisionedStorageInMB”.
Error: [ Cannot convert the “System.Object” value of type “System.Object” to type “System.Double”. ]
The variables have the following values:
$_.ProvisionedStorageInMB:
1438851
533470
5576093
41549797
42807
244205
1776643
512017
7202102
492230
803842
The variable type is:
$fProvisionedStorageInMB.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Double System.ValueType
$fCustomerVPGs:
OrganizationName : MyCompany Finance Australia
VpgName : MYC-PREPROD-PRD-MYCAUDITAPP01
VmsCount : 1
ActualRPO : 6
LastTest :
Status : 1
SubStatus : 0
ProvisionedStorageInMB : 81923
Zorg : @{rel=; href=https://10.0.0.58:9669/v1/zorgs/32e2b5c6-e666-ea46-916f-e35b8e1834b1; type=ZorgApi; identifier=32e2b5c6-e666-ea46-916f-e35b8e1834b1}
VpgIdentifier : f72f7896-ee99-0123-b3d4-eceb4c503f50
Variable Type:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
I changed the code to this:
$fProvisionedStorageInMB = $_.ProvisionedStorageInMB
$fProvisionedStorageInGB = $fProvisionedStorageInMB /1kb
And now I get the error “[ Method invocation failed because [System.Object] does not contain a method named ‘op_Division’. ]” at this line:
$fProvisionedStorageInGB = $fProvisionedStorageInMB /1kb
The variable type here is:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
I don’t understand what’s going on?