VMware Cloud Community
CenturaBro
Enthusiast
Enthusiast
Jump to solution

AggregateException when using a txt file to exclude VM's from output

I'm trying to use a text file that includes an extensive list of VM's i want excluded from an output.  The problem is when i run the script and it goes to exclude a larger number of VM's it throws AggregateException

Here's a sample:

$GetAllVms = Get-VM

$VMXNET3OneOffException = get-content "C:\scripts\ComplianceScriptBundle\ExceptionFiles\OneOffExceptions\Vmxnet3OneOffException.txt"

$GetAllVMs | Get-NetworkAdapter | Where-object {($VMXNET3WildcardException -notcontains $_.parent)} | select parent

This is the error in PS

Also if i remove this text file OR have a smaller list it will work fine.

I know you can have exclusions in the script itself but I'm trying to make it easier to view the script and change exclusions.

Any ideas would be helpful.  thanks!

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I suspect you might have one or more VMs in your environment that are in a funny state.

Can you try the following and verify if that returns any error messages?

Get-VM | Select *

Get-VM | Get-NetworkAdapter | Select *


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

View solution in original post

Reply
0 Kudos
15 Replies
LucD
Leadership
Leadership
Jump to solution

Can't see the error via the link you included I'm afraid.

And I assume the 2 different variables, $VMXNET3OneOffException and $VMXNET3WildcardException, are a typo.

Try using a RegEx expression instead.

Something like this

$GetAllVms = Get-VM

$vmExclude = (Get-Content "C:\scripts\ComplianceScriptBundle\ExceptionFiles\OneOffExceptions\Vmxnet3OneOffException.txt") -join '|'

[RegEx]$VMXNET3OneOffException = "^($($vmExclude))$"

$GetAllVMs | Get-NetworkAdapter |

    Where-object {($_.parent.Name -notmatch $VMXNET3OneOffException)} |

    select parent

On another note, it's mostly better, performance-wise, to do your filtering earlier in the construct.

Something like this for example

$GetAllVMs |

    Where-object {($_.Name -notmatch $VMXNET3OneOffException)} |

    Get-NetworkAdapter |

    select parent


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

Reply
0 Kudos
CenturaBro
Enthusiast
Enthusiast
Jump to solution

So the wildcard was a typo Smiley Wink

Also I cant define the exceptions earlier in the construct due the variable being called and filtered through multiple text files throughout the script.

I'll try the regex way and update thanks!

Reply
0 Kudos
CenturaBro
Enthusiast
Enthusiast
Jump to solution

interesting development:

Ran the script on a box with:

Powershell version: 5.1.14409

PowerCLI version 6.5 R1 Build 4624819

it failed with aggregateException

Ran the script with:

Powershell version : 5.1.15063

PowerCLI Verison : 6.3 Release 1 build 3737840

works fine.

Time to figure out whats going on here.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Was that the script with the RegEx expression, or your original one?

And would you mind sharing a screenshot of the error?


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

Reply
0 Kudos
CenturaBro
Enthusiast
Enthusiast
Jump to solution

This happened in both expressions.  It looks like when i call any query with an exeption file, whether its in the script or in a text file, it will spit back the same error.

Ill probably try to upgrade or reinstall the powershell version when i have a minute to work on this again.

Gyazo - 0f62215e2949b9e5ed4d0e25a457e82b.png

hopefully this link works

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you already verify what is actually in $VMXNET3OneOffException

​Is that a single string?


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

Reply
0 Kudos
CenturaBro
Enthusiast
Enthusiast
Jump to solution

yes that variable is just piping an output of get-networkadapter, filtering results, and formatting it to be displayed in HTML.

this is the whole command I'm running:

$VMXNET3 = $GetAllVMs | Get-NetworkAdapter | Where-object {($_.Type -ne "Vmxnet3")`

-and ($_.Type -ne "EnhancedVmxnet")`

  -and ($VMXNET3Exception -notcontains $_.parent)`

   -and ($VMXNET3WildcardException -notcontains $_.parent)`

   } | select parent, type, @{N="vCenter";E={(($_.uid).split("@")[1]).split(":")[0]}} | ConvertTo-Html -Fragment -PreContent '<h3>Showing any NIC that is not VMXNET3</h3>'

I have a script VM running most of the scripts in our environment and that is the box that is having this powerCLI issue.  If ran from my desktop i don't get the issue.

this script is part of a larger script looking for issues in our environment.

EDIT:

to clarify: The initial command up top of this thread is me simplifying the actual section of the script posted above.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

If that is the value in there then the match will never work.

You have the results of a Select-Object in there, that is a complex object, not a string


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

Reply
0 Kudos
CenturaBro
Enthusiast
Enthusiast
Jump to solution

ok so... 

$GetAllVMs = get-vm

