Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 将选定的.System.String转换为System.String_Powershell_Powershell 4.0 - Fatal编程技术网

Powershell 将选定的.System.String转换为System.String

Powershell 将选定的.System.String转换为System.String,powershell,powershell-4.0,Powershell,Powershell 4.0,在Powershell中,我调用[io.path]::GetExtension(..),如下所示: 我得到以下输出: [io.path]::GetExtension($_) --------------------------- .dll .dll .dll .dll .dll

在Powershell中,我调用[io.path]::GetExtension(..),如下所示:

我得到以下输出:

[io.path]::GetExtension($_)
---------------------------






.dll                       
.dll                       
.dll                       
.dll                       
.dll                       
.dll                       
.dll                       
.dll                       
.dll                       
.dll
(注意,一些输入缺少扩展,因此其输出为空)

以及“获取成员”所产生的管道:

   TypeName: Selected.System.String

Name                        MemberType   Definition                                
----                        ----------   ----------                                
Equals                      Method       bool Equals(System.Object obj)            
GetHashCode                 Method       int GetHashCode()                         
GetType                     Method       type GetType()                            
ToString                    Method       string ToString()                         
[io.path]::GetExtension($_) NoteProperty System.String [io.path]::GetExtension($_)=
但是,我想要System.String,而不是Selected.System.String,因为我想对对象进行分组,而我(显然)无法对Selected.System.String进行分组,因为它没有实现IComparable

调用“.ToString()”不会引发错误,但不会将其转换为字符串


如何生成字符串或将输出转换为字符串?

要将输出打印为字符串,请尝试以下操作:

'C:\x.ini','C:\y.ini' | % { [io.path]::GetExtension($_) } 
.ini

.ini

TypeName:System.String


在这种情况下,%或Foreach对象将为每个输入运行命令,生成所需的输出。选择对象将实际创建一个包含输出的选择对象。打印此对象内容的方法有很多,但使用Foreach对象可能是最简单的方法。

如果您处理的是字符串,我建议只使用正则表达式:

$Paths -replace '.*?(\.\w+$)', '$1'

这将返回一个
[System.String[]
对象,其扩展名为
.exe

更改
选择对象{[io.path]::GetExtension($)}
选择对象-扩展属性扩展
@Bill\u Stewart“Property”Extension”。在这种情况下,$\在管道中是一个字符串。@JacobColvin,这似乎没有任何作用。没有任何产出;甚至无法通过管道输出来获取成员我假设输入对象为
IO.FileInfo
input对象(这不是不合理的,因为您没有指定管道的内容)。在这种情况下,您将用
ForEach对象{[IO.Path]::GetExtension($)}
替换
Select对象
。如果您想要完整的答案,您应该显示完整的代码。或者更好地提供一个有人否决你的建议。我不知道为什么,因为这是正确的答案。我感谢你的帮助@NexTerren从技术上讲,如果质量不好,或者是一个糟糕的解决方案,那么否决一个被接受的答案仍然是可以的,等等。。。然而,在这种情况下,我不知道为什么,特别是因为他们没有留下评论。哦,好吧
$Paths -replace '.*?(\.\w+$)', '$1'