Hello. First time on this forum. I am new to PowerShell and even newer to XML I am trying to change XML values from a CVS file.
My CSV would look like:
HOST_NAME,HOST_IP,HOST_MAC,HOST_FQDN
ComputerName,92.1.1.09,12-23-34-45-56-67,ComputerName.FQDN.DOMAIN
ComputerName2,92.2.2.12,00-00-00-00-00-00,ComputerName2.FQDN.DOMAIN
ComputerName3,92.3.3.13,11-00-11-00-11-00,ComputerName3.FQDN.DOMAIN
I’m a little confused on your requirements. You said
but it looks like you are editing an existing XML file not creating one. Since you didn’t provide an example of an existing XML file(s), I’ll work off of creating new ones. If anything it could just overwrite the existing ones with all new data accomplishing the same thing.
The main thing you are missing in your script is a loop since each row in the csv gets its own XML file, you’ll need to iterate row by row. Also you can simplify creation of the nodes by creating an inner loop to iterate over each column in the row, creating a new node for each.
Here’s my take. I used your example for testing.
I almost forgot one other thing is you can’t interpolate object properties like this in double-quote strings:
You’ll have to do it like this:
"$($e.HOST_FQDN)"
You can interpolate simple string variables without the subexpression, but once you use the property dereference operator “.” you’ll have to use it.
Is there just one existing XML file with each host info inside tags or are there separate existing XML files for each host? If separate ones, what is the naming convention? Also, do these tags exist already in the existing file and you just need to modify the values or are you creating new tags that don’t exist in the existing XML file?
1/2. There is only one existing XML file and I would need to fill the each Host info from CSV (row) , then save that as computer1.xml
3. The tags already exist in the XML file and I would just need to modify the values.
Since you are working off an existing large XML with all the hosts in it and need to extract data by hosts, you wouldn’t copy the existing file to a new one. What you need to do is find the appropriate parent element containing the hostname each iteration (assuming the hostname is correct in the existing xml), then copy that parent node to a new xml object, make updates based on csv and then save. Here’s my take: