Rob*_*zek 5 powershell powercli
我正在编写一个脚本来部署 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
Run Code Online (Sandbox Code Playgroud)
我希望用户能够使用先前执行的 GET 命令的输出自动完成字符串。如果可以的话请帮忙
如果它是函数参数,您可以使用 [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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2271 次 |
最近记录: |