获取 "Supply an argument that is in the set and then try the command again"

Getting "Supply an argument that is in the set and then try the command again"

我正在使用 PS 创建 Dynamics 环境,效果非常好。 https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerApps.Administration.PowerShell/new-adminpowerappenvironment?view=pa-ps-latest

我正在为所有用户启用一个选项来创建环境,并希望为他们创建一个简单的表单来创建新环境。 我测试并检查了很多建议,但我似乎无法将 TextBox-es 中的数据提取到命令中。首先,我使用带有预定义值的下拉菜单测试选项,但出现以下错误:

New-AdminPowerAppEnvironment : Cannot validate argument on parameter 'EnvironmentSku'. The argument "$($TextBox1.Text)" does not 
belong to the set "Trial,Sandbox,Production,SubscriptionBasedTrial,Teams" specified by the ValidateSet attribute. Supply an argument 
that is in the set and then try the command again.

然后我将所有内容都更改为 TextBox,但还是出现同样的错误。 以下是表格:

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = New-Object System.Drawing.Point(450,350)
$Form.text                       = "Creating Dynamics CRM Instance"
$Form.TopMost                    = $false

$Label1                          = New-Object system.Windows.Forms.Label
$Label1.text                     = "Environment Type"
$Label1.AutoSize                 = $true
$Label1.width                    = 25
$Label1.height                   = 10
$Label1.location                 = New-Object System.Drawing.Point(17,27)
$Label1.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$TextBox1                        = New-Object system.Windows.Forms.TextBox
$TextBox1.multiline              = $false
$TextBox1.width                  = 146
$TextBox1.height                 = 20
$TextBox1.location               = New-Object System.Drawing.Point(162,23)
$TextBox1.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$form.Controls.Add($TextBox1)

$Label2                          = New-Object system.Windows.Forms.Label
$Label2.text                     = "Currency"
$Label2.AutoSize                 = $true
$Label2.width                    = 25
$Label2.height                   = 10
$Label2.location                 = New-Object System.Drawing.Point(17,58)
$Label2.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$TextBox2                        = New-Object system.Windows.Forms.TextBox
$TextBox2.multiline              = $false
$TextBox2.width                  = 53
$TextBox2.height                 = 20
$TextBox2.location               = New-Object System.Drawing.Point(162,56)
$TextBox2.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$Label3                          = New-Object system.Windows.Forms.Label
$Label3.text                     = "Display Name"
$Label3.AutoSize                 = $true
$Label3.width                    = 25
$Label3.height                   = 10
$Label3.location                 = New-Object System.Drawing.Point(17,95)
$Label3.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = New-Object System.Drawing.Point(380,270)
$Form.text                       = "form"
$Form.TopMost                    = $false

$TextBox3                        = New-Object system.Windows.Forms.TextBox
$TextBox3.multiline              = $false
$TextBox3.width                  = 146
$TextBox3.height                 = 20
$TextBox3.location               = New-Object System.Drawing.Point(162,91)
$TextBox3.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$Label4                          = New-Object system.Windows.Forms.Label
$Label4.text                     = "Language"
$Label4.AutoSize                 = $true
$Label4.width                    = 25
$Label4.height                   = 10
$Label4.location                 = New-Object System.Drawing.Point(18,126)
$Label4.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$TextBox4                        = New-Object system.Windows.Forms.TextBox
$TextBox4.multiline              = $false
$TextBox4.width                  = 146
$TextBox4.height                 = 20
$TextBox4.location               = New-Object System.Drawing.Point(162,124)
$TextBox4.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$ButtonOK                        = New-Object system.Windows.Forms.Button
$ButtonOK.text                   = "Create"
$ButtonOK.width                  = 70
$ButtonOK.height                 = 30
$ButtonOK.location               = New-Object System.Drawing.Point(180,230)
$ButtonOK.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$ButtonOK.DialogResult           = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton               = $ButtonOK
$form.Controls.Add($ButtonOK)

$cancelButton                    = New-Object System.Windows.Forms.Button
$cancelButton.Location           = New-Object System.Drawing.Point(270,230)
$cancelButton.Size               = New-Object System.Drawing.Size(70,30)
$cancelButton.Text               = 'Cancel'
$cancelButton.Font               = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$cancelButton.DialogResult       = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton               = $cancelButton
$form.Controls.Add($cancelButton)

$Form.controls.AddRange(@($Label1,$TextBox1,$Label2,$TextBox2,$Label3,$TextBox3,$Label4,$TextBox4))


$form.Topmost = $true
$Form.controls.AddRange(@($Label1,$TextBox1,$Label2,$TextBox2,$Label3,$TextBox3,$Label4,$TextBox4,$ButtonOK, $cancelButton))
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    Add-PowerAppsAccount

    $x = New-AdminPowerAppEnvironment -EnvironmentSku '$($TextBox1.Text)' -LocationName europe -CurrencyName '$($TextBox2.Text)' -DisplayName '$($TextBox3.Text)' -LanguageName '$($TextBox4.Text)' -ProvisionDatabase -RegionName westeurope -WaitUntilFinished 1
    $x
}

注意:如果您没有试用版或 Dynamics 订阅,您很可能无法测试脚本,但对于在表单中设置值的正确方法的任何建议,我将不胜感激。

提前致谢。

在 PowerShell 字符串中引用变量时,使用双引号 (") 而不是单引号 (')

所以

... -EnvironmentSku "$($TextBox1.Text)" ...

而不是

... -EnvironmentSku '$($TextBox1.Text)' ...

错误信息拼写得很清楚,但很容易错过!它的字面意思是“$($TextBox1.Text)”,它不是指变量在运行时的实际值。

为了避免在传递除了变量之外没有任何其他参数时出现此问题,只需按如下所示不带引号传递它:

... -EnvironmentSku $TextBox1.Text ...

请参阅此 link,其中显示了引用规则:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7.1