The Custom PowerShell command which will be executed must have a param (…) block. The entire command is parsed and validated for PowerShell 5. The command must be syntactically correct in order to pass validation.
Supported types
The following is a list of the supported types:
PowerShell | Field | Notes |
-none- | Text | |
[string] | Text | (1) |
[byte] | Number | (1) |
[sbyte] | Number | (1) |
[short] | Number | (1) |
[ushort] | Number | (1) |
[int] | Number | (1) |
[uint] | Number | (1) |
[ulong] | Number | (1) |
[float] | Number | (1) |
[double] | Number | (1) |
[decimal] | String | (1) |
[bool] | Boolean | (1) |
[switch] | Boolean | |
-other- | String |
(1) Accepts CLR type name, eg System.String, System.UInt32, System.Boolean, etc
Recognised attributes
[Parameter]
Mandatory
is supported. Mandatory fields must be provided when user tries to execute script. Mandatory[bool]
and[switch]
parameters should be avoided. While most of values ($null
,1
,"true"
, …) can be converted to boolean value, user should use either[Parameter(Mandatory)]
or[Parameter(Mandatory = $true)]
.ParameterSetName
is not supported. Multiple parameter sets may cause script to be not executable.- Other parameters properties are ignored.
[ValidateNotNullOrEmpty]
Will be supported in the future.
Parsing and saving
When a command is stored, the parser extracts known validation attributes and stores information in the parameter model. This information is then translated into DTO so the user interface can render the appropriate field.
The parser ignores validation attributes it can not recognize.
Here is example of parameters and corresponding extracted validation.
// param ($Foo)
{
"name": "Foo",
"isMandatory": false,
"validateNotNullOrEmpty": false,
...
}
// param([Parameter(Mandatory)] [ValidateNotNullOrEmpty] $Foo)
{
"name": "Foo",
"isMandatory": true,
"validateNotNullOrEmpty": true,
...
}
Triggering execution
The Custom PowerShell commands are execute from the ‘Execute Command’ button in the user interface.
Mandatory and required fields
Text fields
isMandatory | validate Not Null Or Empty | parameters value (execute request) | request is valid | Notes |
false | false | { } | true | |
false | false | { “Foo” : “” } | true | |
false | false | { “Foo” : null } | true | |
false | false | { “Foo” : “bar” } | true | |
true | false | { } | false | UI must send { “Foo” : “” } |
true | false | { “Foo” : “” } | true | |
true | false | { “Foo” : null } | true | This is acceptable but “” is preferred. |
true | false | { “Foo” : “bar” } | true | |
false | true | { } | true | |
false | true | { “Foo” : “” } | false | |
false | true | { “Foo” : null } | false | |
false | true | { “Foo” : “bar” } | true | |
true | true | { } | false | Field is required |
true | true | { “Foo” : “” } | false | Field is required |
true | true | { “Foo” : null } | false | Field is required |
true | true | { “Foo” : “bar” } | true |
Boolean fields
isMandatory | parameters value (execute request) | isValid | Notes |
false | { } | true | |
false | { “Foo”: false } | true | |
true | { } | false | |
true | { “Foo” : true } | true |
Example scripts
Create a user, using the AzureAD module:
param(
[Parameter(Mandatory=$true)] $displayname,
[Parameter(Mandatory=$true)] $givenName,
[Parameter(Mandatory=$true)] $surName,
[Parameter(Mandatory=$true)] $upn,
[Parameter(Mandatory=$true)] $usageLocation,
[Parameter(Mandatory=$true)] $nickname,
[Parameter(Mandatory=$true)] $password,
[Parameter(Mandatory=$true)] $skuname
)
$PasswordProfile = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile
$PasswordProfile.Password = "$password"
New-AzureADUser -DisplayName "$displayname" -GivenName "$givenName" -SurName "$surName" -UserPrincipalName $upn -MailNickName $nickname -PasswordProfile $PasswordProfile -AccountEnabled $true
Set-AzureADUser -ObjectID $upn -UsageLocation $usageLocation
# Create the objects we'll need to add and remove licenses
$license = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
# Find the SkuID of the license we want to add e.g. Win10_VDA_E3
$license.SkuId = (Get-AzureADSubscribedSku | Where-Object -Property SkuPartNumber -Value "$skuname" -EQ).SkuID
# Set the Office license as the license we want to add in the $licenses object
$licenses.AddLicenses = $license
Set-AzureADUserLicense -ObjectId "$upn" -AssignedLicenses $licenses
Create a new Microsoft Team, with some specified channels:
param(
[Parameter(Mandatory=$true)] $TeamName,
[Parameter(Mandatory=$true)] $desc,
[Parameter(Mandatory=$true)] $TeamVisibility,
[Parameter(Mandatory=$true)] $channelName1,
[Parameter(Mandatory=$true)] $channelName2,
[Parameter(Mandatory=$true)] $channelName3
)
$group = New-Team -DisplayName "$TeamName" -Description "$desc" -visibility $TeamVisibility
New-TeamChannel -GroupId $group.GroupId -DisplayName "$channelName1"
New-TeamChannel -GroupId $group.GroupId -DisplayName "$channelName2"
New-TeamChannel -GroupId $group.GroupId -DisplayName "$channelName3"