powershell - Powershell仅排除文件

标签 powershell

在5.1版的Powershell中运行脚本时出现以下错误

在我用来删除180天以上文件的简单脚本下面,我尝试了一些解决方案,但未发现可能是什么错误。
(以下错误从葡萄牙语翻译为英语)

“文件外:无法验证'Encoding'参数中的参数。“\ file \ DeleteLogFile_21_01_2020.log”文件不属于该集合“未知;”串; unicode; bigendianunicode; utf8; utf7; utf32; ascii默认; “oem”由ValidateSet属性指定。请提供一个参数,该参数位于集合中,然后重试命令。在行上:1个字符:36 + $ Log | Out-File -Append D:\程序文件\ DeleteLogFile_ $ LogDate.log + ~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CategoryInfo:InvalidData:(:) [Out-File],ParameterBindingValidationException + FullyQualifiedErrorId: ParameterArgumentValidationError,Microsoft .PowerShell.Commands.OutFileCommand“

$logPath = "D:\Program files\NICE Systems\Logs"
$nDays = 10
$startTime = Get-Date
$Extensions = "*.*"
$LogDate = (Get-Date).ToString("dd_MM_yyyy")
$Files = Get-Childitem $LogPath -Include $Extensions -Recurse | Where-Object {$_.LastWriteTime -le (Get-Date).AddDays(-$nDays)}
foreach ($File in $Files) 
{
    if ($NULL -ne $File )
    {
        $Log = "`nData e Hora da Deleção: " + $startTime + ". O arquivo " + $File + " foi deletado do sistema."
        $Log | Out-File -Append D:\Program files\DeleteLogFile_$LogDate.log
        Remove-Item $File.FullName | out-null
    }
}

最佳答案

您的日志文件路径周围没有引号,因此它会在第一个空格处将其切断。您可以使用双引号,并且变量仍将扩展。单例不会。

$Log | Out-File -Append "D:\Program files\DeleteLogFile_$LogDate.log"

默认情况下,Windows也不允许您写入程序文件文件夹,这通常不是一个好习惯。

关于powershell - Powershell仅排除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59851094/

相关文章:

sql-server - 如何在PowerShell中解析模棱两可的类型名称?

Powershell:IOException try/catch 不起作用

variables - powershell测试尚未分配的变量

powershell - 随机数怪异powershell问题

powershell - 检查 HTTP 端口是否在 Windows 上打开

powershell - 无法在更新模块上找到存储库

powershell - 无法通过Powershell连接到live @ edu

windows - 如何创建 PowerShell 脚本以将文件复制到 USB 闪存驱动器?

shell - 使用powershell获取csv文件中的特定单元格值

PowerShell 中的日期时间解析