是否可以使用之前的 Get 命令输出自动完成 Read-Host 提示。不保存输出文件

问题描述 投票:0回答:2

我正在编写一个脚本来部署 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
2个回答
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

0
投票

如果它是函数参数,您可以使用 [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
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.