webtet 发表于 2018-9-3 07:10:55

Powershell_Script<1>

  Windows Powershell中的函数参数
  PowerShell中的最大特点之一是函数使用上的可扩展性强。在这篇文章中,我们将仔细看一下专业类型的函数:产品质量函数。
  AD:
  在先前关于用户自定义的Windows PowerShell的的文章中,我已经说过PowerShell中的最大特点之一是函数使用上的可扩展性强。在这篇文章中,我们将仔细看一下专业类型的函数:产品质量函数。
  你问有什么区别?产品质量函数花力气来测试输入并在提供信息输出的情况下为算是错误进行稳固工作。通常当在为产品运用函数时,你想知道它是否中断-- 同时你也一定很想知道为什么。其它的语言需要你自己来设计参数和处理错误。我们是幸运的,Windows PowerShell有许多类似的内置函数。
  PowerShell的参数
  当我们谈论Windows PowerShell函数的时候,我们需要考虑三件事情:输入、输出和错误。这篇文章将重点说明输入,也被称为参数。PowerShell有许多参数选项,并且可以通过以下三种方式之一来进行运用:
  位置参数
  PowerShell可以创建一个数值数组传递给函数的$args变量。传递给函数的每一个值从0开始被添加到这个数组中。例如:
  function foo
  {
  Write-Host $args $args
  }
  foo &quot;This is parameter 1&quot; &quot;This is parameter 2&quot;名字参数
  PowerShell输入的参数也可以命名,这就意味着它们可以通过名字传递,并且值被放置在相应的变量里。例如(注意当这个函数被调用的时候,参数颠倒,但是数值能正确的返回):
  Example (notice the parameters are reversed when the function is called,
  but the values are returned correctly):
  function foo
  {
  Param($param1,$param2)
  Write-Host $param1 $param2
  }
  foo -param2 &quot;This is parameter 2&quot; -param1 &quot;This is
  parameter 1&quot;Splatting参数
  在PowerShell的参数传递中,这个或许是最常用的方法。它包含创建一个数组或哈希表作为传递给函数的参数组。这个让你可以动态地创建整个脚本的参数,然后当你准备好后即可调用函数。例如:
  function foo
  {
  Param($param1,$param2)
  Write-Host $param1 $param2
  }
  Create Hash table
  $blah = @{&quot;Param1&quot;=&quot;This is parameter 1&quot;;
  &quot;Param2&quot;=&quot;This is parameter 2&quot;}
  # Pass hash table to function
  foo @BlahPowerShell 参数的属性
  Mandatory – 这个属性在PowerShell参数选项里是默认的,但是如果你知道你所需要的参数类型,你可以使用这个属性来强制用户传递这种类型的参数。如果它们没有这样做,PowerShell将报错给它们,并且强迫的它们提供这种类型的值,以便函数能够正常的运行。例如:
  function foo
  {
  Param(
  
  $param1
  )
  Write-Host $param1
  }ParameterSetName --我们常常需要一起传递一组参数(通常因为一些意外所中断)。例如,你有一个函数要获得一个活动目录对象,如果它是一个用户或是一个计算机,你就需要知道帐户:
  function Get-ADObject
  {
  Param(
  [Parameter(Mandatory=$True,
  ParameterSetName=&quot;User&quot;)]
  $User,
  [Parameter(Mandatory=$True,
  ParameterSetName=&quot;Computer&quot;)]
  $Computer
  )
  $PScmdlet.ParameterSetName
  }
  Get-ADObject --# This will throw an error because no
  parameters passed
  Get-ADObject –user &quot;joe&quot; # Will return 'User'
  Get-ADObject –Computer &quot;joe&quot; # Will return 'Computer'
  Get-ADObject –User &quot;joe&quot; –Computer &quot;joe&quot; # Will return
  an errorValueFromPipeline -- 这个属性告诉函数某个特定的参数值可以通过管道来传递参数。例如:
  function Get-ADUserObject
  {
  Param(
  
  $User,
  )
  Process
  {
  $User
  }
  }
  }
  $ListofUsers | Get-ADUserObjectValueFromPipelineByPropertyName -- 这个属性似乎与ValueFromPipeline有点相似,但是并不是使用“类型”,它使用的是传入对象的属性名称。例如,如果你有一个叫做UserName的用户对象的属性。
  function Get-ADUserObject
  {
  Param(
  [Parameter(ValueFromPipeline
  ByPropertyName=$true)]
  $Username,
  )
  Process
  {
  $UserName
  }
  }
  $ListofUserObjects | Get-ADUserObjectHelpMessage -- 这允许你给用户添加一个帮助信息。如果他们没有指定mandatory属性来调用你的函数,这可以给他们解释需要输入用户名:
  function Get-ADComputerObject
  {
  Param(
  [Parameter(Mandatory=$True,HelpMessage=
  &quot;Enter computer name.&quot;)]
  $ComputerName,
  )
  $ComputerName
  }
  以上这些信息应该能够帮助你开始写一些产品质量函数.

页: [1]
查看完整版本: Powershell_Script<1>