Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Artifacts/windows-add-user-to-group/Artifactfile.json
Original file line number Diff line number Diff line change
@@ -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'), '''\"')]"
}
}
76 changes: 76 additions & 0 deletions Artifacts/windows-add-user-to-group/addUserToGroup.ps1
Original file line number Diff line number Diff line change
@@ -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
}