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

Faye Müller 96 Reputation points
2022-03-12T16:46:05.017+00:00

Hi,

sometimes I run this script that I need to expand disks.
On Windows server 2019 I get this error:

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

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

+ FullyQualifiedErrorId : MethodNotFound

+ PSComputerName        : XXXX

$hdLetter="D:"
$server="XXXX"
$user = Read-Host "Enter domain\user name"
$pass = Read-Host -AsSecureString -Prompt "Enter password"
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, $pass
$guestLogicalDiskNumberPartition ="Disk #1, Partition #0"

Invoke-Command -ComputerName $server -Credential $cred -ScriptBlock {

$diskNumber = $($Using:guestLogicalDiskNumberPartition).Substring($($Using:guestLogicalDiskNumberPartition).IndexOf("Disk #")+6, $($Using:guestLogicalDiskNumberPartition).IndexOf(", Partition #")-6)

Update-Disk -Number $diskNumber

$timeOut = 0

# aspetta che la configurazione sia ok

do {

    $size = Get-PartitionSupportedSize -DriveLetter $Using:hdLetter

    $sizeGB = [math]::Round($size.SizeMax / 1024 / 1024 / 1024)

    Write-Host "." -NoNewline

    $timeOut += 300

    Start-Sleep -Milliseconds 300

} until ($sizeGB -eq $Using:capacityGB -or $timeOut -gt 20000)

Resize-Partition -DriveLetter $Using:hdLetter -Size $size.SizeMax

Write-Host "!"

Write-Host "Disco espanso a: $sizeGB GB" -ForegroundColor Green

}

can someone give me some help?

Big thanks!
Regards
Faye

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-03-12T20:19:46.093+00:00

    The only place I see that you use division is after the Get-SupportedPartitionSize cmdlet. The variable $size, however, shouldn't be an array.

    I've added error handling to your script, and replaced the multiple "Substring" operation with a regex (it makes what you're trying to do less difficult to figure out!). I've also redirected the error stream from the Invoke-Command so it will be returned. The results of the Invoke-Command are placed into the variable $result. You should verify at the completion of the Invoke-Command that no error objects are present in that variable.

    $hdLetter = "D:"
    $server = "XXXX"
    $user = Read-Host "Enter domain\user name"
    $pass = Read-Host -AsSecureString -Prompt "Enter password"
    $cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, $pass
    $guestLogicalDiskNumberPartition = "Disk #1, Partition #0"
    
    $result = Invoke-Command -ComputerName $server -Credential $cred -ScriptBlock {
        if ($diskNumber = $Using:guestLogicalDiskNumberPartition -match "^Disk #([0-9]+?),"){ 
            $disknumber = $matches[1]
            Try{
                Update-Disk -Number $diskNumber -ErrorAction Stop
            }
            Catch{
                Throw $_      # just return the error object
            }
            $timeOut = 0
            # aspetta che la configurazione sia ok
            do {
                Try{
                    $size = Get-PartitionSupportedSize -DriveLetter $Using:hdLetter -ErrorAction STOP
                    if ($size -is [array]){
                        Throw "Get-PartitionSupportedSize returned multiple results!"
                    }
                    if (($size.SizeMax) -is [array]){
                        Throw ("Logical Disk $($Using:hdletter) has multiple partitions!")
                    }
                    $sizeGB = [math]::Round($size.SizeMax / 1024 / 1024 / 1024)
                }
                Catch{
                    Throw $_  # just return the error object
                }
                Write-Host "." -NoNewline
                $timeOut += 300
                Start-Sleep -Milliseconds 300
            } until ($sizeGB -eq $Using:capacityGB -or $timeOut -gt 20000)      # NOTE: $capacityGB isn't defined!
            Try{
                Resize-Partition -DriveLetter $Using:hdLetter -Size $size.SizeMax -ErrorAction Stop
            }
            Catch{
                Throw $_  # just return the error object
            }
            Write-Host "!"
            Write-Host "Disco espanso a: $sizeGB GB" -ForegroundColor Green
            "Disco espanso a: $sizeGB GB"
        }
        else{
            Write-Host "Failed to find the disk number in $($Using:guestLogicalDiskNumberPartition)"
            "Failed to find the disk number in $($Using:guestLogicalDiskNumberPartition)"
        }
    } 2>&1      # redirect the error stream so Invoke-Command returns not just stuff that works!
    $result
    
    0 comments No comments

  2. Anonymous
    2022-03-14T09:42:59.707+00:00

    Hi,

    It seems the variable $size is an array. If what you posted is not the full script, the variable $hdLetter may be modified somewhere else. Please check if $Using:hdLetter contains multiple letters before the line $size = Get-PartitionSupportedSize -DriveLetter $Using:hdLetter.
    Also the colon ":" has to be removed from $hdLetter because the DriveLetter property of the MSFT_Partition class contains only letters.

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    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.