Search code examples
powershelldynamics-crmmicrosoft-dynamicsazure-powershell

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


I am creating Dynamics environments using PS which works perfectly fine. https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerApps.Administration.PowerShell/new-adminpowerappenvironment?view=pa-ps-latest

I am enabling an option for all users to create environments and wanted to create a simple form for them to create new environments. I tested and checked a lot of suggestions, but I cannot seem to extract the data from the TextBox-es into the command. First I tested the options with dropdowns with predefined values, but got the below error:

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.

I then changed everything to TextBox, but again the same error. Below is the Form:

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
}

Note: you will most probably not be able to test the script if you do not have a trial or a subscription for Dynamics, but I would appreciate any suggestions for the proper way to set the values in the form.

Thank you in advance.


Solution

  • When referencing variables in PowerShell strings, use double quotes (") rather than single quotes (')

    So

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

    Instead of

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

    The error message is spelling it out but its easy to miss! it means "$($TextBox1.Text)" quite literally, its not referring to the actual value of the variable at runtime.

    To avoid this issue when passing parameters that have nothing other than a variable, just pass it as below without quotes:

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

    See this link which shows quoting rules: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7.1