Powershell Select-对象如何获取值数组
Powershell Select-Object how to get array of values
我正在尝试删除几个文件夹的内容。
我有:
$Config = @{
InstallPath = 'C:\Program Files\App'
SubPaths = @('www\app1', 'www\app2', 'www\app3')
}
这里是获取内容的代码:
$Config.SubPaths | Select-Object { Join-Path $Config.InstallPath $_ } | Get-ChildItem
但它不起作用,因为 Get-ChildItem
收到如下对象:
@{ Join-Path $Config.InstallPath $_ =C:\Program Files\App\www\app1}
错误:
Get-ChildItem : Cannot find drive. A drive with the name '@{ Join-Path $Config.InstallPath $_ =C' does not exist.
At line:1 char:85
+ ... elect-Object { Join-Path $Config.InstallPath $_ } | Get-ChildItem
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (@{ Join-Path $C...stallPath $_ =D:String) [Get-ChildItem], DriveNotFoun
dException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
如何将 Select-Object
的结果转换为简单的字符串数组?或者任何其他方法可以使代码更好?
您得到的结果是因为您使用文字 属性 Join-Path $Config.InstallPath $_
创建了一个新的 object。相反...
$Config.SubPaths | ForEach-Object { Join-Path $Config.InstallPath $_ } | Get-ChildItem
您不是要 select 单个子路径的 属性 而是从每个子路径生成一个字符串。使用 Foreach-object
而不是遍历 collection 应该可以得到你正在寻找的结果。
虽然您可以使用计算的属性创建自定义 objects 和属性,但我认为这不是您想要的方向。但是要回答标题中的问题,您可以这样做:
$Config.SubPaths |
Select-Object @{Name="Path";Expression={Join-Path $Config.InstallPath $_}} |
Get-ChildItem
Get-ChildItem
应该绑定到 object 正在制作的新路径 属性
我正在尝试删除几个文件夹的内容。 我有:
$Config = @{
InstallPath = 'C:\Program Files\App'
SubPaths = @('www\app1', 'www\app2', 'www\app3')
}
这里是获取内容的代码:
$Config.SubPaths | Select-Object { Join-Path $Config.InstallPath $_ } | Get-ChildItem
但它不起作用,因为 Get-ChildItem
收到如下对象:
@{ Join-Path $Config.InstallPath $_ =C:\Program Files\App\www\app1}
错误:
Get-ChildItem : Cannot find drive. A drive with the name '@{ Join-Path $Config.InstallPath $_ =C' does not exist.
At line:1 char:85
+ ... elect-Object { Join-Path $Config.InstallPath $_ } | Get-ChildItem
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (@{ Join-Path $C...stallPath $_ =D:String) [Get-ChildItem], DriveNotFoun
dException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
如何将 Select-Object
的结果转换为简单的字符串数组?或者任何其他方法可以使代码更好?
您得到的结果是因为您使用文字 属性 Join-Path $Config.InstallPath $_
创建了一个新的 object。相反...
$Config.SubPaths | ForEach-Object { Join-Path $Config.InstallPath $_ } | Get-ChildItem
您不是要 select 单个子路径的 属性 而是从每个子路径生成一个字符串。使用 Foreach-object
而不是遍历 collection 应该可以得到你正在寻找的结果。
虽然您可以使用计算的属性创建自定义 objects 和属性,但我认为这不是您想要的方向。但是要回答标题中的问题,您可以这样做:
$Config.SubPaths |
Select-Object @{Name="Path";Expression={Join-Path $Config.InstallPath $_}} |
Get-ChildItem
Get-ChildItem
应该绑定到 object 正在制作的新路径 属性