COM object output saved to variable in PowerShell cannot be reused after first access

23 Views Asked by At

I have a question regarding COM object output saved to a variable in PowerShell:

$netShare = New-Object -ComObject HNetCfg.HNetShare

$connections = $netShare.EnumEveryConnection
$connectionsProps = $connections | ForEach-Object { $netShare.NetConnectionProps.Invoke($_) }

After accessing the variable $connections once, the variable content is cleared and you cannot reuse it later in a script. While easy to workaround, why is this happening and can you prevent it?

0

There are 0 best solutions below