Bar*_*son 7 powershell timestamp datecreated
对于 Windows 10 上的 PowerShell 命令,我遇到了一个奇怪的行为。
要更改文件的创建日期,我使用:
Get-ChildItem C:\testFile1.txt | % {$_.CreationTime = '01/11/2005 06:00:36'}
Run Code Online (Sandbox Code Playgroud)
要更改文件夹的创建日期,我使用:
Get-Item C:\testFolder1 | % {$_.CreationTime = '01/11/2004 22:13:36'}
Run Code Online (Sandbox Code Playgroud)
这 2 个命令在系统分区 C:\ 或桌面上定期运行良好。
如果文件夹存在于外部 USB 闪存驱动器上,情况就不同了。
(PS 更改文件时间戳的命令在外部 USB 闪存驱动器上仍然有效。)
假设我尝试更改外部 USB 闪存驱动器上文件夹(而非文件)的创建日期:
Get-Item U:\testFolder1 | % {$_.CreationTime = '01/11/2002 06:00:36'}
Run Code Online (Sandbox Code Playgroud)
我收到此错误消息:
异常设置“CreationTime”:“进程无法访问文件‘U:\testFolder1’,因为它正被另一个进程使用。” 在行:1 字符:31 + ... et-Item U:\testFolder1 | % {$_.CreationTime = '01/11/2002 06:00:36'} + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], SetValueInvocationException + FullQualifiedErrorId : ExceptionWhenSetting
进一步挖掘,我意识到该过程是 Windows 10 的文件资源管理器,它阻止我更改时间戳。只要我不打开文件资源管理器,我就可以使用 PowerShell 更改 U 盘上文件夹的创建日期。
有没有像 .Dispose() 这样的方法来阻止 Windows 10 的文件资源管理器锁定文件夹,而无需每次都关闭文件资源管理器?
小智 12
请尝试以下命令。
这也适用于外部驱动器。
(Get-Item "U:\testFolder1").LastWriteTime = '01/11/2002 06:00:36'
Run Code Online (Sandbox Code Playgroud)
我手头有一个函数,它使用 SysInternals 中的 Handle.exe 来查找哪个进程对文件具有锁定,然后尝试终止该进程对文件的锁定。
Function Close-LockedFile{
Param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][Object[]]$InputFile
)
Begin{
$HandleApp = 'C:\localbin\Handle.exe'
If(!(Test-Path $HandleApp)){Write-Host "Handle.exe not found at $HandleApp`nPlease download it from www.sysinternals.com and save it in the afore mentioned location.";break}
}
Process{
$HandleOut = Invoke-Expression ($HandleApp+' '+$InputFile.Fullname)
$Locks = $HandleOut |?{$_ -match "(.+?)\s+pid: (\d+?)\s+type: File\s+(\w+?): (.+)\s*$"}|%{
[PSCustomObject]@{
'AppName' = $Matches[1]
'PID' = $Matches[2]
'FileHandle' = $Matches[3]
'FilePath' = $Matches[4]
}
}
ForEach($Lock in $Locks){
Invoke-Expression ($HandleApp + " -p " + $Lock.PID + " -c " + $Lock.FileHandle + " -y") | Out-Null
}
$InputFile
}
}
Run Code Online (Sandbox Code Playgroud)
您应该能够将文件通过管道传输到该文件,它将解锁任何具有锁的文件,然后将文件对象传递到管道中。
归档时间: |
|
查看次数: |
24929 次 |
最近记录: |