Usage of Invoke-Command in PowerShell Connector Operations

If the use of the Invoke-Command cmdlet is required in a script, special consideration must be given for successful execution.

Objects provided by UNIFYBroker, such as the $components and $entities objects cannot be used within the Invoke-Command script block. If passed into the script block via argument list, attempting to access will result in a deserialization error.

The work-around to this limitation is to create a serializable object (array, hashtable, etc) containing required data which can be added to the argument list. Similarly, results can be packaged in a serializable object inside the script block and returned for use after the Invoke-Command has executed.

# Collect required entity data into serializable object.
$entityDataArray = @()
foreach ($entity in $components.InputEntities)
    $entityDataHash = @{}
    $entityDataHash['ID'] = $entity['ID'];
    $entityDataHash['Name'] = $entity['Name'];
    $entityDataArray += $entityDataHash
# Add to Invoke-Command argument list.
Invoke-Command -ComputerName localhost -ArgumentList $entityDataArray -ScriptBlock {
    # utilize $enityDataArray  
# Get data from source, return as serializable object.
$invokeResult = Invoke-Command -ComputerName localhost -ScriptBlock {
    $resultData = @{}
    $resultData['ID'] = # ID from data source
    $resultData['Name'] = # Name from data source
    return $resultData
# Utilize returned data.
$entity = $entities.Create()
$entity['ID'] = $invokeResult['ID']
$entity['Name'] = $invokeResult['Name']

Is this article helpful for you?