2

我正在尝试使用 Remove-Item cmdlet 从变量“$exclude_files”中删除存储为 InputObject 名称的文件列表。

由于它是一个列表,我正在遍历列表并获取 InputObject 文件名。

下面是代码:

$source_dir ="C:\Files"

#Files are in below variable $exclude_files

$exclude_files

InputObject      SideIndicator
-----------      -------------
Credentials.xml  =>
EC2_Ubuntu.pem   =>
file2.png        =>
file3.txt        =>
Terminals.config =>

# tried with giving path and without giving path
foreach ($i in $exclude_files){ Remove-Item -Path $source_dir  $i.InputObject }

但是,我收到以下错误:

删除项目:找不到路径“C:\Files\file3.txt”,因为它不存在。在 line:1 char:31 + foreach($i in $exclude_files){Remove-Item $i.InputObject} + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~ + CategoryInfo : ObjectNotFound: (C:\Files...file3.txt:String) [Remove-Item], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

4

2 回答 2

1

尝试以下操作:

$exclude_files.InputObject | Remove-Item -Path {Join-Path $source_dir $_ }

似乎它$i.InputObject被用作-Filter参数的输入(因为这是Path参数集中的第一个位置参数),这可能不是预期的想法。

于 2019-08-11T06:35:04.423 回答
1

我得到了解决方案..正如你所建议的,当使用 InputObject 遍历列表时,它的工作

foreach($i in $exclude_files)
            {
                $i.InputObject | Remove-Item -Path {Join-Path $source_dir $_}
    }

谢谢你

于 2019-08-11T07:53:39.363 回答