我尝试创建一个PS脚本,该脚本将从XML文件读取多个文件目录,然后根据文件类型和修改日期> 10天我想删除文件。
我不确定如何访问配置值以便我可以设置foreach循环。当我在我的剧本中有这个
#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
}
我的XML配置文件如下所示:
<?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>
答案 0 :(得分:1)
我不得不更改以下内容。
$dirs = @($configfile.Settings.DirectoryName)
这样的事情。
$dirs = @($configfile.Settings.DirectoryName.Split(",").Trim())
让我知道这是否让你更接近。