* Adapted the powershell to connect to the central with a dedicated sql login
* reverted script 9_Update_FTPLogin_Central to the original version
* script 9_Update_FTPLogin_Central is executed on the central directly with a dedicated login
* reverted scripts 20_update_system_site_centrale.sql and 21_update_point_of_sale_centrale.sql to work locally only
*  scripts scripts 20_update_system_site_centrale.sql and 21_update_point_of_sale_centrale.sql are executed on the cenrtal with a specific login and on the local instance
* when fetching the mac addresses, only take up interfaces in account and group the results by mac addresses, to avoid virtual mac addresses duplicate causing an error
This commit is contained in:
2024-03-15 11:23:24 +01:00
parent 6903f71717
commit 88a89d84fe
4 changed files with 93 additions and 168 deletions

View File

@@ -143,6 +143,7 @@ $keyVaultName = "kv-ApoLsPoc-Tde"
$secretFtpPassword ="centralinfra---svc-APH-trans"
$secretMasterKey ="MasterKey"
$secretTriaPharmSQLCert = "TriaPharmSQLCert"
$secretSqlAzureMigration = "sqlAzureMigration"
# Convert the client secret to a secure string
$securePassword = ConvertTo-SecureString -String $clientSecret -AsPlainText -Force
@@ -153,9 +154,11 @@ $psCred = New-Object System.Management.Automation.PSCredential -ArgumentList $cl
# Authenticate using the service principal
$null = Connect-AzAccount -ServicePrincipal -Credential $psCred -TenantId $tenantId
# Get the secret from Azure Key Vault
# $secret = Get-AzKeyVaultSecret -VaultName $keyVaultName -Name $secretName
# write-host "domain: $($ua.Tags["domain"])"
# Get the login for sqlAzureMigration from Azure Key Vault
$secret_central = Get-AzKeyVaultSecret -VaultName $keyVaultName -Name $secretSqlAzureMigration
$login_central = "sqlAzureMigration"
$psCredCentral = New-Object System.Management.Automation.PSCredential($login_central, $secret_central.SecretValue)
# Display the secret value
#Write-Host "Secret Value: $($secret.SecretValue | ConvertFrom-SecureString -AsPlainText)"
@@ -308,55 +311,14 @@ if($true -eq $checkoutGit){
#$instanceReachable = $false
if($true -eq $instanceReachable){
##fetch OU details from control center
## CLA: can be ignored due to SWITCH directly after this QUERY
<#
$query=@"
SELECT LOWER([SERV].[SE_DNS]) AS SE_DNS,
CASE
WHEN UPPER(LEFT([SERV].[SE_DNS], 3)) = 'SCV' THEN 'CVI'
WHEN UPPER(LEFT([SERV].[SE_DNS], 3)) = 'SSU' THEN 'SUN'
WHEN UPPER(LEFT([SERV].[SE_DNS], 3)) = 'SAM' THEN 'AMA'
ELSE UPPER(LEFT([SERV].[SE_DNS], 3))END AS [customer],
CASE
WHEN UPPER([SERV].[SE_designation]) LIKE '%ACCEPTANCE%' THEN 'ACCE'
WHEN UPPER([SERV].[SE_designation]) LIKE '%FORMATION%' THEN 'FORM'
WHEN UPPER([SERV].[SE_designation]) LIKE '%SCHULUNG%' THEN 'FORM'
ELSE 'PROD' END AS [context],
LEFT(LTRIM([cfg].[CF_value]), 4) AS [version],
LOWER([SERV].[SE_DNS] + '\' + [SERV].[SE_instance_name]) AS [aliasInstance]
FROM [ControlCenter].[dbo].[Server] SERV
JOIN [ControlCenter].[dbo].[Config] cfg
ON [cfg].[CF_server] = [SERV].[SE_id]
JOIN [ControlCenter].[dbo].[ConfigType] cfgt
ON [cfgt].[CFTY_id] = [cfg].[CF_configtype]
JOIN [ControlCenter].[dbo].[Entity] ent
ON [ent].[EN_id] = [SERV].[SE_entity]
WHERE [cfgt].[CFTY_type] = 'TriaPharmVersion'
AND [SERV].[SE_status] = 1
AND ( ([SERV].[SE_end_prod_date] > GETDATE())
OR ([SERV].[SE_end_prod_date] IS NULL))
AND [SERV].[SE_designation] NOT LIKE '%Fermé%'
AND [SERV].[SE_designation] NOT LIKE '%REF%'
AND [ent].[EN_code] IN ( 'AMA', 'SUN', 'CVI' )
AND LOWER([SERV].[SE_DNS]) LIKE '$alias%'
ORDER BY [SERV].[SE_entity],
SE_DNS;
"@ #>
# $dbacc = Invoke-Sqlcmd -ServerInstance hcimon -Database ControlCenter -Query $query
if($null -eq $dbacc){
Write-Output "No results in HCIMON, faking them"
$dbacc = New-Object psobject
#$dbacc | Add-Member -MemberType NoteProperty -Name "context" -Value "DEVE"
$dbacc | Add-Member -MemberType NoteProperty -Name "context" -Value $ContextType
$dbacc | Add-Member -MemberType NoteProperty -Name "customer" -Value $customer
$dbacc | Add-Member -MemberType NoteProperty -Name "version" -Value -1
$dbacc | Add-Member -MemberType NoteProperty -Name "SE_DNS" -Value $hostname
$dbacc | Add-Member -MemberType NoteProperty -Name "aliasInstance" -Value $sqlVariables
$dbacc = New-Object psobject
$dbacc | Add-Member -MemberType NoteProperty -Name "context" -Value $ContextType
$dbacc | Add-Member -MemberType NoteProperty -Name "customer" -Value $customer
$dbacc | Add-Member -MemberType NoteProperty -Name "version" -Value -1
$dbacc | Add-Member -MemberType NoteProperty -Name "SE_DNS" -Value $hostname
$dbacc | Add-Member -MemberType NoteProperty -Name "aliasInstance" -Value $sqlVariables
}
##step 1: configure SQL instance max memory
Invoke-Sqlcmd -ServerInstance $SqlInstance -Database master -InputFile .\1_Configure_InstanceMemory.sql
Write-Output "Executed 1_Configure_InstanceMemory.sql"
@@ -392,10 +354,9 @@ if($true -eq $instanceReachable){
$query = $query.Replace("`$fqdn", $alias)
#Invoke-Sqlcmd -ServerInstance gcmPrdCent -Database arizona -Query $query
Invoke-Sqlcmd -ServerInstance $SqlInstance -Database arizona -Query $query
Invoke-Sqlcmd -ServerInstance $CentraleServer -Database arizona -Query $query -Credential $psCredCentral
Write-Output "Executed 9_Update_FTPLogin_Central.sql"
##step 6: execute all DBA packages
$LogFile = "C:\Temp\DBApackagesCLA.log"
@@ -532,21 +493,44 @@ if($true -eq $instanceReachable){
##step 20: update system_site on the central
$fqdn = [System.Net.Dns]::GetHostByName($alias)
#Get pharmacy OU_id
$query=@"
USE [master];
DECLARE @ou_id INT;
EXECUTE [Arizona].[dbo].[sp_bmc_Bmc_Applic_Default]
@in_job_type = 3,
@in_param_int_1 = NULL, /* Company */
@in_param_int_2 = NULL, /* Subsidiary */
@in_param_varchar_1 = 'cvCurrentOrganizationalUnit',
@out_default_value = @ou_id OUTPUT,
@out_param_int_1 = NULL;
SELECT @ou_id as ou_id
"@
$ou_dataset = Invoke-Sqlcmd -ServerInstance $SqlInstance -Database master -Query $query
#update the centrale
$query=Get-Content -Path .\20_update_system_site_centrale.sql
$query = $query.Replace("`$fqdn", $fqdn.HostName)
$query = $query.Replace("`$alias", $alias)
$query = $query.Replace("`$ou_id", $ou_dataset.ou_id)
Invoke-Sqlcmd -ServerInstance $SqlInstance -Database master -Query $query
Write-Output "Executed 20_update_system_site_centrale.sql"
Write-Output "Executed 20_update_system_site_centrale.sql locally"
##step 21: update Point_of_sale on the central
Invoke-Sqlcmd -ServerInstance $CentraleServer -Database master -Query $query -Credential $psCredCentral
Write-Output "Executed 20_update_system_site_centrale.sql on the central $CentraleServer"
##step 21: update Point_of_sale
#fetch a list of mac adresses
$macs = @{}
$pos = 0
Get-NetAdapter | Select-Object ifIndex, Name, MacAddress | ForEach-Object {
$macs.Add($pos, $_.MacAddress)
Get-NetAdapter | Where-Object -Property status -eq up | Group-Object -Property MacAddress | ForEach-Object {
$macs.Add($pos, $_.Name)
$pos++
}
@@ -556,11 +540,12 @@ if($true -eq $instanceReachable){
exit 42102
}
else{
$query=Get-Content -Path .\21_update_point_of_sale_centrale.sql
$query = $query.Replace("`$fqdn", $fqdn.HostName)
$query = $query.Replace("`$mac", $macs[0].value)
$query=Get-Content -Path C:\Users\ua208700\Documents\21_update_point_of_sale_centrale.sql
$query = $query.Replace("`$fqdn", $hostname)
$query = $query.Replace("`$mac", $macs[0])
Invoke-Sqlcmd -ServerInstance $SqlInstance -Database master -Query $query
#Invoke-Sqlcmd -ServerInstance $SqlInstance -Database master -Query $query
Write-Output $query
Write-Output "Executed 21_update_point_of_sale_centrale.sql"
}