Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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函数的description属性添加值?_Powershell - Fatal编程技术网

如何为powershell函数的description属性添加值?

如何为powershell函数的description属性添加值?,powershell,Powershell,我想填充我在$PROFILE中创建的Powershell函数的Description属性。我想向Description属性添加一个值,例如在personal PROFILE中创建的值。这可能吗 目前,如果我检查我的函数描述,我发现没有填充任何函数,例如: Get-Command -Type Function -Name get-* | Select-Object -Property Name, Description -First 10 Name

我想填充我在$PROFILE中创建的Powershell函数的Description属性。我想向Description属性添加一个值,例如在personal PROFILE中创建的值。这可能吗

目前,如果我检查我的函数描述,我发现没有填充任何函数,例如:

    Get-Command -Type Function -Name get-* | Select-Object -Property Name, Description -First 10

Name                             Description
----                             -----------
Get-AlertLog
Get-AllColors
Get-AppBackgroundTask
Get-AppvVirtualProcess
Get-AppxLastError
Get-AppxLog
Get-AssignedAccess
Get-AutologgerConfig
Get-BCClientConfiguration
Get-BCContentServerConfiguration
填充值将允许我搜索并快速查看我的函数是在哪里创建的,或者它们是做什么的,等等

多谢各位

+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~

注:

我不想通过Get Help检索信息,而是填充以下类型的某些属性:System.Management.Automation.FunctionInfo:

    Get-Command -Type Function -Name Get-AllColors | Get-Member

TypeName: System.Management.Automation.FunctionInfo

Name                MemberType     Definition
----                ----------     ----------
Equals              Method         bool Equals(System.Object obj)
GetHashCode         Method         int GetHashCode()
GetType             Method         type GetType()
ResolveParameter    Method         System.Management.Automation.ParameterMetadata ResolveParameter(string name)
ToString            Method         string ToString()
CmdletBinding       Property       bool CmdletBinding {get;}
CommandType         Property       System.Management.Automation.CommandTypes CommandType {get;}
DefaultParameterSet Property       string DefaultParameterSet {get;}
Definition          Property       string Definition {get;}
Description         Property       string Description {get;set;}
HelpFile            Property       string HelpFile {get;}
Module              Property       psmoduleinfo Module {get;}
ModuleName          Property       string ModuleName {get;}
Name                Property       string Name {get;}
Noun                Property       string Noun {get;}
Options             Property       System.Management.Automation.ScopedItemOptions Options {get;set;}
OutputType          Property       System.Collections.ObjectModel.ReadOnlyCollection[System.Management.Automation.PS...
Parameters          Property       System.Collections.Generic.Dictionary[string,System.Management.Automation.Paramet...
ParameterSets       Property       System.Collections.ObjectModel.ReadOnlyCollection[System.Management.Automation.Co...
RemotingCapability  Property       System.Management.Automation.RemotingCapability RemotingCapability {get;}
ScriptBlock         Property       scriptblock ScriptBlock {get;}
Source              Property       string Source {get;}
Verb                Property       string Verb {get;}
Version             Property       version Version {get;}
Visibility          Property       System.Management.Automation.SessionStateEntryVisibility Visibility {get;set;}
HelpUri             ScriptProperty System.Object HelpUri {get=$oldProgressPreference = $ProgressPreference...
另一个问题是,如果没有方法用值填充属性并通过选择对象-属性检索它们,为什么会有这样的类型


谢谢。

您应该使用基于注释的帮助,而不是使用功能。基于注释的帮助将允许您使用PowerShell的内置帮助系统。写起来并不难,你甚至不需要使用所有的章节

以下是示例:

基于注释的帮助的语法如下所示:

# .< help keyword>
# <help content>
阅读链接文章中的所有关键词及其描述。通过运行get-help about_comment_-based_-help,可以找到类似的信息。您可以使用它向函数中添加说明和许多其他信息:

例如:


到目前为止,这些回答并没有回答所问的实际问题,这也是一个很好的问题,尽管添加帮助是一个很好且有用的想法,无论是基于注释的帮助还是完整的XML帮助

问题在于,基于注释的帮助仅提供函数帮助的描述,而不提供运行Get命令时显示的属性

使用Get命令获取描述是一个非常有用且独立的想法

但是,只有函数的某些属性是可直接设置的,我们可以使用Get Member-membertype属性发现这一点,如原始问题所示,其输出包括部分答案:

Description         Property       string Description {get;set;}
成员属性“Description”可直接设置,如属性定义的结尾所示:{get;set;}:

答:因此我们可以通过直接赋值轻松设置函数的“实际定义”属性:*

我们可以很容易地找到所有可直接设置的属性

gcm mypshost | get-member | findstr "set;"
#  Typically only shows 3 properties unless the function was defined in a module
Description
Options
Visibility
或者更好的方法是设置源属性,该属性现在包含在Get命令的默认输出中,尽管直接赋值并不是那么简单

在具有适当模块清单的模块内创建函数时,允许设置直接定义的函数上通常为空的大多数/所有属性

源将是定义函数的模块,如果在模块中设置了版本,也将设置版本

遗憾的是,函数PSProvider不支持Set ItemProperty命令,因为它是由注册表提供程序和许多其他提供程序支持的。在版本2.0和5.1上,以下操作失败:

Set-ItemProperty function:mypshost -Name Description -Value 'only testing'
虽然它对文件使用类似的语法:

Set ItemProperty dir ic.ps1-Name LastWriteTime-Value get date


另一个例子是OutputType,它可以通过使用高级函数参数块的[CmdletBinding]属性中的一个选项来设置:

我认为HerbM想要的是快速提醒函数的功能,而不是替代帮助。两者都有很好的理由。例如,我有一个函数,它显示命令列表,然后执行这些命令。如果我进入:

范例

GCM列出了我在创建别名NA时包含的简短描述

如果GCM也会列出关于函数的描述,而不是空白,这将非常有用,如下所示:

# .< help keyword>
# <help content>
范例

我相信HerbM并不是在寻找gcm或任何其他工具来代替帮助,而是想要一个简单的函数描述。
如果描述只是“函数”未来版本的占位符,那么请让每个人都知道。

更接近但不是实际的请求:我正在寻找填充函数对象的方法,即函数本身、属性描述以及源。我们的想法是在使用Gcm函数时显示这一点。
Description         Property       string Description {get;set;}
   (Get-Command -Type Function -Name Get-AllColors).Description = 'Defined in $Profile'
# or
    (Get-Command -Type Function -Name Get-AllColors).Description = "Defined in the $Profile"
gcm mypshost | get-member | findstr "set;"
#  Typically only shows 3 properties unless the function was defined in a module
Description
Options
Visibility
Set-ItemProperty function:mypshost -Name Description -Value 'only testing'
gcm na|select -property name,description,DisplayName
Name Description                        DisplayName
---- -----------                        -----------
NA   RB Do a command with no arguments. NA -> Do_Noarg
gcm do_noarg|select -property name,description,Definition

Name     Description Definition
----     ----------- ----------
Do_Noarg              dr      ~\PS1\No_Arg\*.ps1 Invoke-Expression