Functions/Set.ps1
function Set-LinaStrategy { <# .SYNOPSIS Changes the configuration of a strategy .DESCRIPTION Changes the configuration Current limitations : two parameters (gateways where encryption is disabled or WAN mode disabled) cannot be modified .INPUTS Single LinaStrategy Object or Array of parameters .OUTPUTS LinaStrategy Object .PARAMETER Name Name of the strategy to modify .PARAMETER Tenant Optional : Name of the tenant where the strategy to modify is. Needed if multiple strategies have the same name across tenants .EXAMPLE @{ RPOInMinutes = 1 } | Set-LinaStrategy -Name "MyStrat" Modify strategy MyStrat with a RPO of 1 minute .EXAMPLE Get-LinaStrategy -Name "MyTemplate" | Set-LinaStrategy -Name "MyStrat" Clone parameters from strategy MyTemplate to the strategy "MyStrat" #> [cmdletbinding()] Param( [Parameter(ValueFromPipeline = $True, ParameterSetName = "ByObject")] [pscustomobject]$lina_strategy, [Parameter(Mandatory = $true, ParameterSetName = "ByObject", Position = 0)] [ValidateNotNullOrEmpty()] [string]$Name, [Parameter(Mandatory = $false, ParameterSetName = "ByObject", Position = 1)] [ValidateNotNullOrEmpty()] [string]$Tenant ) BEGIN { } PROCESS { $current_tenant_id = Get-LinaCurrentTenantID 6>$null if ($Tenant) { $domain = LinaTenantShort -Name $Tenant 6>$null $domain_id = [int]$domain.ID if ( !($domain_id -gt 0) ) { Write-Host "Error : No tenant found with name $Tenant. Please input the exact name of the Tenant" Return } $void = Set-LinaCurrentTenant -ID $domain_id 6>$null } if ($lina_strategy) { if ([string]$lina_strategy.GetType() -eq "System.Management.Automation.PSCustomObject") { $items = $lina_strategy.PSObject.Properties } else { # Get all items in the array $items = $lina_strategy.GetEnumerator() } $strategy_id = (Get-LinaStrategy -Name $Name).ID if (!$strategy_id -OR $strategy_id -le 0) { Write-Host "ERROR : No Strategy found with this name ($Name)" return } elseif ($strategy_id.Count -gt 1) { Write-Host "ERROR : Multiple strategies are matching this name ($Name). Please select a tenant to identify which one you want to modify." return } } $xml = '<?xml version="1.0" encoding="UTF-8"?><HNConfig><Func>SET</Func><ServiceProfile><ID>' + $strategy_id + '</ID>' foreach ($param in $items) { if ([string]$lina_strategy.GetType() -eq "System.Management.Automation.PSCustomObject") { $current_key = $($param.Name) $current_value = $($param.Value) } else { $current_key = $param.Key $current_value = $param.Value } switch ($current_key) { RPOInMinutes { $current_value = $current_value * 60 } AlwaysEncrypt { $current_value = !$current_value } AlertAfterDays { $current_value = $current_value * 24 * 3600 } QuotaMaxProtSizeMB { $current_value = $current_value * 1024 * 1024 } QuotaMaxHistSizeMB { $current_value = $current_value * 1024 * 1024 } TenantID { # Replacing the Tenant with the one provided if ($domain_id -ge 0) { $current_value = $domain_id } } } # Convert Boolean to 0 or 1 If ($current_value -eq $True -OR $current_value -eq $false) { $current_value = [int]$current_value } # Translate Powershell names to XML tags $int_trans = $INT_STRATEGIES_MAPPINGS[$current_key] if ($current_key -notin "Name", "ID", "InternalName", "ReplicationTarget", "Tenant" ) { # Filter out some parameters for creation $xml += "<$int_trans>$current_value</$int_trans>" } } $xml += "</ServiceProfile></HNConfig>" $body = "data=$xml" $request = CallAPI -Path "/mng_serviceprofile.html" -ContentType "application/xml" -Body $body if ([string]$request -like "*[0]*") { Write-Host "Strategy $Name successfully modified." $strategy_modified = Get-LinaStrategy -Name $Name # Setting back the current tenant to what it was before the Set-LinaStrategy command if ($Tenant) { Set-LinaCurrentTenant -ID $current_tenant_id 6>$null } Return $strategy_modified } else { TranslateErrorCode -LinaError $request -FailedAction "modify strategy $Name" Return $null } } END { } } function Set-LinaCurrentTenant { <# .SYNOPSIS Set the current tenant .DESCRIPTION Set the current tenant. When set, all listings and actions will be executed and filtered inside this tenant. To set the global view (view across all tenants), use the -All switch. .INPUTS LinaTenant Object .OUTPUTS None .PARAMETER ID ID of the tenant to set .PARAMETER Name Name of the tenant to set .PARAMETER All Switch to set the global view (view across all tenants). Available for superadmin only. .EXAMPLE Get-LinaTenant -Name "MyTenant" | Set-LinaCurrentTenant Set the current tenant to MyTenant .EXAMPLE Set-LinaCurrentTenant -All Enable global view across all tenants (superadmin only) .EXAMPLE Set-LinaCurrentTenant -ID 2 Set the current tenant to Tenant with ID 2 #> [cmdletbinding(DefaultParameterSetName = "ByID")] Param( [Parameter(ValueFromPipeline = $True, ParameterSetName = "ByObject")] [pscustomobject]$lina_tenant, [Parameter(Mandatory = $true, ParameterSetName = "ByID")] [ValidateNotNullOrEmpty()] [string]$ID, [Parameter(Mandatory = $true, ParameterSetName = "ByName")] [ValidateNotNullOrEmpty()] [string]$Name, [Parameter(Mandatory = $true, ParameterSetName = "All")] [ValidateNotNullOrEmpty()] [switch]$All ) if ($Name) { LinaTenantShort -Name $Name | Set-LinaCurrentTenant Return } elseif ($ID) { LinaTenantShort -ID $ID | Set-LinaCurrentTenant Return } # All tenants is 1 or -1 (no tenant selected, global view) if ($All) { $ID = -1 $tenant_name = "Global View / All Tenants" } elseif ($lina_tenant -AND $lina_tenant.ID -ge 1) { $ID = $lina_tenant.ID $tenant_name = $lina_tenant.Name } elseif (!$ID) { $ID = -1 $tenant_name = "Global View / All Tenants" } Write-Verbose "Setting current tenant to Tenant ID $ID" $request = CallAPI -Path "/ADM/domain_set_current.json?domain_id=$ID" if ($request.status -eq 0) { Write-Verbose "Current tenant has been set to $tenant_name (ID $ID)" Return $ID } else { TranslateErrorCode -LinaError $request.status -FailedAction "to set current tenant to tenant ID $ID" Return } } function Set-LinaAgent { <# .SYNOPSIS Changes the configuration of an agent. .DESCRIPTION Changes the configuration of an agent for example its name. .INPUTS Array of LinaAgent Objects .OUTPUTS The modified LinaAgent Objects .PARAMETER NewName New name for the agent. If multiple agent are passed, only the first one will be renamed as names have to be unique. .PARAMETER Strategy Change the strategy for this agent .PARAMETER Protection Change the protection for this agent .PARAMETER Tenant Move the agent to this tenant. .PARAMETER Group Move the agent to this agent group. Note : Agent group has to be in the same tenant as the agent or in its new tenant (if Tenant is provided) .EXAMPLE Get-LinaAgent -Name "TEST1" | Set-LinaAgent -NewName "TEST2" Changes the name of the agent from TEST1 to TEST2 .EXAMPLE Get-LinaAgent -Name "TEST1" | Set-LinaAgent -Tenant "MyTenant" Moves the agent TEST1 to the Tenant MyTenant .EXAMPLE Get-LinaAgent -Name "TEST1" | Set-LinaAgent -NewName "TEST2" -Tenant "MyTenant" -Group "MyGroup" -Strategy "MyStrategy" -Protection "MyProtection" Changes everything at the same time (Name, Tenant, Group, Strategy and Protection) #> [cmdletbinding(DefaultParameterSetName = 'Update')] Param( [Parameter(ValueFromPipeline = $True, Mandatory = $true, ParameterSetName = "Update")] [pscustomobject[]]$LinaAgents, [Parameter(Mandatory = $false, ParameterSetName = "Update")] [ValidateNotNullOrEmpty()] [string]$NewName, [Parameter(Mandatory = $false, ParameterSetName = "Update")] [ValidateNotNullOrEmpty()] [string]$Tenant, [Parameter(Mandatory = $false, ParameterSetName = "Update")] [ValidateNotNullOrEmpty()] [string]$Group, [Parameter(Mandatory = $false, ParameterSetName = "Update")] [ValidateNotNullOrEmpty()] [string]$Strategy, [Parameter(Mandatory = $false, ParameterSetName = "Update")] [ValidateNotNullOrEmpty()] [string]$Protection ) BEGIN { $lina_agents_processed = 0 } PROCESS { foreach ($LinaAgent in $LinaAgents) { If (!$LinaAgent.ID) { return } $agent_name = $LinaAgent.Name $agent_id = $LinaAgent.ID $agent_tenantname = $LinaAgent.Tenant $timestamp = LinaTimestamp if ($NewName) { # Renaming Agent => can only be done on one agent at a time if ($lina_agents_processed -ge 1) { Write-Host "WARNING : You cannot rename multiple agents with the same name. Agent $agent_name will be ignored." Return } else { Write-Host "Renaming Lina agent named $agent_name to new name $NewName" $timestamp = LinaTimestamp $body = 'data=<?xml version="1.0" encoding="UTF-8"?><HNConfig><Func>REN</Func><Client><ID>' + $agent_id + '</ID><Name>' + $NewName + '</Name></Client></HNConfig>' $request = CallAPI -Path "/mng_client.html?timestamp=$timestamp" -ContentType "application/xml" -Body $body if ([string]$request -like "*[0]*") { Write-Host "Agent $agent_name successfully renamed to $NewName." $agent_name = $NewName } else { TranslateErrorCode -LinaError $request -FailedAction "Renaming agent $agent_name to new name $NewName" Return } } $lina_agents_processed++ } if ($Tenant) { # Moving to new tenant $tenant_id = (LinaTenantShort -Name $Tenant).ID if ($tenant_id -le 1) { Write-Error "ERROR : No tenant found with this name." Return } $body = 'data=<?xml version="1.0" encoding="UTF-8"?><HNConfig><Func>MOV</Func><Client><ID>' + $agent_id + '</ID><DomainID>' + $tenant_id + '</DomainID><Name>@auto</Name></Client></HNConfig>' Write-Host "Moving Lina agent named $agent_name to Tenant $Tenant" $timestamp = LinaTimestamp $request = CallAPI -Path "/mng_client.html?timestamp=$timestamp" -ContentType "application/xml" -Body $body if ([string]$request -like "*[0]*") { Write-Host "Agent $agent_name successfully moved to Tenant $Tenant." $agent_tenantname = $Tenant } else { TranslateErrorCode -LinaError $request -FailedAction "Moving agent $Name to Tenant $Tenant" Return } } if ($Strategy) { # Setting a new strategy $strategy_id = (Get-LinaStrategy -Name $Strategy).ID if ($strategy_id -lt 1 ) { Write-Error "ERROR : No Strategy found with this name." Return } $body = 'data=<?xml version="1.0" encoding="UTF-8"?><HNConfig><Func>SET</Func><Client><ID>' + $agent_id + '</ID><ServiceProfileID>' + $strategy_id + '</ServiceProfileID></Client></HNConfig>' Write-Host "Assigning strategy $Strategy on Lina agent named $agent_name" $timestamp = LinaTimestamp $request = CallAPI -Path "/mng_client.html?timestamp=$timestamp" -ContentType "application/xml" -Body $body if ([string]$request -like "*[0]*") { Write-Host "Strategy $Strategy assigned successfully to Agent $agent_name." } else { TranslateErrorCode -LinaError $request -FailedAction "Assigning strategy $Strategy to agent $Name" Return } } if ($Protection) { # Setting a new strategy $protection_id = (Get-LinaProtection -Name $Protection).ID if ($protection_id -lt 1) { Write-Error "ERROR : No Protection found with this name." Return } $body = 'data=<?xml version="1.0" encoding="UTF-8"?><HNConfig><Func>SET</Func><Client><ID>' + $agent_id + '</ID><DataProfileID>' + $protection_id + '</DataProfileID></Client></HNConfig>' Write-Host "Assigning protection $Protection on Lina agent named $agent_name" $timestamp = LinaTimestamp $request = CallAPI -Path "/mng_client.html?timestamp=$timestamp" -ContentType "application/xml" -Body $body if ([string]$request -like "*[0]*") { Write-Host "Protection $Protection assigned successfully to Agent $agent_name." } else { TranslateErrorCode -LinaError $request -FailedAction "Assigning protection $Protection to agent $Name" Return } } if ($Group) { # Moving to group, filtering to group in the same tenant as the agent to move $group_id = (Get-LinaAgentGroup -Name $Group -Tenant $agent_tenantname).ID if ($group_id -le 0) { Write-Error "ERROR : No group found with this name in the same tenant as the agent." Return } $body = 'data=<?xml version="1.0" encoding="UTF-8"?><HNConfig><Func>SETG</Func><Client><ID>' + $agent_id + '</ID><AttachementArray><Attachement><HierarchyID>1</HierarchyID><GroupID>' + $group_id + '</GroupID></Attachement></AttachementArray></Client></HNConfig>' Write-Host "Moving Lina agent named $agent_name to agent group $Group" $timestamp = LinaTimestamp $request = CallAPI -Path "/mng_client.html?timestamp=$timestamp" -ContentType "application/xml" -Body $body if ([string]$request -like "*[0]*") { Write-Host "Agent $agent_name successfully moved to agent group $Group in Tenant $agent_tenantname." } else { TranslateErrorCode -LinaError $request -FailedAction "Moving agent $Name to Group $Group in Tenant $agent_tenantname." Return } } Return Get-LinaAgent -ID $agent_id } } END { } } function Set-LinaTenant { <# .SYNOPSIS Changes the configuration of a Tenant. .DESCRIPTION Changes the configuration of a tenant for example its Comments. .INPUTS LinaTenant Object .PARAMETER isDefault Set the tenant as the default one .OUTPUTS The modified LinaTenant Object .EXAMPLE Get-LinaTenant -ID 2 | Set-LinaTenant -SetDefault Set the Tenant with ID 2 as the default tenant .EXAMPLE Get-LinaTenant -Name "MyTenant" | Set-LinaTenant Unset the Tenant MyTenant as the default tenant #> [cmdletbinding(DefaultParameterSetName = 'update')] Param( [Parameter(ValueFromPipeline = $True, Mandatory = $true, ParameterSetName = "update")] [pscustomobject[]]$LinaTenants, [Parameter(Mandatory = $false, ParameterSetName = "update")] [Nullable[boolean]]$IsDefault, [Parameter(Mandatory = $false, ParameterSetName = "update")] [Nullable[boolean]]$AutoCreateClients, [Parameter(Mandatory = $false, ParameterSetName = "update")] [ValidateNotNullOrEmpty()] [string]$WindowsDefaultProtection, [Parameter(Mandatory = $false, ParameterSetName = "update")] [ValidateNotNullOrEmpty()] [string]$LinuxDefaultProtection, [Parameter(Mandatory = $false, ParameterSetName = "update")] [ValidateNotNullOrEmpty()] [string]$MacDefaultProtection, [Parameter(Mandatory = $false, ParameterSetName = "update")] [ValidateNotNullOrEmpty()] [string]$DefaultStrategy ) BEGIN { $tenants_modified = 0 } PROCESS { foreach ($Tenant in $LinaTenants) { If (!$Tenant.ID) { return } $tenant_name = $Tenant.Name $tenant_id = $Tenant.ID # Batch all configuration (preferences) modification into a single request $config_modif = 0 $body = 'data=<?xml version="1.0" encoding="UTF-8"?><HNConfig><Func>SET</Func><Globals>' if ($Null -ne $AutoCreateClients) { $body += '<AutoCreateClients>' + ([int]$AutoCreateClients) + '</AutoCreateClients>' $config_modif = 1 } if ($WindowsDefaultProtection -ne "" ) { $current_item = $WindowsDefaultProtection $item_id = (Get-LinaProtection -Name $current_item).ID if ($null -eq $item_id -OR $item_id -le 0) { Write-Host "Error : No Protection found with name $current_item." Return } $body += '<WinDefaultDataProfileID>' + $item_id + '</WinDefaultDataProfileID>' $config_modif = 1 } if ($LinuxDefaultProtection -ne "") { $current_item = $LinuxDefaultProtection $item_id = (Get-LinaProtection -Name $current_item).ID if ($null -eq $item_id -OR $item_id -le 0) { Write-Host "Error : No Protection found with name $current_item." Return } $body += '<LinuxDefaultDataProfileID>' + $item_id + '</LinuxDefaultDataProfileID>' $config_modif = 1 } if ($MacDefaultProtection -ne "") { $current_item = $MacDefaultProtection $item_id = (Get-LinaProtection -Name $current_item).ID if ($null -eq $item_id -OR $item_id -le 0) { Write-Host "Error : No Protection found with name $current_item." Return } $body += '<MacDefaultDataProfileID>' + $item_id + '</MacDefaultDataProfileID>' $config_modif = 1 } if ($DefaultStrategy -ne "") { $current_item = $DefaultStrategy $item_id = (Get-LinaStrategy -Name $current_item).ID if ($null -eq $item_id -OR $item_id -le 0) { Write-Host "Error : No Strategy found with name $current_item." Return } $body += '<WinDefaultServiceProfileID>' + $item_id + '</WinDefaultServiceProfileID>' $config_modif = 1 } if ($config_modif -eq 1 ) { $current_tenant_id = Get-LinaCurrentTenantID 6>$null Write-Host "Modifying configuration of tenant $tenant_name" Set-LinaCurrentTenant -ID $tenant_id 6>$null $body += '</Globals></HNConfig>' $request = CallAPI -Path "/mng_globals.html" -ContentType "application/xml" -Body $body # Setting back the current tenant to what it was before the Set-LinaTenant command Set-LinaCurrentTenant -ID $current_tenant_id 6>$null if ([string]$request -like "*[0]*") { Write-Host "Tenant $tenant_name config has been succesfully applied" } else { TranslateErrorCode -LinaError $request.status -FailedAction "applying config on tenant $tenant_name " Return } } # Modifications of default tenant (not the same requests) if ($Null -ne $IsDefault) { If ($tenants_modified -ge 1) { Write-Host "Warning : Only one tenant can be set as the default one. Only the first one will be set as the default tenant" return } if ($IsDefault -eq $True) { $request = CallAPI -Path "/ADM/domain_set_default.json?id=$tenant_id" -ContentType "application/json" if ($request.status -eq 0) { Write-Host "Tenant $tenant_name is now the default Tenant" $tenants_modified++ } else { TranslateErrorCode -LinaError $request.status -FailedAction "set tenant $tenant_name as the default tenant" Return } } elseif ($IsDefault -eq $False) { if (-NOT (LinaTenantShort -Name $tenant_name).IsDefault) { Write-Host "Tenant $tenant_name is not the default => No modification is required" } $request = CallAPI -Path "/ADM/domain_unset_all.json" -ContentType "application/json" if ($request.status -eq 0) { Write-Host "Tenant $tenant_name is not the default Tenant anymore and no Tenant is set as default." $tenants_modified++ } else { TranslateErrorCode -LinaError $request.status -FailedAction "unset tenant $tenant_name as the default tenant" Return } } } Return Get-LinaTenant -ID $tenant_id } } END { } } |