将 XML 配置读入 PowerShell 数组
Read XML Config into PowerShell array
我正在尝试创建一个 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>
我不得不更改以下行。
$dirs = @($configfile.Settings.DirectoryName)
像这样。
$dirs = @($configfile.Settings.DirectoryName.Split(",").Trim())
让我知道这是否能让你更亲近。
我正在尝试创建一个 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>
我不得不更改以下行。
$dirs = @($configfile.Settings.DirectoryName)
像这样。
$dirs = @($configfile.Settings.DirectoryName.Split(",").Trim())
让我知道这是否能让你更亲近。