【问题标题】:Is it possible to autocomplete a Read-Host prompt with previous Get command output. Without saving an output file是否可以使用先前的 Get 命令输出自动完成读取主机提示。不保存输出文件
【发布时间】:2019-08-16 14:04:29
【问题描述】:

我正在编写一个脚本来部署 VM 主机,并且我想运行一个 Get 命令来向它们显示可用选项,然后将它们的输入与上一个 GET 命令的自动完成一起使用。我这样做是因为我想避免在手动输入期间可能出现的任何拼写错误。

我曾尝试使用 Select-string,但我认为它会保存到 .txt 文件中,我不希望将其保存在 txt 文件中。我宁愿把它保存在变量中。

Get-VMHost | Select-Object -Property Name | Format-Table -Property Name
$VMHost = Read-Host -Prompt 'Please select the host for your VM

我希望用户能够使用之前执行的 GET 命令的输出自动完成字符串。可以的话请帮忙

【问题讨论】:

    标签: powershell powercli


    【解决方案1】:

    这是我用于类似目的的 New-ChoicePrompt 函数。

    function New-ChoicePrompt {  [cmdletBinding()]
      param( 
          [parameter(mandatory=$true)]$Choices, 
          $Property, 
          $ReadProperty, 
          $ExprLabel,
          [switch]$AllowManualInput, 
          [Scriptblock]$ReadPropertyExpr, 
          $ManualInputLabel = "Type my own"
      )
      if ( $choices[0] -isnot [string] -and !$property ) {"Please include New-ChoicePrompt -Property unless -Choices is an array of strings."; break}
      if ( $choices[0] -is [string] -and ($property -or $ReadProperty) ) {"When New-ChoicePrompt -Choices is an array of strings, please omit -Property and -ReadProperty."; break}
      #if ( $choices[0] -isnot [string] -and $allowManualInput ) {"When New-ChoicePrompt -Choices is a PSobject, please omit -AllowManualInput"; break}
      $x = 0; $script:options = @()
      $script:propty = $property
      $script:choices = $choices
      $manualInputLabel = "<" + $manualInputLabel + ">"
      foreach ($item in $choices) { $value = $null
        $x += 1
        if ($property) { $value = $item | select -expand $property } `
        else {$value = $item}
        if ($readProperty) {
          $readVal = $item | select -expand $readProperty
          $row = new-object -type psObject -property @{Press = $x; 'to select' = $value; $readproperty = $readVal}
        } ` #close if readProperty
        elseif ($readPropertyExpr) `
        {
          $readVal = & $ReadPropertyExpr
          $row = new-object -type psObject -property @{Press = $x; 'to select' = $value; $ExprLabel = $readVal}
        }` #close if readPropertyExpr
        else { $row = new-object -type psObject -property @{'to select' = $value; Press = $x} }
        $script:options += $row
      } #close foreach
      if ($AllowManualInput) {
        $row = new-object -type psObject -property @{'to select' = $manualInputLabel; Press = ($x + 1) }
        $script:options += $row
      } #close if allowManualInput
      if ($ReadProperty) { $script:options | Select Press, "to select", $readproperty | ft -auto }
      elseif ($ReadPropertyExpr) { $script:options | Select Press, "to select", $ExprLabel | ft -auto }
      else { $script:options | Select Press, "to select" | ft -auto }
    } #end function new-choicePrompt
    

    这是一个使用示例。

      $vmhosts = Get-VMHost | sort Name
      if ($vmhosts.count -gt 1) {
        do {
          new-choicePrompt -choices $vmhosts -property name
          $in = read-host -prompt 'Please select a target host'
          $range = $options | select -expand press
        } #close do
        until ($range -contains $in)
        $selection = $options | where {$_.press -eq $in} | select -expand 'To select'
        $choice = $choices | where {$_.@($propty) -eq $selection} 
        $vmHost = $choice
      } else {$vmhost = $vmhosts} #close if multiple hosts
      "Target host: " + $vmhost.name
    

    【讨论】:

      【解决方案2】:

      如果它是一个函数参数,您可以使用 [ValidateSet] 来限制值,例如:https://www.mssqltips.com/sqlservertip/4205/powershell-parameters-part-ii--validateset-and-validatepattern/ 它最终也支持制表符完成。如果您将其设置为强制,如果未给出,它也会提示输入。

      Function Pass-Set {
          Param(
              [ValidateSet("oro","plata")][string]$specificstring
          )
          Process
          {
              Write-Host "It must be one of two words; in this case $specificstring."
          }
      }
      
      pass-set -specificstring oro
      It must be one of two words; in this case oro.
      
      pass-set -specificstring plata
      It must be one of two words; in this case plata.
      
      pass-set -specificstring plata2
      Pass-Set : Cannot validate argument on parameter 'specificstring'. The argument "plata2" does not belong to the set "oro,plata" specified by the
      ValidateSet attribute. Supply an argument that is in the set and then try the command again.
      At line:1 char:26
      + pass-set -specificstring plata2
      +                          ~~~~~~
          + CategoryInfo          : InvalidData: (:) [Pass-Set], ParameterBindingValidationException
          + FullyQualifiedErrorId : ParameterArgumentValidationError,Pass-Set
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-08
        • 1970-01-01
        • 1970-01-01
        • 2021-12-28
        相关资源
        最近更新 更多