1

I have an XML file with the following structure:

<ReportParameters>
<Parameter id="mxrNZh/4cyLB/qExTbTjCg==" subreportfilter="True">
<Name>Absence Start Date</Name>
<Type>DateRange</Type>
<PromptText>Absence Start Date is between</PromptText>
<Values>
<DateRange>
<Start>2022-04-21T13:45:35</Start>
<End>2023-04-21T13:45:35</End>
</DateRange>
</Values>
</Parameter>
<Parameter id="mxrNZh/4cyLB/qExTbTjCg==" subreportfilter="True">
<Name>Absence Start Date</Name>
<Type>DateRange</Type>
<PromptText>Absence Start Date is between</PromptText>
<Values>
<DateRange>
<Start>2022-04-21T13:43:01</Start>
<End>2023-04-21T13:43:01</End>
</DateRange>
</Values>
</Parameter>
<Parameter id="mxrNZh/4cyLB/qExTbTjCg==" subreportfilter="True">
<Name>Absence Start Date</Name>
<Type>DateRange</Type>
<PromptText>Absence Start Date is between</PromptText>
<Values>
<DateRange>
<Start>2022-04-21T11:51:40</Start>
<End>2023-04-21T11:51:40</End>
</DateRange>
</Values>
</Parameter>
<Parameter id="mxrNZh/4cyLB/qExTbTjCg==" subreportfilter="True">
<Name>Absence Start Date</Name>
<Type>DateRange</Type>
<PromptText>Absence Start Date is between</PromptText>
<Values>
<DateRange>
<Start>2022-04-21T13:11:49</Start>
<End>2023-04-21T13:11:49</End>
</DateRange>
</Values>
</Parameter>
</ReportParameters>

When I try

$xmldocument = [xml](Get-Content "params.xml")
$xmldocument.reportparameters.parameter.values.DateRange.start = (Get-Date).AddYears(-2).ToString("yyyy-MM-ddTHH:mm:ss")

I get the error that says property start is not found. When I remove the three duplicate nodes at the end of the file so only a single one remains, it works.

In order for the program I'm running to work, it needs all 4 nodes. I'm just trying to change the date (to the same date) of each one. Is there any way to do this?

Shane
  • 11
  • 2

1 Answers1

1

Member-Access enumeration only works for getting values, not setting them. See about_Member-Access_Enumeration.

You can use the member-access operator to get the values of a property on items in a collection but you can't use it to set them directly. For more information, see about_Arrays.

You need to loop over each object to update their property values:

$xmldocument.reportparameters.parameter.values.DateRange |
    ForEach-Object { $_.start = (Get-Date).AddYears(-2).ToString('yyyy-MM-ddTHH:mm:ss') }
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37