下午好
最近,我一直在尝试修改此powershell脚本(来自“嘿,脚本专家!博客”),以修改单个文件而不是文件夹文件的文件时间戳(CreationTime,LastAccessTime和LastWriteTime)。但是,在进行修改后,我一直遇到问题。
原始脚本如下:
Set-FileTimeStamps function
Function Set-FileTimeStamps
{
Param (
[Parameter(mandatory=$true)]
[string[]]$path,
[datetime]$date = (Get-Date))
Get-ChildItem -Path $path |
ForEach-Object {
$_.CreationTime = $date
$_.LastAccessTime = $date
$_.LastWriteTime = $date
}
} #end function Set-FileTimeStamps
修改后的是这样的:
Function Set-FileTimeStamps
{
Param (
[Parameter(mandatory=$true)]
[string]$file,
[datetime]$date = (Get-Date))
$file.CreationTime = $date
$file.LastAccessTime = $date
$file.LastWriteTime = $date
} #end function Set-FileTimeStamps
当我尝试运行脚本时,会引发以下错误:
Property 'CreationTime' cannot be found on this object; make sure it exists and is settable.
At C:\Users\Anton\Documents\WindowsPowerShell\Modules\Set-FileTimeStamps\Set-FileTimeStamps.psm1:7 char:11
+ $file. <<<< CreationTime = $date
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
因此,我不清楚在修改原始脚本时我哪里失败了,如果有人能为我指明正确的方向,我将不胜感激。
提前致谢。
该类型[string]
没有CreationTime
,LastAccessTime
而LastWriteTime
属性仅仅是因为是一个文件名...它始终是一种[string]
类型。您需要将一种[system.io.fileinfo]
类型作为脚本的参数传递或强制转换为该类型:
Function Set-FileTimeStamps
{
Param (
[Parameter(mandatory=$true)]
[string]$file,
[datetime]$date = (Get-Date))
$file = resolve-path $file
([system.io.fileinfo]$file).CreationTime = $date
([system.io.fileinfo]$file).LastAccessTime = $date
([system.io.fileinfo]$file).LastWriteTime = $date
} #end function Set-FileTimeStamps
在原始脚本中,该cmdletGet-ChildItem -Path $path
返回[fileinfo]类型,这就是它起作用的原因。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句