【问题标题】:Return result of a function into the run time script将函数的结果返回到运行时脚本
【发布时间】:2015-12-09 20:34:39
【问题描述】:

我有两个 powershell 脚本,一个具有检查作为参数传递的变量“name”的值的函数。如果不匹配“John”,则结果为 2,否则,结果为 0(包含在函数中)。

这里是函数脚本:

Function test {

    Param (
        $name = $(throw "need -name"),
        $result = 0         
    )

    Process {
        if($name -ne "John") {
            Write-Host "Name is incorrect"
            $result = 2 
        } else {
            Write-Host "Student is correct"
        }
    }  
}

第二个脚本调用这个函数并运行为 -

Import-Module C:\function.ps1
test -name Sandra

现在我想将结果(或状态)从函数返回到第二个脚本,并根据结果在第二个脚本中添加更多条件。为了实现这一点,脚本或函数需要进行哪些更改?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    您似乎是 PowerShell 的新手,所以这里有一些提示:

    1. 您应该使用Approved Verbs e。 G。改变你的功能 命名为Test-StudentName
    2. 有一个language integrated support for parameter handling
    3. Using Write-Host is almost always wrong

    所以你的脚本可能看起来像这样:

    function Test-StudentName
    {
        [CmdletBinding()]
        [OutputType([int])]
        Param
        (
            [Parameter(Mandatory=$true, Position=0)]
            [ValidateNotNullOrEmpty()]
            [string]
            $Name
        )
    
        if ($name -ne 'John') 
        {
            Write-Output 2
        }
        else
        {
            Write-Output 0
        }
    }
    

    【讨论】:

      【解决方案2】:
      <#
      .Synopsis
         Short description
      .DESCRIPTION
         Long description
      .EXAMPLE
         Example of how to use this cmdlet
      .EXAMPLE
         Another example of how to use this cmdlet
      #>
      function Test-Name
      {
          [CmdletBinding()]
          [Alias()]
          [OutputType([int])]
          Param
          (
              # Parameter Name help description
              [Parameter(Mandatory=$true,
                         ValueFromPipelineByPropertyName=$true,
                         Position=0)]
              [string]
              $Name
          )
      
          Begin
          {
          }
          Process
          {
      
              if($name -ne "John") {
                  Write-Host "Name is incorrect"
                  2 
              }
              else {
                  Write-Host "Student is correct"
                  0
              }
          }
          End
          {
          }
      }
      

      然后你“点源”

      Windows PowerShell
      Copyright (C) 2015 Microsoft Corporation. All rights reserved.
      
      PS C:\Users\joshua> cd .\Desktop\
      PS C:\Users\joshua\Desktop> . .\Test-Name.ps1
      PS C:\Users\joshua\Desktop> Test-Name -Name Sandra
      Name is inccorrect
      2
      

      “点源”部分是“..\Test-Name.ps1”。或者,您可以将文件另存为 .psm1——这是创建模块的最简单方法——然后导入它

      PS 是的,Write-Host 的使用是非常不鼓励的,尽管在 powershell 版本 5 中并没有那么多

      【讨论】:

        猜你喜欢
        • 2022-01-12
        • 1970-01-01
        • 2012-08-29
        • 1970-01-01
        • 1970-01-01
        • 2015-05-29
        • 2018-09-05
        • 2020-04-30
        • 2011-01-07
        相关资源
        最近更新 更多