如何在Powershell中使用Get-ChildItem排除带有数组的项目列表?(How to use Get-ChildItem with excluding a list of items with an array in Powershell?)

15 IT屋

I want to use an array for the exclusion:

Remove-Item -Path "$InstallDir\Lang\*" -Exclude "de.txt", "en.txt"

or

Get-ChildItem "$InstallDir\Lang"  -EXCLUDE "es.txt", "de.txt"| Remove-Item

These both work fine.

Whereas

Get-ChildItem "$InstallDir\Lang\*" -Exclude "$Language" | remove-item

does not work.

I tried several ways ( e.g. How to use Get-ChildItem with filter array in Powershell? or How to exclude list of items from Get-ChildItem result in powershell?) but I can´t find a solution. It seems as if $Language can't be interpreted by the command.

This is how $language is built:

[string]$Language = @('"de.txt"')
If ($PackageConfigFile.Language -notlike $Null) {
    foreach ($LIP in $PackageConfigFile.Language) {
        $Language += ",`n ""$LIP.txt"""
    }
}

$language has e.g. the following content

"de.txt",

"en.txt",

"es.txt"

Has anybody an idea?

解决方案

First:

Construct your $Language argument as an actual PowerShell array; what you attempted creates a multil-line string instead.

Creating that array should be as simple as:

$Language = $PackageConfigFile.Language -replace '$', '.txt'

-replace, with a collection (array) as the LHS, operates on each item in the collection individually; '$', '.txt' effectively appends .txt to the end ($) of each input item, and the resulting modified elements are collected in $Language as an array, of .NET type System.Object[].

Second:

Do not enclose $Language, your array argument, in "...".

Get-ChildItem $InstallDir\Lang\* -Exclude $Language | Remove-Item -WhatIf

If you enclose an array variable in "...", PowerShell converts it to a single string, composed of the array elements concatenated with the value of preference variable $OFS, which defaults to a space; e.g.:

PS> $arr = 'a', 'b', 'c'; "[$arr]"
[a b c]

For readers coming from a UNIX / bash background:

PowerShell variables do NOT need to be double-quoted when they're passed to other commands, whatever they may contain (spaces or other shell metacharacters).

When calling PowerShell-native functionality (cmdlets, functions, scripts), the variable's original type is preserved as-is (the ability to use the .NET Framework's rich type system is the core feature that exemplifies PowerShell's evolutionary quantum leap in the world of shells).

Only use "..." if you explicitly want to pass a string to the target command.

我要使用数组作为排除对象:



 删除项-路径" $ InstallDir\Lang\ *"-排除" de.txt"," en.txt" 




  Get-ChildItem" $ InstallDir\Lang" -EXCLUDE" es.txt"," de.txt" |删除项目


这些都可以正常工作。





  Get-ChildItem" $ InstallDir\Lang\ \ *"-排除" $ Language" | remove-item 


不起作用。



我尝试了几种方法(例如如何在Powershell中将Get-ChildItem与过滤器数组一起使用?如何在Powershell中从Get-ChildItem结果中排除项目列表?),但我找不到解决方案。
似乎该命令无法解释 $ Language 。



$ language 被构建:



  [string] $ Language = @( '" de.txt"')
If($ PackageConfigFile.Language -notlike $ Null){
foreach($ PackageConfigFile.Language中的$ LIP){
$ Language + =",` n"" $ LIP.txt"""
}
}


$语言有例如以下内容




" de.txt",



" en.txt ",



" es.txt"




有人有想法吗?


解决方案

首先:



< strong>将您的 $ Language 参数构造为实际的 PowerShell数组 ;尝试创建的是多行字符串



创建该数组应该很简单:



  $ Language = $ PackageConfigFile.Language -replace'$','.txt'


-替换,并带有一个集合(数组)作为LHS,分别对集合中的每个项目进行操作; '$','。txt'有效地附加 .txt 到每个输入项的结尾( $ ),然后将生成的修改后的元素收集为 $ Language 作为NET类型 System.Object [] 的 array



第二:



请勿将数组参数 $ Language 括在< code>" ..." 。



  Get-ChildItem $ InstallDir\Lang\ *-排除$ Language | Remove-Item -WhatIf 





如果封闭数组" ..." 中的变量,PowerShell将其转换为单个 string ,该字符串由与首选项变量<$的值连接的数组元素组成c $ c> $ OFS ,默认为 space ;例如:



  PS> $ arr =‘a’,‘b’,‘c’; " [$ arr]" 
[abc]





对于来自UNIX / bash 背景的读者



将PowerShell变量传递给其他命令(无论包含什么内容)时,都无需双引号(空格或其他shell元字符)。



调用PowerShell本机功能(cmdlet,函数,脚本)时,变量的原始类型将按原样保留(使用.NET Framework的丰富类型系统的能力是例证PowerShell在世界范围内的进化飞跃的核心功能)

如果您明确想传递 string 到目标命令。


本文地址:IT屋 » 如何在Powershell中使用Get-ChildItem排除带有数组的项目列表?