我正在使用PS创建Dynamics环境,它运行得非常好。https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerApps.Administration.PowerShell/new-adminpowerappenvironment?view=pa-ps最新
我为所有用户启用了一个创建环境的选项,并希望为他们创建一个简单的表单来创建新的环境。我测试并检查了很多建议,但似乎无法将TextBoxes中的数据提取到命令中。首先,我用预定义的值测试了下拉选项,但得到了以下错误:
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 ...
请参阅此链接,其中显示引用规则:https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7.1