From 6b858f278c6a9035bdf556834303f860275d1a35 Mon Sep 17 00:00:00 2001 From: David Mikhayelyan Date: Mon, 11 Nov 2019 14:18:00 -0800 Subject: [PATCH 1/8] Added first version of addUserToGroup artifact --- .../Artifactfile.json | 25 +++++++ .../addUserToGroup.ps1 | 66 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 Artifacts/windows-add-user-to-group/Artifactfile.json create mode 100644 Artifacts/windows-add-user-to-group/addUserToGroup.ps1 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..ec0593e81 --- /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 user group", + "description": "Adds a user account to a user group on the targetted virtual machine.", + "publisher": "Microsoft", + "tags": [ + "Windows" + ], + "targetOsType": "Windows", + "parameters": { + "username": { + "type": "string", + "displayName": "User account", + "description": "The user account that will be added to the provided user group of the target virtual machine." + }, + "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..e4875a424 --- /dev/null +++ b/Artifacts/windows-add-user-to-group/addUserToGroup.ps1 @@ -0,0 +1,66 @@ +# +# 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 +} From 7f41cd3891c02b69b99e76c3a5eedda0dc6ddad2 Mon Sep 17 00:00:00 2001 From: David Mikhayelyan Date: Mon, 11 Nov 2019 15:20:45 -0800 Subject: [PATCH 2/8] Added parameters to script file --- .../windows-add-user-to-group/addUserToGroup.ps1 | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Artifacts/windows-add-user-to-group/addUserToGroup.ps1 b/Artifacts/windows-add-user-to-group/addUserToGroup.ps1 index e4875a424..57e2bbd13 100644 --- a/Artifacts/windows-add-user-to-group/addUserToGroup.ps1 +++ b/Artifacts/windows-add-user-to-group/addUserToGroup.ps1 @@ -1,4 +1,15 @@ -# +# Parameters for this script file. +# + +[CmdletBinding()] +param( + [string] $Username, + [string] $Group +) + +################################################################################################### + +# # Functions used in this script. # From 1540446982550fe3487354d3b64dbe074eb6b99d Mon Sep 17 00:00:00 2001 From: David Mikhayelyan Date: Mon, 11 Nov 2019 15:26:39 -0800 Subject: [PATCH 3/8] Added quotation marks --- Artifacts/windows-add-user-to-group/Artifactfile.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Artifacts/windows-add-user-to-group/Artifactfile.json b/Artifacts/windows-add-user-to-group/Artifactfile.json index ec0593e81..dc70cac37 100644 --- a/Artifacts/windows-add-user-to-group/Artifactfile.json +++ b/Artifacts/windows-add-user-to-group/Artifactfile.json @@ -20,6 +20,6 @@ } }, "runCommand": { - "commandToExecute": "[concat('powershell.exe -ExecutionPolicy bypass \"& ./addUserToGroup.ps1', ' -Username ', parameters('username'), ' -Group ', parameters('group'), '\"')]" + "commandToExecute": "[concat('powershell.exe -ExecutionPolicy bypass \"& ./addUserToGroup.ps1', ' -Username ', \"parameters('username')\", ' -Group ', \"parameters('group')\", '\"')]" } } From a242ce5f6ad75156e43215d8abf7f57bafde55af Mon Sep 17 00:00:00 2001 From: David Mikhayelyan Date: Mon, 11 Nov 2019 15:47:20 -0800 Subject: [PATCH 4/8] Removed quotation marks, now using multiple single quotes --- Artifacts/windows-add-user-to-group/Artifactfile.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Artifacts/windows-add-user-to-group/Artifactfile.json b/Artifacts/windows-add-user-to-group/Artifactfile.json index dc70cac37..9ea9a1df9 100644 --- a/Artifacts/windows-add-user-to-group/Artifactfile.json +++ b/Artifacts/windows-add-user-to-group/Artifactfile.json @@ -20,6 +20,6 @@ } }, "runCommand": { - "commandToExecute": "[concat('powershell.exe -ExecutionPolicy bypass \"& ./addUserToGroup.ps1', ' -Username ', \"parameters('username')\", ' -Group ', \"parameters('group')\", '\"')]" + "commandToExecute": "[concat('powershell.exe -ExecutionPolicy bypass \"& ./addUserToGroup.ps1', ' -Username ''', parameters('username'), ''' -Group ''', parameters('group'), '''\"')]" } } From cdf21da23aea05f08b0a9d3cdc993870cf22ea6d Mon Sep 17 00:00:00 2001 From: David Mikhayelyan Date: Mon, 11 Nov 2019 15:59:15 -0800 Subject: [PATCH 5/8] Fixed comment --- Artifacts/windows-add-user-to-group/Artifactfile.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Artifacts/windows-add-user-to-group/Artifactfile.json b/Artifacts/windows-add-user-to-group/Artifactfile.json index 9ea9a1df9..1e3288d4a 100644 --- a/Artifacts/windows-add-user-to-group/Artifactfile.json +++ b/Artifacts/windows-add-user-to-group/Artifactfile.json @@ -1,7 +1,7 @@ { "$schema": "https://raw.githubusercontent.com/Azure/azure-devtestlab/master/schemas/2015-01-01/dtlArtifacts.json", "title": "Add user to user group", - "description": "Adds a user account to a user group on the targetted virtual machine.", + "description": "Adds a user account to a group on the targetted virtual machine.", "publisher": "Microsoft", "tags": [ "Windows" @@ -11,7 +11,7 @@ "username": { "type": "string", "displayName": "User account", - "description": "The user account that will be added to the provided user group of the target virtual machine." + "description": "The user account that will be added to the provided group on the target virtual machine." }, "group": { "type": "string", From 5a38c82706b2f99097867cbb6c5c456d4db801f3 Mon Sep 17 00:00:00 2001 From: David Mikhayelyan Date: Mon, 11 Nov 2019 16:01:42 -0800 Subject: [PATCH 6/8] Fixed title so its just group instead of user group --- Artifacts/windows-add-user-to-group/Artifactfile.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Artifacts/windows-add-user-to-group/Artifactfile.json b/Artifacts/windows-add-user-to-group/Artifactfile.json index 1e3288d4a..81f6ff5ef 100644 --- a/Artifacts/windows-add-user-to-group/Artifactfile.json +++ b/Artifacts/windows-add-user-to-group/Artifactfile.json @@ -1,6 +1,6 @@ { "$schema": "https://raw.githubusercontent.com/Azure/azure-devtestlab/master/schemas/2015-01-01/dtlArtifacts.json", - "title": "Add user to user group", + "title": "Add user to group", "description": "Adds a user account to a group on the targetted virtual machine.", "publisher": "Microsoft", "tags": [ From c3b49a472f05ebd0165863882bff3f96a0268eb1 Mon Sep 17 00:00:00 2001 From: David Mikhayelyan Date: Mon, 11 Nov 2019 16:09:25 -0800 Subject: [PATCH 7/8] Added comments to clarify that this can be used for groups as well as for users --- Artifacts/windows-add-user-to-group/Artifactfile.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Artifacts/windows-add-user-to-group/Artifactfile.json b/Artifacts/windows-add-user-to-group/Artifactfile.json index 81f6ff5ef..aeec4aeac 100644 --- a/Artifacts/windows-add-user-to-group/Artifactfile.json +++ b/Artifacts/windows-add-user-to-group/Artifactfile.json @@ -1,7 +1,7 @@ { "$schema": "https://raw.githubusercontent.com/Azure/azure-devtestlab/master/schemas/2015-01-01/dtlArtifacts.json", "title": "Add user to group", - "description": "Adds a user account to a group on the targetted virtual machine.", + "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" @@ -10,8 +10,8 @@ "parameters": { "username": { "type": "string", - "displayName": "User account", - "description": "The user account that will be added to the provided group on the target virtual machine." + "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", From 11a4beb4ee0b1bfb55585b4c05de88b4f66051e5 Mon Sep 17 00:00:00 2001 From: David Mikhayelyan Date: Mon, 11 Nov 2019 16:14:20 -0800 Subject: [PATCH 8/8] Removed extra line --- Artifacts/windows-add-user-to-group/addUserToGroup.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/Artifacts/windows-add-user-to-group/addUserToGroup.ps1 b/Artifacts/windows-add-user-to-group/addUserToGroup.ps1 index 57e2bbd13..e53b8afa6 100644 --- a/Artifacts/windows-add-user-to-group/addUserToGroup.ps1 +++ b/Artifacts/windows-add-user-to-group/addUserToGroup.ps1 @@ -1,5 +1,4 @@ # Parameters for this script file. -# [CmdletBinding()] param(