0

I'm trying to create a PS script that will read multiple file directories from an XML file and then depending on the filetype and date modified > 10 Days I want to delete the files.

I'm not sure how I should be accessing the config value so that i can setup a foreach loop. At the minute I have this in my script

#Read XML Config File to get settings
[xml]$configfile = Get-Content "C:\Project\TestForXML.xml"

#Declare and set variables from Config values
$dirs = @($configfile.Settings.DirectoryName)
$scanSubDirectories = $configfile.Settings.ScanSubDirectories
$deleteAllFiles = $configfile.Settings.deleteAllFiles
$fileTypesToDelete = $configfile.Settings.FileTypesToDelete
$liveSiteLogs = $configfile.Settings.LiveSiteLogs
$fileExclusions = $configfile.Settings.FileExclusions
$retentionPeriod = $configfile.Settings.RetentionPeriod
$aicLogs = $configfile.Settings.AICLogs
$aicLogsRententionPeriod = $configfile.Settings.AICLogsRententionPeriod

$Now = Get-Date -format d 
$Days = "10" 
$LastWrite = $Now.AddDays(-$retentionPeriod) 
$aicLastWrite = $Now.AddDays(-$aicLogsRententionPeriod) 

$Logs = Get-Childitem $dirs -Include $fileTypesToDelete,$liveSiteLogs -Recurse | Where {$_.LastWriteTime -le "$LastWrite"} 


foreach ($Log in $Logs) 
    { 
    Remove-Item $Log.FullName 
    $Msg =Write-Output "Deleting File $Log" 
    $Msg | out-file "C:\Desktop\Output.txt" -append 
    }

My XML Config files looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<Settings>
<DirectoryName>"ServerA\C$\logs", "ServerB\C$\logfiles", 
               "ServerC\C$\logfiles", "ServerD\C$\logs"</DirectoryName>
<AICLogs>D:\DebugXML</AICLogs>
<AICLogsRententionPeriod>30</AICLogsRententionPeriod>
<ScanSubDirectories>True</ScanSubDirectories>
<DeleteAllFiles>False</DeleteAllFiles>
<FileTypesToDelete>.txt;.log;.mf</FileTypesToDelete>
<LiveSiteLogs>livesite.runtime.*</LiveSiteLogs> 
<FileExclusions>fdlogfile.log</FileExclusions>
<RetentionPeriod>13</RetentionPeriod>

2
  • Your XML config file is malformed, there is no closing </Settings> tag Commented Mar 10, 2015 at 15:36
  • Apologies my copy and paste didn't fully paste. the close </Settings> tag is there Commented Mar 10, 2015 at 16:47

1 Answer 1

1

I had to change out the following line.

$dirs = @($configfile.Settings.DirectoryName)

To something like this.

$dirs = @($configfile.Settings.DirectoryName.Split(",").Trim())

let me know if that gets you closer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.