我正在尝试设置目录中具有特定修改日期的许多文件夹的所有权,这需要我使用Foreach对象。我能够获取我想要的文件夹的所有名称,但是,我无法在文件夹上使用命令提示符命令"takeown"和"icacls"。
这是我尝试过的:
Get-ChildItem D:2008_Profile |
?{$_.LastWriteTime -lt (Get-Date).AddDays(-163)} |
ForEach-Object {
echo $_.Name
takeown /f $_.Name /R
icacls $_.Name /grant administrators:F
}
这是我得到的:
aan.V2
takeown : ERROR: The system cannot find the file specified.
At line:4 char:1
+ takeown /f $_.Name /R
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (ERROR: The syst...file specified.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Successfully processed 0 files; Failed processing 1 files
icacls : aan.V2: The system cannot find the file specified.
At line:5 char:1
+ icacls $_.Name /grant administrators:F
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (aan.V2: The sys...file
specified.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
注:AAN。V2 是文件夹名称
评论中回答的问题:
将
$_.Name
替换为$_.FullName
以完整引用事物 路径。或者将当前目录更改为引用的目录 对象。– 疯狂技术员