Public/Users/New-UKGUser.ps1
|
function New-UKGUser { <# .SYNOPSIS Creates a new user in the UKG HR Service Delivery system. .DESCRIPTION Creates a new user account (typically for HR administrators or system users). .PARAMETER Email The user's email address (required). .PARAMETER FirstName The user's first name (required). .PARAMETER LastName The user's last name (required). .PARAMETER RoleId The ID of the role to assign to the user. .PARAMETER OrganizationIds Array of organization IDs the user has access to. .PARAMETER Properties Hashtable of additional properties to set on the user. .PARAMETER InputObject A hashtable or PSCustomObject containing the user data. .EXAMPLE New-UKGUser -Email "admin@company.com" -FirstName "Admin" -LastName "User" -RoleId "role123" .EXAMPLE $user = @{ email = "hr@company.com" first_name = "HR" last_name = "Manager" role_id = "role456" } New-UKGUser -InputObject $user .OUTPUTS UKG.User object representing the created user. #> [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'Properties')] [OutputType([PSCustomObject])] param( [Parameter(Mandatory, ParameterSetName = 'Properties')] [ValidateNotNullOrEmpty()] [string]$Email, [Parameter(Mandatory, ParameterSetName = 'Properties')] [ValidateNotNullOrEmpty()] [string]$FirstName, [Parameter(Mandatory, ParameterSetName = 'Properties')] [ValidateNotNullOrEmpty()] [string]$LastName, [Parameter(ParameterSetName = 'Properties')] [string]$RoleId, [Parameter(ParameterSetName = 'Properties')] [string[]]$OrganizationIds, [Parameter(ParameterSetName = 'Properties')] [hashtable]$Properties, [Parameter(Mandatory, ParameterSetName = 'InputObject', ValueFromPipeline)] [object]$InputObject ) process { $body = @{} if ($PSCmdlet.ParameterSetName -eq 'InputObject') { if ($InputObject -is [hashtable]) { $body = $InputObject.Clone() } else { foreach ($prop in $InputObject.PSObject.Properties) { $body[$prop.Name] = $prop.Value } } } else { $body['email'] = $Email $body['first_name'] = $FirstName $body['last_name'] = $LastName if ($RoleId) { $body['role_id'] = $RoleId } if ($OrganizationIds) { $body['organization_ids'] = $OrganizationIds } if ($Properties) { foreach ($key in $Properties.Keys) { $body[$key] = $Properties[$key] } } } $displayName = if ($body['email']) { $body['email'] } else { "$FirstName $LastName" } if ($PSCmdlet.ShouldProcess($displayName, 'Create User')) { $response = Invoke-UKGRequest -Endpoint '/users' -Method POST -Body $body if ($response) { $response.PSObject.TypeNames.Insert(0, 'UKG.User') } return $response } } } |