powershell - 输出文本文件以识别PowerShell中的操作文件

标签 powershell rename-item-cmdlet

我一直在这个问题上寻求帮助,但运气不佳。我的脚本监视文件夹中是否存在以'.tmp'结尾的超过12分钟的文件。然后删除“.tmp”。这部分工作正常。

我希望脚本输出一个包含重命名文件的文件名的文本文件。在下面的脚本中,文本文档为空。

#Computer name from Description
$ComputerName = Get-WmiObject -Class Win32_OperatingSystem | Select Description
$ComputerName = "$ComputerName".split("}")
$ComputerName = "$ComputerName".split("=") | Select-Object -Last 1
$ComputerName = "$ComputerName".split(" ") | Select-Object -First 1

#$ComputerName = $env:computername
Write-Host "This is the computer name: $ComputerName"

$c1Output = 'E:\' + $ComputerName + '\Capture One Session\Output\Market\'

get-childitem -Path "$c1Output" -Filter *.tmp |
    where-object {$_.LastWriteTime -lt (get-date).AddMinutes(-12)} | 
    rename-item -newname { $_.name.substring(0,$_.name.length-4) }
    out-file \\server\Report.txt

这应该很容易弄清楚,但是我还是PowerShell的新手。

谢谢!

最佳答案

您的问题在这里:

get-childitem -Path "$c1Output" -Filter *.tmp |
    where-object {$_.LastWriteTime -lt (get-date).AddMinutes(-12)} | 
    rename-item -newname { $_.name.substring(0,$_.name.length-4) }
    out-file \\server\Report.txt

您需要进行两项更改:将-Passthru开关添加到对Rename-Item的调用中,并将结果通过管道传递到Out-File:
get-childitem -Path "$c1Output" -Filter *.tmp |
    where-object {$_.LastWriteTime -lt (get-date).AddMinutes(-12)} | 
    rename-item -newname { $_.name.substring(0,$_.name.length-4) } -Passthru |
    out-file \\server\Report.txt

关于powershell - 输出文本文件以识别PowerShell中的操作文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45576165/

相关文章:

powershell - 重命名项目 : Cannot rename because item at . .. 不存在

powershell - WindowsPowerShell 重命名项目

powershell 重命名项替换为变量递增

powershell - 如何从模块中检索导出的变量?

powershell - Move-Item的通配符问题

从函数返回的 PowerShell 行为

Powershell Rename-Item 一次性替换多个命名参数

powershell - 用不同的名称重命名多个文件

windows - 如何正确循环遍历 Ansible 中 block 内任务的文件?

powershell - 如何使用 PowerShell 更新 BuildNumber