Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

PowerShell XML OperationStopped: No coercion operator is defined between types 'System.Object&' and 'System.Object'

While working with XML files using Powershell, I imported XML file and started exploring data inside XML. On the left hand side I have opened XML file in notepad. XML has nested objects. While accessing the nested data (Address) inside XML table I was receiving error OperationStopped: No coercion operator is defined between types 'System.Object&' and 'System.Object'. Although it is objects inside array, but it was not allowing to access the data.

Microsoft Powershell root encoding child subchild content system.Object system.private .corelib xmlwriter culuter neutral publickeytoken init no coerciaon operator.png

$xml = [xml](Get-Content C:\temp\TestXML.xml)

##====##

$xml
xml                            customer_list
---                            -------------
version="1.0" encoding="UTF-8" customer_list

##====##

$xml.customer_list
customer
--------
{customer, customer}

##====##

$xml.customer_list.customer
firstname lastname Address
--------- -------- -------
Sanjay    Angelo   Address
Micheal   Joshi    Address

##====##

$xml.customer_list.customer.Address
OverloadDefinitions
-------------------
System.Object&, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e Address(int )

##====##

$xml.customer_list.customer.Address(0)
OperationStopped: No coercion operator is defined between types 'System.Object&' and 'System.Object'.

Resolution: As xml has array of information. You can use foreach Loop fetch the information like below.

$xml.customer_list.customer.Count
2

##====##

$xml.customer_list.customer.ForEach('Address')
city     state
----     -----
Mumbai   Maharashtra
Banglore Karnataka

Microsoft Powershell foreach() xml get-content variable [xml] system.xml system.object nested xml error corelib.png

Useful Articles
Powershell Convert range of numbers into another list of numbers maintaining ratio
Powershell Active Directory: List complete hierarchy of upstream nested groups recursively of User
Powershell Active Directory: Show treeview of User or Group memberof hierarchy
Powershell Active Directory: Show treeview of nested Group members downstream hierarchy
Oneliner Microsoft Powershell Script Get members from a list of group from Active Directory in excel
Powershell Active Directory 1: Check, enable and disable child OU protect object from accidental deletion
Oneliner Powershell how to add or remove AD user members and objects in groups

Go Back

Comment

Blog Search

Page Views

11272911

Follow me on Blogarama