Thank you for your help. More context: i have a graphical interface with a combobox, the user choose a country. The script reads the xml, retrieve the attributes for this country, then display it to the next screen and do lot of others tasks (domain join, registration …) it’s why i need these variables to be created.
Thanks for the foreach-object that’s the trick , but i don’t know how to separate the “left” attribute and the right “value”
example :
$test | ForEach-Object {$_}
CountryDisplayName : China
Sorry, i think my request is not clearly understood. I would like to create as many variables as the number of properties in my object $Test.
More clear: $Test (which is a xmlview type) has multiple properties
$Test.CountryCode is CHN-W10
$Test.ADDomainNetbiosName : CHINA
…
i want to find all the existing properties for $Test (this is not a fixed number and properties names) , then create the variable $CountryCode, ADDomainNetbiosName , and store the value in this variable
Hmmm … and I think you didn’t understand what I was trying to explain. In 99.99% of the cases that’s not necessary.
I’ve got this.
Even if I’m still not convinced that this is needed I already said what you need - a loop and New-Variable
Please share some sanitized sample data of your XML file (formatted as code, please) and explain what you want to do with the variables you think you need to create.
Here is one part of the xml.
I need a function that will create the variables depending the CountryDisplaName.
Set-VariablesFromXML -xml $xml -country “China” > creates all variables in the China node ($CountryDisplaName, $CountryCode … with their repective values China, CHN-W10 …)
Each node can have more or less variables.
You still don’t say whatfor you need all these variables.
i thought my answer was clear, do you need more details ? > The script reads the xml, retrieve the attributes for this country, then display it to the next screen and do lot of others tasks (domain join, registration …) it’s why i need these variables to be created.
You may show your code. You don’t need to create variables to display the properties and / or the values. You can use the existing properties and values of the instance of the objects created by PowerShell from your input XML.
One issue with programmaticall created variables with arbitrary names is to enumerate them later on.
Finally i did it with your help, sorry if it is not the way to do, sure it can be done better, but it allow me to do what i need and that’s make me happy Sometimes, the best is the ennemy of the good
Here is the code:
$ComboBoxResult = $XmlLTI.countries.country | Where-Object CountryDisplayName -eq 'China'
$NodesList = $ComboBoxResult.psobject.BaseObject | Get-Member | Where-Object -Property MemberType -EQ -Value Property | Select-Object -Property Name
foreach ($Node in $NodesList) {
Write-host "create new variable $($Node.Name) with value $($ComboBoxResult.$($Node.Name)) "
New-Variable -Name $($Node.Name) -Value $($ComboBoxResult.$($Node.Name)) -Force
}
Yes Olaf you are totally right, but i’m using these values lot of time in my script, and it’s really easier to create it all altough there is few unused . Thank you again.
hmmm … I’m afraid we do not talk about the same topic. I was curious how you use variables you created programmatically when you’re actually not know in advance what their names are.
I’m not using those that i don t know in the main script but it was quicker to use the loop to create all the variables based on the xml than specifying it all one by one.
Hmmm … that’s actually what I meant. I’d consider this as unnecessary cumbersome complex. In this case you can simply use the XML variable reference … like this: $XmlLTI.countries.country