Powershell: Cannot convet System.object to System.Int32

I am trying to create a vlan on a hyperv virtual network adapter by passing the parameter as a variable which is imported from an xml file.  

I am using the following code but for some reason, it is giving an error



$xml = [XML](Get-Content .\Variables.xml)

$ClusterVLAN = $xml.variables.ClusterVLAN

Set-VMNetworkAdapterVlan –ManagementOS –VMNetworkAdapterName “Cluster” –Access –VlanId ($ClusterVLAN)



Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Nullable`1[System.Int32]".
At C:\windows\system32\4 Setup HyperV Network.ps1:71 char:9
+         Set-VMNetworkAdapterVlan –ManagementOS –VMNetworkAdapterName “Cluster” – ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : ConvertToFinalInvalidCastException





I am not a powershell expert so it might also be a minor issue.
LVL 1
adalliAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rob StoneCommented:
If you type $ClusterVLAN do you get a header as well as the value?

In some of my scripts I've had to put an additional variable to remove the header, so
$VlanId=$ClusterVLAN.objectname
0
adalliAuthor Commented:
Hi Stoner79,

if I run $ClusterVLAN I get the value that was set in the xml file but displayed twice.  This lead me the conclusion the setting $ClusterVLAN[0] as a working solution.  Nevertheless,  I do not understand the logics behind it.  

I will allocate 1/2 of the points to your post as it lead me to the solution but I would appreciate if you could guide me further to ensure that it is the right way to do it.
0
Rob StoneCommented:
Are you able to provide me a sample of your XML file?

By the looks of what you've put it's got multiple values so it's stored the values as an array.
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

adalliAuthor Commented:
Here it is


<Variables>

<!--# Variables-->


<!--#Generic Network Settings-->
<DnsServer1>10.20.31.2</DnsServer1>

<DnsServer2>0</DnsServer2>

<DefaultGateway>10.20.31.1</DefaultGateway>

<LiveMigarationVLAN>60</LiveMigarationVLAN>

<ClusterVLAN>120</ClusterVLAN>

<MgmtOSVLAN>1</MgmtOSVLAN>

<PublicInterface>1</PublicInterface> <!--# 0 = No, 1 = Y-->

<LiveMigarationVLAN>60</LiveMigarationVLAN>

<ClusterVLAN>120</ClusterVLAN>

<MgmtOSVLAN>1</MgmtOSVLAN>

<PublicVLAN>99</PublicVLAN>




<!--#Host 1 Specific Setting -->


<HyperVHostname1>SRV-FIN-HV01</HyperVHostname1>
<MgmtIP1>10.20.31.99</MgmtIP1>
<LiveMigrationIP1>192.168.60.11</LiveMigrationIP1>
<ClusterNicIP1>192.168.120.11</ClusterNicIP1>



<LoadBalAlgor>TransportPorts</LoadBalAlgor> <!--Options are HyperVPort, IPAddresses, MACAddresses, TransportPorts-->


</Variables>
0
Rob StoneCommented:
The reason you have two lines is because each object below has two entries in the XML file:
<LiveMigarationVLAN>60</LiveMigarationVLAN>
<ClusterVLAN>120</ClusterVLAN>
<MgmtOSVLAN>1</MgmtOSVLAN>

So that will create the variable as an array.

You can run it how you've done it, or you could run this command as well:
$xml.variables.ClusterVLAN |Select-object -first 1
0
adalliAuthor Commented:
Hi Stoner79,

what do you mean by two entries.  Isn't 120 the only entry for the $clusterVLAN?

Regards
0
Rob StoneCommented:
This is your example XML:
<Variables>

<!--# Variables-->


<!--#Generic Network Settings-->
<DnsServer1>10.20.31.2</DnsServer1>
<DnsServer2>0</DnsServer2>
<DefaultGateway>10.20.31.1</DefaultGateway>
<LiveMigarationVLAN>60</LiveMigarationVLAN>
<ClusterVLAN>120</ClusterVLAN>
<MgmtOSVLAN>1</MgmtOSVLAN>
<PublicInterface>1</PublicInterface> <!--# 0 = No, 1 = Y-->
<LiveMigarationVLAN>60</LiveMigarationVLAN>
<ClusterVLAN>120</ClusterVLAN>
<MgmtOSVLAN>1</MgmtOSVLAN>
<PublicVLAN>99</PublicVLAN>

<!--#Host 1 Specific Setting -->
<HyperVHostname1>SRV-FIN-HV01</HyperVHostname1>
<MgmtIP1>10.20.31.99</MgmtIP1>
<LiveMigrationIP1>192.168.60.11</LiveMigrationIP1>
<ClusterNicIP1>192.168.120.11</ClusterNicIP1>

<LoadBalAlgor>TransportPorts</LoadBalAlgor> <!--Options are HyperVPort, IPAddresses, MACAddresses, TransportPorts-->

</Variables> 

Open in new window


Lines 10,11,12 match that of 14,15,16. That's why you are having two values returned under the ClusterVLAN object.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
adalliAuthor Commented:
Such a silly mistake.  took me 3 hrs of work :-(
0
Rob StoneCommented:
No problem, sometimes we go blind looking at our own files.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VMware

From novice to tech pro — start learning today.