Functions/Copy.ps1
function Copy-LinaUserGroup { <# .SYNOPSIS Clones a user group with a new name .DESCRIPTION Clones a user group with a new name .INPUTS Array of LinaUserGroup .OUTPUTS The new LinaUserGroup Object .PARAMETER Tenant Optional : Name of the tenant where the user group should be created. If not provided, user group will be created in the current tenant. In global view (no tenant selected), agent will be created in the default tena .PARAMETER Name Mandatory : Name for the new User Group .EXAMPLE Get-LinaUserGroup -Name "MyTemplateUG" | Copy-LinaUserGroup -Name "MyUserGroup" -Tenant "MyTenant" Clone the MyTemplateUG user group to MyUserGroup in tenant MyTenant .EXAMPLE Get-LinaUserGroup -Name "MyTemplates*" | Copy-LinaUserGroup -Tenant "MyTenant" Clone all MyTemplates* user groups to tenant MyTenant #> [cmdletbinding()] Param( [Parameter(ValueFromPipeline = $True, ParameterSetName = "ByObject")] [pscustomobject[]]$LinaUserGroups, [Parameter(Mandatory = $false,ParameterSetName = "ByObject", Position = 0)] [ValidateNotNullOrEmpty()] [string]$Name, [Parameter(Mandatory = $False,ParameterSetName = "ByObject", Position = 1)] [ValidateNotNullOrEmpty()] [string]$Tenant ) BEGIN { $nb_of_usergroups_to_create = 0 if ($global:GLOBAL_LINA_CONNECTED -eq $False) { Write-Error "$($global:GLOBAL_DISCONNNECTED_MESSAGE)" return } if ($PSVersionTable.PSVersion.Major -lt 5) { Write-Host2 "ERROR : This cmdlet requires PowerShell 5.0 or greater in order to work" Return } } PROCESS { # Should be useless now If (!$LinaUserGroups.ID) { return } foreach ($LinaUserGroup in $LinaUserGroups) { $nb_of_usergroups_to_create += 1 # Should be useless now If (!$LinaUserGroup.ID) { return } if (!$Name) { $Name = $LinaUserGroup.Name }elseif ($nb_of_usergroups_to_create -gt 1) { Write-Host2 "ERROR : You cannot use -Name if you pipe multiple user groups. Remove the -Name option, Original Names will be kept" Return } Write-Host2 "Creating a Lina user group named $Name" $current_tenant_id = Get-LinaCurrentTenantID 6>$null if (!$Tenant) { if ($current_tenant_id -le 1) { # No current tenant (global view) => using default tenant $domain = LinaTenantShort 6>$null | Where-Object { $_.IsDefault } if ($domain.ID -le 1) { # No default Tenant .... Write-Host2 "ERROR : There is no default tenant. Please select a tenant." return } $domain_id = [int]$domain.ID $domain_name = [string]$domain.Name Write-Host2 "No tenant selected, user profile will be created in default tenant $domain_name (ID $domain_id)" } else { # Using Current tenant $domain = LinaTenantShort -ID (Get-LinaCurrentTenantID) 6>$null $domain_id = [int]$domain.ID $domain_name = [string]$domain.Name Write-Host2 "No tenant selected, user profile will be created in current tenant $domain_name (ID $domain_id)" } } else { $domain = LinaTenantShort 6>$null | Where-Object { $_.Name -eq $Tenant } $domain_id = [int]$domain.ID $domain_name = [string]$domain.Name if ( !($domain_id -gt 0) ) { Write-Host2 "ERROR : No tenant found with name $Tenant. Please input the exact name of the Tenant" Return } } $void = Set-LinaCurrentTenant -ID $domain_id 6>$null # Needs to clarify if ID of hierarchy can be anything else than 1 or not (<ID>1</ID>) $usergroup_exists = Get-LinaUserGroup -Name $Name 6>$null if ($Null -ne $usergroup_exists) { Write-Host2 "ERROR : A user group already exists with this name ($Name) in this Tenant" Return } if ($LinaUserGroup) { $ObjectType = [string]$LinaUserGroup.GetType() if ($ObjectType -like "*PSCustomObject*") { $item = $LinaUserGroup.PSObject.Properties $role1 = 0 $role2 = 0 $role3 = 0 $role4 = 0 $comment = "" foreach ($param in $item) { if ($ObjectType -like "*PSCustomObject*") { $current_key = $($param.Name) $current_value = $($param.Value) } else { $current_key = $param.Key $current_value = $param.Value } switch ($current_key) { InternalRoles1 { $role1 = $current_value } InternalRoles2 { $role2 = $current_value } InternalRoles3 { $role3 = $current_value } InternalRoles4 { $role4 = $current_value } Comment { $comment = $current_value } } } $name_enc = [System.Web.HttpUtility]::UrlEncode($Name) $comment_enc = [System.Web.HttpUtility]::UrlEncode($comment) $request = CallAPI -Path "/ADM/add_user_group.json?name=$name_enc&roles1=$role1&roles2=$role2&roles3=$role3&roles4=$role4&comment=$comment_enc" -ContentType "application/json" if ([string]$request -like "*[0]*") { $usergroup_id = $request.id Write-Host2 "User group $Name successfully created (ID $usergroup_id)" $usergroup_created = Get-LinaUserGroup -ID $usergroup_id # Setting back the current tenant to what it was before the Copy-LinaAgentGroup command $void = Set-LinaCurrentTenant -ID $current_tenant_id 6>$null Return $usergroup_created } else { TranslateErrorCode -LinaError $request -FailedAction "create user group $Name" # Setting back the current tenant to what it was before the Copy-LinaAgentGroup command $void = Set-LinaCurrentTenant -ID $current_tenant_id 6>$null Return $null } } else { Write-Host2 "ERROR : This method is not supported yet. Please provide an existing UserGroupas input : Get-LinaUserGroup | New-LinaUserGroup" Return $null #$item = $LinaUserGroup.GetEnumerator() } } } } END { } } |