I have been tasked at work to automate the monthly maintenance. Basically I need to edit the date values in an xmla file to the next month.
This is the relevant section in the XMLA file:
<Process xmlns="http://schemas.microsoft.com/analysisservices/2003/engine">
<Type>ProcessFull</Type>
<Object>
<DatabaseID>OLAPQA</DatabaseID>
<CubeID>Model</CubeID>
<MeasureGroupID>TRANSACTIONS</MeasureGroupID>
<PartitionID>TRANSACTIONS 201410</PartitionID>
</Object>
</Process>
<Process xmlns="http://schemas.microsoft.com/analysisservices/2003/engine">
<Type>ProcessFull</Type>
<Object>
<DatabaseID>OLAPQA</DatabaseID>
<CubeID>Model</CubeID>
<MeasureGroupID>TRANSACTIONS</MeasureGroupID>
<PartitionID>TRANSACTIONS 201411</PartitionID>
</Object>
</Process>
<Process xmlns="http://schemas.microsoft.com/analysisservices/2003/engine">
<Type>ProcessFull</Type>
<Object>
<DatabaseID>OLAPQA</DatabaseID>
<CubeID>Model</CubeID>
<MeasureGroupID>TRANSACTIONS</MeasureGroupID>
<PartitionID>TRANSACTIONS 201412</PartitionID>
</Object>
</Process>
I need to write a windows powershell script with the following logic:
- Get current date and verify if it’s the first day of the month
- If yes, then calculate the previous day (in order to calculate previous 2 year/month values )
- Search XMLA file to replace previous year/month combinations with new values
I have solved number 1 and 2 but I am having some trouble with number 3.
This is my code:
$con = Get-Content .\Process2Periods.xmla
$con | % { $_.Replace("((Get-Date).AddMonths(-2) | Get-Date -Format "yyyyMM")", ("((Get-Date).AddMonths(-1) | Get-Date -Format "yyyyMM")") } | Set-Content .\Process2Periods.xmla
How I got to this was running the following code which worked unlike the code above:
$con = Get-Content .\Process2Periods.xmla
$con | % { $_.Replace("201410", "201411") } | Set-Content .\Process2Periods.xmla
My question is how to make this dynamic instead of hard coding the values into the script. I will need this to change the three tags every month. The code works with the string values but how do I take the output of some code as the string value? Should I use variables?
Thanks in advance.
I am having trouble understanding the logic of what you are trying to do. I see that you are changing the month of every value up by one. So I am going into this assuming that you are going to up the month of every date by one month at the first of the month. I don't understand number 2 since I don't see you doing any manipulation of days.
Take the contents of the file and process them through the
foreach-Object
loop assuming its the first of the month. Output the normal lines to output but if you find a line that had thePartitionID
tags the we pull the 6 digit out. Next we write the manipulate string back to output with an updated date. The fun part is how the match and replace works which I will try to break down.matches[1]
contains the first capture group from the match. In our case 6 digits.[datetime]
object using parse exact.-f
parameter.I'm sure something is wrong and this was not your intention but like i mentioned before I dont really understand what you are doing. If you can make sense of what I should you I would think we can make this work with what you really want.