当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 浏览 PowerShell 命令

作者:精品下载站 日期:2024-12-14 07:39:13 浏览:13 分类:玩电脑

浏览 PowerShell 命令


每当我探索新的 PowerShell 模块或管理单元时,我做的第一件事就是列出该模块中找到的所有命令。

PS C:\scripts> get-command -module psworkflow

CommandType     Name                                               ModuleName
-----------     ----                                               ----------
Function        New-PSWorkflowSession                              PSWorkflow
Cmdlet          New-PSWorkflowExecutionOption                      PSWorkflow

您可以指定模块或管理单元。两者都使用 -module 参数。然而,对于较大的模块,我意识到我需要一种更好的方式来浏览命令。例如,我可能需要查看它们按动词或名词组织。该信息包含在 Get-Command 表达式中。我只需要要求它。这是更彻底的命令和结果。

get-command -mod Hyper-V | select Name,Noun,Verb,CommandType

[玩转系统] 浏览 PowerShell 命令

我包含了命令类型,因为某些模块可能包含 cmdlet 和函数。我可以修改这个表达式并插入排序命令。但这打字太多了。特别是如果我想排序和重新排序。相反,我会将此命令通过管道传输到 Out-Gridview。

get-command -mod Hyper-V | select Name,Noun,Verb,CommandType | Out-Gridview -Title "Hyper-V"

[玩转系统] 浏览 PowerShell 命令

现在我有了所有命令的可排序和可过滤视图。另外,它位于一个单独的窗口中,因此我可以返回提示并获取列出的命令的帮助。我什至决定构建一个快速而肮脏的函数。


Function Get-CommandGridView {

[cmdletbinding()]

Param([string]$Name)

 #get commands for the module or snapin
 $commands = Get-Command -Module $name

 #if module or snapin not found, Get-Command doesn't throw an exception
 #so I'll simply test if $commands contains anything
 if ($commands) {
    #include the module name because $Name could contain a wildcard
    $Commands | 
    Select-Object -Property Name,Noun,Verb,CommandType,ModuleName | 
    Out-Gridview -Title "$($Name.ToUpper()) Commands"
 }
 else {
    Write-Warning "Failed to find any commands for module or snapin $name"
 }

} #close Get-CommandGridView

Set-Alias -Name gcgv -Value Get-CommandGridView

由于模块或管理单元名称可以包含通配符,因此我将模块名称添加到输出中。现在,我有一个工具可以从一个模块或一组模块中获取所有命令,并且可以过滤和浏览我想要的所有命令,而无需在提示符下重新输入或修改命令。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