【问题标题】:How do you correctly document Powershell functions?您如何正确记录 Powershell 函数?
【发布时间】:2020-10-29 14:19:16
【问题描述】:

我是 Powershell 的新手,尽我所能发现它在 Python 中没有像 PEP8/PEP484 这样的东西。我找到了来自 Microsoft 的 this document 和来自 Posh Code 的 this 第三方指南。我写了以下函数:

function Invoke-Authenticate {

  [CmdletBinding()]
  param (
      [Parameter(Mandatory)]
      [string] 
      # IP address of the OME server
      $Ip,

      [Parameter(Mandatory)]
      # Credentials for the OME server
      [pscredential] $Credentials
  )

  $SessionUrl  = "https://$($IpAddress)/api/SessionService/Sessions"
  $Type        = "application/json"
  $UserDetails = @{"UserName"=$Credentials.username;"Password"=$Credentials.GetNetworkCredential().password;
                   "SessionType"="API"} | ConvertTo-Json
  $Headers     = @{}

  try {
    $SessResponse = Invoke-WebRequest -Uri $SessionUrl -Method Post -Body $UserDetails -ContentType $Type `
                                      -SkipCertificateCheck
    if ($SessResponse.StatusCode -eq 200 -or $SessResponse.StatusCode -eq 201) {
      # Successfully created a session - extract the auth token from the response
      # header and update our headers for subsequent requests
      $Headers."X-Auth-Token" = $SessResponse.Headers["X-Auth-Token"]
    } else {
      Write-Error "OME did not return a 200 or 201 status code. Received $($SessResponse.StatusCode).
      We are unsure why this happened."
    }
  }
  catch [Microsoft.PowerShell.Commands.HttpResponseException] {
    Write-Error "There was a problem authenticating with OME. Are you sure you have the right username and password?"
    return $null
  }
  catch [System.Net.Http.HttpRequestException] {
    Write-Error "There was a problem connecting to OME. Are you sure you have the right IP and that it is available?"
    return $null
  }

  return $Headers

  <#
    .SYNOPSIS
    Authenticates with OME and creates a session

    .DESCRIPTION
    Authenticates with OME and creates a session

    .PARAMETER Ip
    IP address of the OME server

    .PARAMETER Credentials
    Credentials for the OME server

    .INPUTS
    None. You cannot pipe objects to Invoke-Authenticate.

    .OUTPUTS
    hashtable. The Invoke-Authenticate function returns a hashtable with the headers resulting from authentication 
               against the OME server

  #>
}

据我所知,根据正确的指南,在我看来,最后的描述看起来很愚蠢。是否有我为 Powershell 缺少的设置编码样式指南,或者这是否正确?您是否应该只删除不适用于某个功能的字段?例如我没有.INPUTS。我应该完全删除它吗?

【问题讨论】:

    标签: powershell code-documentation


    【解决方案1】:

    它被称为“基于评论的帮助”(about_Comment_Based_Help)

    您有 3 个选项来放置文档:

    • 在函数体的开头。

    • 在函数体的末尾。

    • 在 function 关键字之前。不能有多个空白 函数帮助的最后一行和函数之间的行 关键字。

    因此,您可以轻松地将它们放在函数的顶部(内部或外部):

    function Invoke-Authenticate {
    <#
    .SYNOPSIS
    ...
    #>
    

    <#
    .SYNOPSIS
    ...
    #>
    function Invoke-Authenticate {
    

    并非所有部分都是必需的,您可以添加任何您想要的部分。这取决于您的用例和公司指南。我通常至少包括.SYSNOPSIS.DESCRIPTION.PARAMETERS.EXAMPLES

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-25
      • 2010-11-14
      • 2020-12-27
      • 2019-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多