首页 > 解决方案 > 如何自定义 Get-Windowsfeature 路径的输出

问题描述

实际上,我正在寻找自定义我的输出Get-Windowsfeature | select path,但我被卡住了。

这是简单命令的输出:

Path                                                                         
----                                                                         
Services de fichiers et de stockage                                                                                                          
Services de fichiers et de stockage\Services de fichiers et iSCSI                                                                            
Services de fichiers et de stockage\Services de fichiers et iSCSI\Serveur de fichiers                                                        
Services de fichiers et de stockage\Services de fichiers et iSCSI\BranchCache pour fichiers réseau                                           
.........

我希望得到这样的结果(在分隔符“;”之前添加路径的功能名称)。

Path Needed
---- 

File and Storage Services [FileAndStorage-Services];;;;;;;;",

File and Storage Services [FileAndStorage-Services];File and iSCSI Services [File-Services];;;;;;;",

File and Storage Services [FileAndStorage-Services];File and iSCSI Services [File-Services];File Server [FS-FileServer];;;;;;",

File and Storage Services [FileAndStorage-Services];File and iSCSI Services [File-Services];BranchCache for Network Files [FS-BranchCache];;;;;;"

有人有想法吗?

在此先感谢您的帮助。

此致

标签: powershellwindows-server-2012-r2

解决方案


这是 forCalculatedProperties或 foreach 循环的完美案例。我认为 foreach 循环更容易阅读,所以让我们采用这种方法。

$features = get-windowsFeature
forEach ($feature in $features){
    $joinedPath = $feature.Name, $feature.Path -join ";"
    Write-Output $joinedPath
}

您可以将其缩短,但我认为每行都执行一个步骤,这很容易理解。

输出如下所示:

Windows-TIFF-IFilter;Windows TIFF IFilter
WinRM-IIS-Ext;WinRM IIS Extension
WINS;WINS Server
Wireless-Networking;Wireless LAN Service
WoW64-Support;WoW64 Support
XPS-Viewer;XPS Viewer

推荐阅读