$VMXNET3Exception = Get-Content "C:\Scripts\_ComplianceScriptBundle\ExceptionFiles\OneOffExceptions\VMXNET3.txt"

$VMXNET3WildcardException = Get-Content "C:\Scripts\_ComplianceScriptBundle\ExceptionFiles\WildcardExceptions\Vmxnet3WildcardException.txt"

$VMXNET3 = $GetAllVMs | Get-NetworkAdapter | Where-object {($_.Type -ne "Vmxnet3")`

-and ($_.Type -ne "EnhancedVmxnet")`

  -and ($VMXNET3Exception -notcontains $_.parent)`

   -and ($VMXNET3WildcardException -notcontains $_.parent)`

   } | select parent, type, @{N="vCenter";E={(($_.uid).split("@")[1]).split(":")[0]}}

$VMXNET3

If i don't populate those text files and execute this script on my desktop I'll get an output like so:

Parent     Type    vCenter   

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

VM1      e1000    VCSA001

VM2      e1000    VCSA001

VM3      e1000    VCSA001

VM4      e1000    VCSA001

If populate either one of those text files with a VM the VM will not show up, just like I expected.

So the script to my knowledge works.   Just my script VM is throwing the aggregateException error when running the same script.

I'm quite new to scripting so I'm not saying this is the best way just the way I got working.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No problem.

To try and track that exception down, can you run the following?

The Catch block should provide us a bit more info on the error that is happening

$GetAllVMs = Get-VM

$VMXNET3Exception = Get-Content "C:\Scripts\_ComplianceScriptBundle\ExceptionFiles\OneOffExceptions\VMXNET3.txt"

$VMXNET3WildcardException = Get-Content "C:\Scripts\_ComplianceScriptBundle\ExceptionFiles\WildcardExceptions\Vmxnet3WildcardException.txt"

Try{

    $VMXNET3 = $GetAllVMs | Get-NetworkAdapter | Where-object {("Vmxnet3","EnhancedVmxnet" -notcontains $_.Type) -and

        ($VMXNET3Exception -notcontains $_.parent) -and

        ($VMXNET3WildcardException -notcontains $_.parent)} |

        select parent, type, @{N="vCenter";E={(($_.uid).split("@")[1]).split(":")[0]}}

    

    $VMXNET3

}

Catch

{

    $error[0] | Format-List -Property * -Force   

}


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

Reply
0 Kudos
CenturaBro
Enthusiast
Enthusiast
Jump to solution

alright... this is a lot but I'm going to copy the whole thing....

PSMessageDetails      :

