2

I have the folowing code:

$output = foreach ($comp in $maschines.name) {
    invoke-command -computer comp1 -ScriptBlock {
        try
        {
            get-vm –VMName $using:comp | Select-Object VMId | Get-VHD | Select-Object @{ label = "vm"; expression = {$using:comp} }, 
            path,
            VhdType, 
            VhdFormat, 
            @{label = "file(gb)"; expression = {($_.FileSize / 1GB) -as [int]} }, 
            @{label = "size(gb)"; expression = {($_.Size / 1GB) -as [int]} }
        }
        catch
        {
            Write-Host some error
        }
    }
}

and I do not get

some error

but:

> The operation failed because the file was not found.
>     + CategoryInfo          : ObjectNotFound: (Microsoft.Hyper...l.VMStorageTask:VMStorageTask) [Ge     t-VHD],
> VirtualizationOperationFailedException
>     + FullyQualifiedErrorId : ObjectNotFound,Microsoft.Vhd.PowerShell.GetVhdCommand
>     + PSComputerName        : comp1

How can I get

some error

in cach block?

andrej
  • 547
  • 1
  • 6
  • 21

2 Answers2

1

In order for a catch block to be triggered the exception needs to be terminating (PowerShell has both terminating and non-terminating errors).

To force an error from a cmdlet to be terminating, you can use the -ErrorAction parameter with Stop as the value:

$output = foreach ($comp in $maschines.name) {
    invoke-command -computer comp1 -ScriptBlock {
        try
        {
            get-vm –VMName $using:comp -ErrorAction Stop | Select-Object VMId | Get-VHD | Select-Object @{ label = "vm"; expression = {$using:comp} }, 
            path,
            VhdType, 
            VhdFormat, 
            @{label = "file(gb)"; expression = {($_.FileSize / 1GB) -as [int]} }, 
            @{label = "size(gb)"; expression = {($_.Size / 1GB) -as [int]} }
        }
        catch
        {
            Write-Host some error
        }
    }
}
Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
  • 1
    I changed to `...get-vm –VMName $using:comp | Select-Object VMId | Get-VHD -ErrorAction Stop | Select-Object @{ label...` and it works. Thanks! – andrej Mar 19 '19 at 11:39
  • Ah yes, hadn't noticed that cmdlet was being used too. You could also consider doing `$ErrorActionPreference = 'Stop'` at the start of the script to force terminating errors for all cmdlets. – Mark Wragg Mar 19 '19 at 11:40
  • I changed `Write-Host $_.Exception.Message ` in catch and the error says `The operation failed because the file was not found.` How can I see what file? – andrej Mar 19 '19 at 11:49
  • Try doing just `Write-Host $_` to see if the full error record has the information you need. – Mark Wragg Mar 19 '19 at 11:50
  • no, nothing else but `The operation failed because the file was not found` – andrej Mar 19 '19 at 11:54
  • It might be that its not something the `Get-VHD` cmdlet returns in its error record unfortunately. – Mark Wragg Mar 19 '19 at 11:56
1

Add -ErrorAction Stop to Get-Vm to make it terminating.

You can read more about terminating ana non-terminating cmdlet in powershell here :

https://devblogs.microsoft.com/scripting/understanding-non-terminating-errors-in-powershell/

https://devblogs.microsoft.com/powershell/erroraction-and-errorvariable/

Fourat
  • 2,366
  • 4
  • 38
  • 53