Conversion error when dividing an array variable by a fixed value

JDMils 51 Reputation points
2023-01-18T22:48:06.5866667+00:00

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?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,608 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. JDMils 51 Reputation points
    2023-01-18T23:16:29.69+00:00

    What I've noticed is that when the code is in the main body, this line:

    $fCustomerVPGs | % {

    ...cycles thru all the values one by one so the variable "$_.ProvisionedStorageInMB" only has one value at a time. However, since moving the code to a function, the variable now has multiple values.

    For example, I modified the code to this:

    	        $fProvisionedStorageInMB = $_.ProvisionedStorageInMB
    Write-Host "ProvisionedStorageInMB: $fProvisionedStorageInMB"
    	        $fProvisionedStorageInGB = $fProvisionedStorageInMB /1kb
    

    And the Write-Host shows the following when I step the code:

    ProvisionedStorageInMB: 1438851 533470 5576093 41549798 42807 244205 1776643 512017 7202102 492230 803842 2429852 822601 875526 4313810 2528873 1136643 256005 250882 534474 585804 5593917 153931 841686 1153669 493042 368641 1331202 5989379 3098225 3123205 3 68816 163846 665604 601433 184323 81921 1127350 409612 1991941 665604 419871 22478034 360320 615944 2528873 164439 1402881 81928 9125613 102411 153931 1274028 972443 3123205 547891 739349 144288 245766 184322 1535946 553291 2429852 368816 384004 144310 5989 379 266251 4313810 13615268 81933 368641 5570570 368823 220216 44688 410849 20481 4834306 1622284 655428 22737 430084 584328 5593917 124405 1331202 11545627 579888 1136643 410388 81923 81921 7419907 1440783 411650 12509 737402 565670 409610 163846 164329 94 412 1776643 525190 1422366 188420 133152 41142 379083 2705571 409612 601433 3098225 512017 865282 82036 123078 225284 250882 532572 43334 1482115 512020 3445735 1438851 370100 1353157 824323 822601 841686 81946 410343 215243 5576093 1127350 1991941 2048008 803842 60457 124011 487762 7202102 256005 405906 256002 875526 585804 455684 1372369 840477 470521 165052 81923

    It should only have ONE value!?

    0 comments No comments

  2. JDMils 51 Reputation points
    2023-01-19T01:17:51.9966667+00:00

    I figured it out, it was the way I was calling the function. I changed the call to the following and it works:

    $outputArray_VPGInfo = Collate_Data_Into_HTML -fCustomerVPGs2 $CustomerVPGs -fActiveAlerts $ActiveAlertsFound

    Looks like it was my misunderstanding on how to call a function.

    Thanks!

    0 comments No comments

  3. Rich Matheisen 47,581 Reputation points
    2023-01-19T03:30:10.39+00:00

    I'm guessing that you were invoking the function using positional parameters and separating the parameters using a comma (like this):

    $outputArray_VPGInfo = Collate_Data_Into_HTML $CustomerVPGs, $ActiveAlertsFound
    
    

    If that was the case, you were invoking the function and passing a single array consisting of two items. That's because the comma is a list (array) operator in PowerShell. If you want to use positional parameters instead of named parameters just use a space to separate the values:

    $outputArray_VPGInfo = Collate_Data_Into_HTML $CustomerVPGs $ActiveAlertsFound
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.