Exception             : System.AggregateException: One or more errors occurred. ---> VMware.Vim.VimException ---> System.ServiceModel.FaultException`1[VimApi_65.InvalidProperty]

                       

                        Server stack trace:

                           at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)

                           at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)

                           at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)

                           at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

                       

                        Exception rethrown at [0]:

                           at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)

                           at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)

                           at VimApi_65.VimPortType.RetrieveProperties(RetrievePropertiesRequest request)

                           at VMware.Vim.PropertyCollector.RetrieveProperties(PropertyFilterSpec[] specSet)

                           --- End of inner exception stack trace ---

                           at VMware.Vim.PropertyCollector.RetrieveProperties(PropertyFilterSpec[] specSet)

                           at VMware.VimAutomation.ViCore.Impl.V1.Util.ApiData.ApiVirtualMachine.GetAllVMPropertiesWithPropertyCollector(VimClient client, String[] properties)

                           at System.Threading.Tasks.Task`1.InnerInvoke()

                           at System.Threading.Tasks.Task.Execute()

                           --- End of inner exception stack trace ---

                           at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)

                           at VMware.VimAutomation.ViCore.Impl.V1.Util.ApiData.ApiDataUtils.<>c__DisplayClass10_0.<GetLazyProperties>b__0()

                           at System.Lazy`1.CreateValue()

                        --- End of stack trace from previous location where exception was thrown ---

                           at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

                           at VMware.VimAutomation.ViCore.Impl.V1.Service.InventoryServiceImpl.<>c__DisplayClass48_0.<GetVMStream>b__7()

                           at VMware.VimAutomation.ViCore.Impl.V1.VM.UniversalVirtualMachineImpl.get_Name()

                           at System.Management.Automation.LanguagePrimitives.GetInvalidCastMessages(Object valueToConvert, Type resultType)

                           at System.Management.Automation.LanguagePrimitives.ConvertNoConversion(Object valueToConvert, Type resultType, Boolean recurse, PSObject originalValueToConvert,

                        IFormatProvider formatProvider, TypeTable backupTable)

                           at System.Management.Automation.LanguagePrimitives.ConversionData`1.Invoke(Object valueToConvert, Type resultType, Boolean recurse, PSObject originalValueToConvert,

                        IFormatProvider formatProvider, TypeTable backupTable)

                           at System.Management.Automation.LanguagePrimitives.ConvertTo(Object valueToConvert, Type resultType, Boolean recursion, IFormatProvider formatProvider, TypeTable

                        backupTypeTable)

                           at System.Management.Automation.ParameterBinderBase.CoerceTypeAsNeeded(CommandParameterInternal argument, String parameterName, Type toType,

                        ParameterCollectionTypeInformation collectionTypeInfo, Object currentValue)

                           at System.Management.Automation.ParameterBinderBase.EncodeCollection(CommandParameterInternal argument, String parameterName, ParameterCollectionTypeInformation

                        collectionTypeInformation, Type toType, Object currentValue, Boolean coerceElementTypeIfNeeded, Boolean& coercionRequired)

                           at System.Management.Automation.ParameterBinderBase.CoerceTypeAsNeeded(CommandParameterInternal argument, String parameterName, Type toType,

                        ParameterCollectionTypeInformation collectionTypeInfo, Object currentValue)

                           at System.Management.Automation.ParameterBinderBase.BindParameter(CommandParameterInternal parameter, CompiledCommandParameter parameterMetadata,

                        ParameterBindingFlags flags)

                           at System.Management.Automation.CmdletParameterBinderController.BindParameter(CommandParameterInternal argument, MergedCompiledCommandParameter parameter,

                        ParameterBindingFlags flags)

                           at System.Management.Automation.CmdletParameterBinderController.BindParameter(UInt32 parameterSets, CommandParameterInternal argument,

                        MergedCompiledCommandParameter parameter, ParameterBindingFlags flags)

                           at System.Management.Automation.CmdletParameterBinderController.BindPipelineParameter(Object parameterValue, MergedCompiledCommandParameter parameter,

                        ParameterBindingFlags flags)

                           at System.Management.Automation.CmdletParameterBinderController.BindValueFromPipeline(PSObject inputToOperateOn, MergedCompiledCommandParameter parameter,

                        ParameterBindingFlags flags)

                           at System.Management.Automation.CmdletParameterBinderController.BindUnboundParametersForBindingStateInParameterSet(PSObject inputToOperateOn, CurrentlyBinding

                        currentlyBinding, UInt32 validParameterSets)

                           at System.Management.Automation.CmdletParameterBinderController.BindUnboundParametersForBindingState(PSObject inputToOperateOn, CurrentlyBinding currentlyBinding,

                        UInt32 validParameterSets)

                           at System.Management.Automation.CmdletParameterBinderController.BindPipelineParametersPrivate(PSObject inputToOperateOn)

                           at System.Management.Automation.CmdletParameterBinderController.BindPipelineParameters(PSObject inputToOperateOn)

                           at System.Management.Automation.CommandProcessor.Read()

                           at System.Management.Automation.CommandProcessor.ProcessRecord()

                           at System.Management.Automation.CommandProcessorBase.DoExecute()

                           at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)

                           at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[]

                        pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext)

                           at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)

                           at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

                           at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

                        ---> (Inner Exception #0) VMware.Vim.VimException ---> System.ServiceModel.FaultException`1[VimApi_65.InvalidProperty]

                       

                        Server stack trace:

                           at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)

                           at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)

                           at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)

                           at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

                       

                        Exception rethrown at [0]:

                           at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)

                           at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)

                           at VimApi_65.VimPortType.RetrieveProperties(RetrievePropertiesRequest request)

                           at VMware.Vim.PropertyCollector.RetrieveProperties(PropertyFilterSpec[] specSet)

                           --- End of inner exception stack trace ---

                           at VMware.Vim.PropertyCollector.RetrieveProperties(PropertyFilterSpec[] specSet)

                           at VMware.VimAutomation.ViCore.Impl.V1.Util.ApiData.ApiVirtualMachine.GetAllVMPropertiesWithPropertyCollector(VimClient client, String[] properties)

                           at System.Threading.Tasks.Task`1.InnerInvoke()

                           at System.Threading.Tasks.Task.Execute()<---

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I suspect you might have one or more VMs in your environment that are in a funny state.

Can you try the following and verify if that returns any error messages?

Get-VM | Select *

Get-VM | Get-NetworkAdapter | Select *


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

Reply
0 Kudos
CenturaBro
Enthusiast
Enthusiast
Jump to solution

AH!

I think i found the culprit with your help.

The vCenters list I'm pulling from (we have way too many vcenters where I work) had one of the environments were letting die on the vine (4.1) and it should have been excluded from the list.  With powershell and powerCLI on a version so much higher than those objects i have a feeling they are causing the issues. I'm testing this theory ATM

Reply
0 Kudos
CenturaBro
Enthusiast
Enthusiast
Jump to solution

removed the 4.1 environment from the list and it worked great. 

Thanks for the help!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Happy it's working now.


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

Reply
0 Kudos