Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
输出文本文件以标识PowerShell中的已操纵文件_Powershell_Rename Item Cmdlet - Fatal编程技术网

输出文本文件以标识PowerShell中的已操纵文件

输出文本文件以标识PowerShell中的已操纵文件,powershell,rename-item-cmdlet,Powershell,Rename Item Cmdlet,我在这个问题上寻求帮助已经有几个小时了,但运气不好。“我的脚本”监视文件夹中超过12分钟并以“.tmp”结尾的文件。然后删除“.tmp”。这部分工作正常 我希望脚本输出一个文本文件,其中包含重命名文件的文件名。使用下面的脚本,文本文档为空 #Computer name from Description $ComputerName = Get-WmiObject -Class Win32_OperatingSystem | Select Description $ComputerName = "$

我在这个问题上寻求帮助已经有几个小时了,但运气不好。“我的脚本”监视文件夹中超过12分钟并以“.tmp”结尾的文件。然后删除“.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开关添加到重命名项的调用中,并将结果通过管道传输到输出文件:

你的问题是:

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开关添加到重命名项的调用中,并将结果通过管道传输到输出文件:


为了得到计算机名,有很多骗子。可以通过$env:ComputerName获得嘿,James,它实际上是在提取“描述”而不是“名称”。我很想知道一个更快的方法来实现这一点!为了得到计算机名,有很多骗子。可以通过$env:ComputerName获得嘿,James,它实际上是在提取“描述”而不是“名称”。我很想知道一个更快的方法来实现这一点!谢谢@Jeff Zeitlin!嘿@Jeff Zeitlin,当按照您的建议使用此代码时,报告会不断输出。我只想在文件名更改时获取文本文件。有什么建议吗?代码将始终更改满足Where对象条件的任何文件的文件名。不符合该子句的文件将不会通过该筛选器,因此不会重命名,因此不会显示在日志中。您需要确保Where对象只传递您实际需要的文件;我所做的只是纠正了给你留下空白日志的错误。再次感谢@Jeff Zeitlin。我们通过以下方式解决了这个问题:$childItem=get childItem-Path$c1Output | where对象{$$\.LastWriteTime-lt get date.AddMinutes-12}$childItem=$childItem.Name foreach$childItem{if$child-like*.tmp{rename item-newname{$$\.Name.substring0,$\.Name.length-4}-PassThru | out file\\ant\fc\Dept\Photo\SDF8\Studio\Tech\scripts\tmp\u fix\Reports\$computerName'''.\uuu'$time.txt}谢谢@Jeff Zeitlin!嘿@Jeff Zeitlin,当按照您的建议使用此代码时,报告会不断输出。我只想在文件名更改时获取文本文件。有什么建议吗?代码将始终更改满足Where对象条件的任何文件的文件名。不符合该子句的文件将不会通过该筛选器,因此不会重命名,因此不会显示在日志中。您需要确保Where对象只传递您实际需要的文件;我所做的只是纠正了给你留下空白日志的错误。再次感谢@Jeff Zeitlin。我们通过以下方式解决了这个问题:$childItem=get childItem-Path$c1Output | where对象{$$\.LastWriteTime-lt get date.AddMinutes-12}$childItem=$childItem.Name foreach$childItem{if$child-like*.tmp{rename item-newname{$$\.Name.substring0,$\.Name.length-4}-PassThru | out file\\ant\fc\Dept\Photo\SDF8\Studio\Tech\scripts\tmp\u fix\Reports\$computerName'''.\uuu'$time.txt}