diff --git a/Artifacts/windows-add-user-to-group/Artifactfile.json b/Artifacts/windows-add-user-to-group/Artifactfile.json new file mode 100644 index 000000000..aeec4aeac --- /dev/null +++ b/Artifacts/windows-add-user-to-group/Artifactfile.json @@ -0,0 +1,25 @@ +{ + "$schema": "https://raw.githubusercontent.com/Azure/azure-devtestlab/master/schemas/2015-01-01/dtlArtifacts.json", + "title": "Add user to group", + "description": "Adds a user to a group on the targetted virtual machine. You can also use this artifact to add a group to another group.", + "publisher": "Microsoft", + "tags": [ + "Windows" + ], + "targetOsType": "Windows", + "parameters": { + "username": { + "type": "string", + "displayName": "User", + "description": "The user that will be added to the provided group on the target virtual machine. You can also use a group here instead of a user." + }, + "group": { + "type": "string", + "displayName": "Group", + "description": "The group that the user will be added to on the target virtual machine." + } + }, + "runCommand": { + "commandToExecute": "[concat('powershell.exe -ExecutionPolicy bypass \"& ./addUserToGroup.ps1', ' -Username ''', parameters('username'), ''' -Group ''', parameters('group'), '''\"')]" + } +} diff --git a/Artifacts/windows-add-user-to-group/addUserToGroup.ps1 b/Artifacts/windows-add-user-to-group/addUserToGroup.ps1 new file mode 100644 index 000000000..e53b8afa6 --- /dev/null +++ b/Artifacts/windows-add-user-to-group/addUserToGroup.ps1 @@ -0,0 +1,76 @@ +# Parameters for this script file. + +[CmdletBinding()] +param( + [string] $Username, + [string] $Group +) + +################################################################################################### + +# +# Functions used in this script. +# + +function Handle-LastError +{ + [CmdletBinding()] + param( + ) + + $message = $error[0].Exception.Message + if ($message) + { + Write-Host -Object "ERROR: $message" -ForegroundColor Red + } + + # IMPORTANT NOTE: Throwing a terminating error (using $ErrorActionPreference = "Stop") still + # returns exit code zero from the PowerShell script when using -File. The workaround is to + # NOT use -File when calling this script and leverage the try-catch-finally block and return + # a non-zero exit code from the catch block. + exit -1 +} + +function Validate-Params +{ + [CmdletBinding()] + param( + ) + + if ([string]::IsNullOrEmpty($Username)) + { + throw 'Username parameter is required.' + } + if ([string]::IsNullOrEmpty($Group)) + { + throw 'Group parameter is required.' + } +} + + +################################################################################################### + +# +# PowerShell configurations +# + +# NOTE: Because the $ErrorActionPreference is "Stop", this script will stop on first failure. +# This is necessary to ensure we capture errors inside the try-catch-finally block. +$ErrorActionPreference = "Stop" + +################################################################################################### + +# +# Main execution block. +# + +try +{ + Validate-Params + + Add-LocalGroupMember -Group $Group -Member $Username +} +catch +{ + Handle-LastError +}